Skip to content

otelhttp: Ignore informational response status codes when persisting status #6913

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- Jaeger remote sampler's probabilistic strategy now uses the same sampling algorithm as `trace.TraceIDRatioBased` in `go.opentelemetry.io/contrib/samplers/jaegerremote`. (#6892)
- Ignore informational response status codes (`100-199`) when storing the HTTP status code in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` and `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`. (#6913)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (w *RespWriterWrapper) Write(p []byte) (int, error) {

// WriteHeader persists initial statusCode for span attribution.
// All calls to WriteHeader will be propagated to the underlying ResponseWriter
// and will persist the statusCode from the first call.
// and will persist the statusCode from the first call (except for informational response status codes).
// Blocking consecutive calls to WriteHeader alters expected behavior and will
// remove warning logs from net/http where developers will notice incorrect handler implementations.
func (w *RespWriterWrapper) WriteHeader(statusCode int) {
Expand All @@ -77,6 +77,13 @@ func (w *RespWriterWrapper) WriteHeader(statusCode int) {
// parent method.
func (w *RespWriterWrapper) writeHeader(statusCode int) {
if !w.wroteHeader {
// Ignore informational response status codes.
// Based on https://github.com/golang/go/blob/go1.24.1/src/net/http/server.go#L1216
if statusCode >= 100 && statusCode <= 199 && statusCode != http.StatusSwitchingProtocols {
w.ResponseWriter.WriteHeader(statusCode)
return
}

w.wroteHeader = true
w.statusCode = statusCode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func TestRespWriterWriteHeader(t *testing.T) {
assert.Equal(t, http.StatusTeapot, rw.statusCode)
}

func TestRespWriterWriteInformationalStatusCode(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})

rw.WriteHeader(http.StatusContinue)
assert.Equal(t, http.StatusOK, rw.statusCode)
assert.False(t, rw.wroteHeader)

rw.WriteHeader(http.StatusGone)
assert.Equal(t, http.StatusGone, rw.statusCode)
assert.True(t, rw.wroteHeader)
}

func TestRespWriterFlush(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (w *RespWriterWrapper) Write(p []byte) (int, error) {

// WriteHeader persists initial statusCode for span attribution.
// All calls to WriteHeader will be propagated to the underlying ResponseWriter
// and will persist the statusCode from the first call.
// and will persist the statusCode from the first call (except for informational response status codes).
// Blocking consecutive calls to WriteHeader alters expected behavior and will
// remove warning logs from net/http where developers will notice incorrect handler implementations.
func (w *RespWriterWrapper) WriteHeader(statusCode int) {
Expand All @@ -77,6 +77,13 @@ func (w *RespWriterWrapper) WriteHeader(statusCode int) {
// parent method.
func (w *RespWriterWrapper) writeHeader(statusCode int) {
if !w.wroteHeader {
// Ignore informational response status codes.
// Based on https://github.com/golang/go/blob/go1.24.1/src/net/http/server.go#L1216
if statusCode >= 100 && statusCode <= 199 && statusCode != http.StatusSwitchingProtocols {
w.ResponseWriter.WriteHeader(statusCode)
return
}

w.wroteHeader = true
w.statusCode = statusCode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func TestRespWriterWriteHeader(t *testing.T) {
assert.Equal(t, http.StatusTeapot, rw.statusCode)
}

func TestRespWriterWriteInformationalStatusCode(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})

rw.WriteHeader(http.StatusContinue)
assert.Equal(t, http.StatusOK, rw.statusCode)
assert.False(t, rw.wroteHeader)

rw.WriteHeader(http.StatusGone)
assert.Equal(t, http.StatusGone, rw.statusCode)
assert.True(t, rw.wroteHeader)
}

func TestRespWriterFlush(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})

Expand Down
9 changes: 8 additions & 1 deletion internal/shared/request/resp_writer_wrapper.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (w *RespWriterWrapper) Write(p []byte) (int, error) {

// WriteHeader persists initial statusCode for span attribution.
// All calls to WriteHeader will be propagated to the underlying ResponseWriter
// and will persist the statusCode from the first call.
// and will persist the statusCode from the first call (except for informational response status codes).
// Blocking consecutive calls to WriteHeader alters expected behavior and will
// remove warning logs from net/http where developers will notice incorrect handler implementations.
func (w *RespWriterWrapper) WriteHeader(statusCode int) {
Expand All @@ -77,6 +77,13 @@ func (w *RespWriterWrapper) WriteHeader(statusCode int) {
// parent method.
func (w *RespWriterWrapper) writeHeader(statusCode int) {
if !w.wroteHeader {
// Ignore informational response status codes.
// Based on https://github.com/golang/go/blob/go1.24.1/src/net/http/server.go#L1216
if statusCode >= 100 && statusCode <= 199 && statusCode != http.StatusSwitchingProtocols {
w.ResponseWriter.WriteHeader(statusCode)
return
}

w.wroteHeader = true
w.statusCode = statusCode
}
Expand Down
12 changes: 12 additions & 0 deletions internal/shared/request/resp_writer_wrapper_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func TestRespWriterWriteHeader(t *testing.T) {
assert.Equal(t, http.StatusTeapot, rw.statusCode)
}

func TestRespWriterWriteInformationalStatusCode(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})

rw.WriteHeader(http.StatusContinue)
assert.Equal(t, http.StatusOK, rw.statusCode)
assert.False(t, rw.wroteHeader)

rw.WriteHeader(http.StatusGone)
assert.Equal(t, http.StatusGone, rw.statusCode)
assert.True(t, rw.wroteHeader)
}

func TestRespWriterFlush(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})

Expand Down