New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set http.status_code span attribute for handlers that do not write explicit response #2821
Comments
I think we're missing implementation of Flusher in respWriterWrapper. In the net/http implementation of ResponseWriter, we can see Flush does write the header if that hasn't happened yet: https://cs.opensource.google/go/go/+/refs/tags/go1.19.1:src/net/http/server.go;l=1702 |
Good catch. That does indeed appear to be the issue. The
Would it be reasonable to expect this to be added to the |
I think this would definitely be reasonable. We would definitely be happy to see a PR doing that. |
@dmathieu I have started digging into this a bit, and it seems simply implementing the In essence, the handler := otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// The wrapped ResponseWriter will ONLY have bearing on whatever happens inside this function.
fmt.Println("handler invoked")
// Uncommenting either of the two lines below will produce a span that contains
// the http.status_code attribute. When commented, the span does not have this attribute.
// w.WriteHeader(200)
// fmt.Fprintf(w, "OK")
}), "myHandler") Therefore, implementing the It looks like the In conclusion, I would say that implementing the |
You are absolutely right. I've given this a go in #2822. |
Background
The
otelhttp.NewHandler
wraps a handler with a span. After the wrapped handler'sServeHTTP
method is called, thesetAfterServeAttributes
function is called, setting several span attributes are set.One such attribute is the
http.status_code
attribute, which is set if and only if a status code was explicitly set via theResponseWriter
(see documentation). This means that thehttp.status_code
attribute will only be set if the handler function explicitly makes a call toResponseWriter.WriteHeader
orResponseWriter.Write
. Allow me to illustrate with the following very minimal code sample:The issue and proposed solution
In the code snippet above, a minimal HTTP server is created using Go's standard library. Even if the handler function does not explicitly call
w.WriteHeader(200)
orfmt.Fprintf(w, "OK")
, the server will still respond with a 200 status code. In other words, it appears the built-in HTTP server will always set the 200 status code on the response if the handler function returns without panicking or setting a different status code.It therefore appears that the OpenTelemetry instrumentation is slightly too restrictive when it comes to setting the respective
http.status_code
span attribute. Would it not be safe to, under the same assumptions as the HTTP server itself, set this attribute to 200 if the handler function did not explicitly set a different status code? I can easily imagine a scenario in which a user might be under the presumption that this attribute is included in the span, since the request itself did successfully return with a 200 status code. This seems especially significant for HTTP handlers which do not return content, and only a status code.The text was updated successfully, but these errors were encountered: