For the purpose of accurate request tracing when serving HTTP requests (for both HTTP/1.x and HTTP/2), it would be ideal if http.Handlers could obtain a time.Time representing the time of first byte of the request.
In this context, time of first byte would be the best estimate of the instant when the go process reads the first byte of the HTTP request line from the socket (i.e. ignoring the time spent in hardware queues and in kernel buffers).
Between the time of first byte and the time the first http.Handler is invoked for that request quite some time can pass since in that interval all headers must be received (and parsed) and the request body must start. If the client is slow then this interval can be extremely long.
Furthermore, when using the middleware pattern for tracing, the order of middleware can influence the recorded time at which the request starts to be handled. This can skew metrics and the accuracy of tracing, leading to hard-to-debug performance issues.
With this proposal implemented, all of the above issues would be solved:
- request parsing would be correctly accounted for
- slow clients can be correctly accounted for
- request start time can be reliably detected irrespective of middleware order
This information could be included either in the http.Request struct, returned by a method on http.Request, or attached to the request context.Context (perhaps via something like the httptrace.ServerTrace suggestion below by @odeke-em).
update: rephrased according to @odeke-em's suggestions
For the purpose of accurate request tracing when serving HTTP requests (for both HTTP/1.x and HTTP/2), it would be ideal if
http.Handlers could obtain atime.Timerepresenting the time of first byte of the request.In this context, time of first byte would be the best estimate of the instant when the go process reads the first byte of the HTTP request line from the socket (i.e. ignoring the time spent in hardware queues and in kernel buffers).
Between the time of first byte and the time the first
http.Handleris invoked for that request quite some time can pass since in that interval all headers must be received (and parsed) and the request body must start. If the client is slow then this interval can be extremely long.Furthermore, when using the middleware pattern for tracing, the order of middleware can influence the recorded time at which the request starts to be handled. This can skew metrics and the accuracy of tracing, leading to hard-to-debug performance issues.
With this proposal implemented, all of the above issues would be solved:
This information could be included either in the
http.Requeststruct, returned by a method onhttp.Request, or attached to the requestcontext.Context(perhaps via something like thehttptrace.ServerTracesuggestion below by @odeke-em).update: rephrased according to @odeke-em's suggestions