HN
Today

Tracing HTTP Requests with Go's net/HTTP/httptrace

This article delves into Go's often-overlooked net/http/httptrace package, revealing how it enables granular tracing of HTTP requests from DNS resolution to the first byte received. It demystifies the package's unique context.Context-based design, offering practical examples like a curl --trace-style CLI and a logging http.RoundTripper. This level of detail, without external tooling, is precisely the kind of low-level insight Go developers on HN appreciate.

6
Score
0
Comments
#2
Highest Rank
8h
on Front Page
First Seen
Jun 1, 9:00 AM
Last Seen
Jun 1, 4:00 PM
Rank Over Time
8424391317

The Lowdown

The net/http/httptrace package, a part of Go's standard library since 1.7, provides powerful hooks to inspect the lifecycle of an outgoing HTTP request at a granular level. Unlike typical tracing implementations, it leverages context.Context to attach tracing information to requests, allowing for flexible, concurrent, and efficient monitoring without shared mutable state.

  • The article explains why httptrace uses context.Context instead of an interface on http.Client, highlighting how this design allows tracing information to travel with the request and compose with existing middleware.
  • It details the ClientTrace struct, which contains various optional function fields (hooks) for events like DNSStart, TLSHandshakeStart, GotConn, and GotFirstResponseByte.
  • Two practical examples are provided: building a curl --trace-like command-line utility to measure various stages of an HTTP request, such as DNS lookup, TCP connect, and TLS handshake duration.
  • The second example demonstrates creating an http.RoundTripper to automatically log timing information for every request made by an http.Client, including a technique to capture total request duration by wrapping the response body.
  • It also shows how httptrace can reveal crucial insights like connection reuse status through the GotConnInfo struct, helping diagnose connection pooling issues.

In conclusion, net/http/httptrace offers a simple yet powerful built-in mechanism for deep HTTP request introspection. Its context-driven design integrates seamlessly with the Go standard library, making it an invaluable tool for debugging network performance without the need for external libraries or complex instrumentation.