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
Fix superfluous response.WriteHeader call
#47715
base: master
Are you sure you want to change the base?
Conversation
// Used instead of the Flush() we had previously, so we don't set the header twice because of the otel wrapper | ||
// see here: https://github.com/moby/moby/issues/47448 | ||
if rw, ok := w.(http.ResponseWriter); ok { | ||
rw.WriteHeader(http.StatusOK) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to figure out how writing this here would prevent OTEL from trying to write a header again; wouldn't it still hit the "already wrote header" case?
moby/vendor/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/wrap.go
Lines 93 to 99 in f9dfd13
func (w *respWriterWrapper) WriteHeader(statusCode int) { | |
if !w.wroteHeader { | |
w.wroteHeader = true | |
w.statusCode = statusCode | |
} | |
w.ResponseWriter.WriteHeader(statusCode) | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to figure out how writing this here would prevent OTEL from trying to write a header again; wouldn't it still hit the "already wrote header" case?
moby/vendor/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/wrap.go
Lines 93 to 99 in f9dfd13
func (w *respWriterWrapper) WriteHeader(statusCode int) { if !w.wroteHeader { w.wroteHeader = true w.statusCode = statusCode } w.ResponseWriter.WriteHeader(statusCode) }
Our Flusher calls Flush()
on the stdlib implementation, not on the OTEL wrapper because it doesn't wrap the Flush()
func. WriteHeader(), so if we call this here we are setting w.wroteHeader
to true on the OTEL wrapper, so that subsequent calls to the OTEL wrapper's Write()
func will NOT call the OTEL wrapper's WriteHeader()
func.
example of current sitation:
calls to flusher.Flush()
;
- stdlib
Flush(
) gets called byflusher.Flush()
; - stdlib
Write()
gets called by stdlibFlush()
; - stdlib
WriteHeader()
gets called by stdlibWrite()
calls to flusher.Write()
:
- OTEL wrapper
Write()
gets called byflusher.Write()
; - OTEL wrapper
WriteHeader()
gets called by OTEL wrapperWrite()
- only when it hasn't been called before; - stdlib
WriteHeader()
gets called by OTEL wrapperWriteHeader()
; - stdlib
Write()
gets called by OTEL wrapperWrite()
; flusher.Flush()
gets called byflusher.Write()
;- ... repeat above flow
Sorry for the verbosity, too much wrapping confusion here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we either to need to manually instrument the streaming responses or provide a smarter writer implementation to the daemon so that the first flush calls WriteHeader explicitly.
…Flush() Signed-off-by: Christopher Petito <47751006+krissetto@users.noreply.github.com>
64b33e2
to
8224204
Compare
This seems like the cleanest/least-hacky fix for this, thanks! |
// Used before the Flush(), so we don't set the header twice because of the otel wrapper | ||
// see here: https://github.com/moby/moby/issues/47448 | ||
if rw, ok := outStream.(http.ResponseWriter); ok { | ||
rw.WriteHeader(http.StatusOK) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the right place to handle this.
I'm also not sure how this fix the otel issue since the very first write to the stream would be writing out the response header.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the right place to handle this.
...
It seems like we either to need to manually instrument the streaming responses or provide a smarter writer implementation to the daemon so that the first flush calls WriteHeader explicitly.
Yeah i mostly agree with this, but there's another couple things to consider here:
WriteFlusher
is currently exported from our ioutils pkg, and used by a few other projects (https://grep.app/search?q=.NewWriteFlusher%28&filter[lang][0]=Go);- We'd have to call Write() instead of WriteHeader() during Flush(), because of how the otel wrapper is written.
I have opened another draft pr yesterday, moving the current WriteFlusher
impl and making a new internal one in /internal/writeflusher
with these changes.
If we merge that, this PR can be closed as it'd be unnecessary, but I left it here in case we decide on a different strategy and we only want a quick fix for now.
// Used instead of the Flush() we had previously, so we don't set the header twice because of the otel wrapper | ||
// see here: https://github.com/moby/moby/issues/47448 | ||
if rw, ok := w.(http.ResponseWriter); ok { | ||
rw.WriteHeader(http.StatusOK) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like we either to need to manually instrument the streaming responses or provide a smarter writer implementation to the daemon so that the first flush calls WriteHeader explicitly.
Fixes #47448
- What I did
Edited the endpoints for container stats and logs to avoid
superfluous response.WriteHeader call
being logged by the stdlib because we callWriteHeader()
too many times.- How I did it
Writing the response headers manually for streaming response writers instead of calling
Flush()
.OTEL instrumentation doesn't seem to wrap the
Flush()
func ofhttp.ResponseWriter
, so if we callFlush()
onioutils.WriteFlusher
we directly hit the underlying stdlib implementation ofWrite()
/WriteHeader()
instead of the OTEL wrapped one.If we then call
Write()
on the OTEL wrapper, after manually callingFlush()
fromioutils
, the wrapper will not know that headers have been written already and will therefore call the stdlibWriteHeaders()
again, causing the warning log- How to verify it
- Description for the changelog
- A picture of a cute animal (not mandatory but encouraged)