Hasty Briefsbeta

Bilingual

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

2 days ago
  • #Performance Debugging
  • #Go Programming
  • #HTTP Tracing
  • httptrace exposes hooks for HTTP request events like DNS resolution, connection acquisition, TLS handshake, and response byte arrival.
  • Traces are attached via context.Context using httptrace.WithClientTrace, allowing per-request tracing and easy composition with middleware.
  • The design uses a struct of optional function fields, enabling addition of new hooks without breaking existing code.
  • A CLI tool can be built to output timing breakdowns similar to curl --trace by recording timestamps in each hook.
  • A reusable http.RoundTripper can automatically trace all requests through a client, logging timing information.
  • Timing hooks allow debugging of connection reuse, DNS lookups, TLS handshakes, server processing, and content transfer.
  • Traces can be composed when multiple are attached to a context, preserving all hooks.
  • Reading the response body's close event ensures total request duration includes body transfer.
  • GotConnInfo reveals connection reuse status, helping diagnose connection pooling issues.