Skip to content

Commit ad58180

Browse files
committed
Add WriteHeader call to Flush
Flush is another of the methods that will call WriteHeader if it hasn't happened yet. Since we want to call observeWriteHeader (if set), we need to do the WriteHeader call already here, similar to what we have done in Write and ReadFrom. This commit also adds comments explaining the above to not tempt developers to remove the WriteHeader call. Signed-off-by: beorn7 <[email protected]>
1 parent 4efc3cc commit ad58180

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

prometheus/promhttp/delegator.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func (r *responseWriterDelegator) WriteHeader(code int) {
6262
}
6363

6464
func (r *responseWriterDelegator) Write(b []byte) (int, error) {
65+
// If applicable, call WriteHeader here so that observeWriteHeader is
66+
// handled appropriately.
6567
if !r.wroteHeader {
6668
r.WriteHeader(http.StatusOK)
6769
}
@@ -82,12 +84,19 @@ func (d closeNotifierDelegator) CloseNotify() <-chan bool {
8284
return d.ResponseWriter.(http.CloseNotifier).CloseNotify()
8385
}
8486
func (d flusherDelegator) Flush() {
87+
// If applicable, call WriteHeader here so that observeWriteHeader is
88+
// handled appropriately.
89+
if !d.wroteHeader {
90+
d.WriteHeader(http.StatusOK)
91+
}
8592
d.ResponseWriter.(http.Flusher).Flush()
8693
}
8794
func (d hijackerDelegator) Hijack() (net.Conn, *bufio.ReadWriter, error) {
8895
return d.ResponseWriter.(http.Hijacker).Hijack()
8996
}
9097
func (d readerFromDelegator) ReadFrom(re io.Reader) (int64, error) {
98+
// If applicable, call WriteHeader here so that observeWriteHeader is
99+
// handled appropriately.
91100
if !d.wroteHeader {
92101
d.WriteHeader(http.StatusOK)
93102
}

0 commit comments

Comments
 (0)