Skip to content

Commit 4fe93f6

Browse files
committed
promhttp: Ignore informational header when handling status code
Signed-off-by: Janusz Marcinkiewicz <[email protected]>
1 parent 45edd8a commit 4fe93f6

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

prometheus/promhttp/delegator.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,25 @@ 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 headers.
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+
goto propagate
61+
}
62+
63+
if r.observeWriteHeader != nil {
64+
// Only call observeWriteHeader for the 1st time. It's a bug if
65+
// WriteHeader is called more than once, but we want to protect
66+
// against it here. Note that we still delegate the WriteHeader
67+
// to the original ResponseWriter to not mask the bug from it.
68+
r.observeWriteHeader(code)
69+
}
6270
}
6371
r.status = code
6472
r.wroteHeader = true
73+
74+
propagate:
6575
r.ResponseWriter.WriteHeader(code)
6676
}
6777

0 commit comments

Comments
 (0)