Search code examples
httpgologgingtrace

Tracing an HTTP request in Go in a one structured log line


I have learned you can "decorate" the HTTP transport so that you can details of the request, however I can't quite figure out how you log the URL in the same line.

https://play.golang.org/p/g-ypQN9ceGa

results in

  INFO[0000] Client request            dns_start_ms=0 first_byte_ms=590 response_code=200 total_ms=590 url=
  INFO[0000] 200

I'm perpetually confused if I should be using https://golang.org/pkg/context/#WithValue to pass around the context in a struct, especially in light where https://blog.golang.org/context-and-structs concludes with pass context.Context in as an argument.


Solution

  • Replace r.RequestURI by r.URL.String() in your code to log the full, valid URL (https://golang.org/pkg/net/url/#URL.String). RequestURI is empty on the client side (https://golang.org/pkg/net/http/#Request), as the output from your code is showing.

    I don't see how context.Context relates to your question, but I believe https://blog.golang.org/context-and-structs is considered "best practice".