Skip to content

Commit 742f891

Browse files
committed
promhttp: Ignore informational response status codes when handling
response Signed-off-by: Janusz Marcinkiewicz <[email protected]>
1 parent 45edd8a commit 742f891

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

prometheus/promhttp/delegator.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,21 @@ func (r *responseWriterDelegator) Written() int64 {
5353
}
5454

5555
func (r *responseWriterDelegator) WriteHeader(code int) {
56-
if r.observeWriteHeader != nil && !r.wroteHeader {
57-
// Only call observeWriteHeader for the 1st time. It's a bug if
58-
// WriteHeader is called more than once, but we want to protect
59-
// against it here. Note that we still delegate the WriteHeader
60-
// to the original ResponseWriter to not mask the bug from it.
61-
r.observeWriteHeader(code)
56+
if !r.wroteHeader {
57+
// Ignore informational response status codes.
58+
// Based on https://github.com/golang/go/blob/go1.24.1/src/net/http/server.go#L1216
59+
if code >= 100 && code <= 199 && code != http.StatusSwitchingProtocols {
60+
r.ResponseWriter.WriteHeader(code)
61+
return
62+
}
63+
64+
if r.observeWriteHeader != nil {
65+
// Only call observeWriteHeader for the 1st time. It's a bug if
66+
// WriteHeader is called more than once, but we want to protect
67+
// against it here. Note that we still delegate the WriteHeader
68+
// to the original ResponseWriter to not mask the bug from it.
69+
r.observeWriteHeader(code)
70+
}
6271
}
6372
r.status = code
6473
r.wroteHeader = true

0 commit comments

Comments
 (0)