Skip to content
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

Request context is overridden in instrumented http clients #580

Closed
shturec opened this issue May 16, 2019 · 1 comment · Fixed by #582
Closed

Request context is overridden in instrumented http clients #580

shturec opened this issue May 16, 2019 · 1 comment · Fixed by #582

Comments

@shturec
Copy link

shturec commented May 16, 2019

It is quite common for golang http clients to make use of the context package. Example use cases are to halt stalled requests or shutdown appropriately for example. When an application uses the github.com/prometheus/client_golang/prometheus package to instrument an http client for metrics, it expects the transport wrappers to preserve the behavior and state of the wrapped client.

However, what happens with a request context set before the instrumentation of the client is that it gets replaced by the default root context and that makes quite of a difference:

r = r.WithContext(httptrace.WithClientTrace(context.Background(), trace))

From the golang docs:
"WithContext returns a shallow copy of r with its context changed to ctx."

The effect is that if you created a context with, for example, context.WithTimeout and set it to a http.Client (client.WithContext(ctx)), and then instrument the client, for example as suggested here, and then send requests to a server that is slow enough to trigger timeout, the timeout will never occur.

What is expected instead is that the InstrumentRoundTripperTrace function respects existing request context, if any.

All findings are wrt v0.9.2 and master.

@beorn7
Copy link
Member

beorn7 commented May 16, 2019

Thanks for the report.

I assume this is only an issue with the InstrumentRoundTripperTrace function. Please let me know if that's not true.

beorn7 added a commit that referenced this issue May 16, 2019
Fixes #580

Signed-off-by: beorn7 <bjoern@rabenste.in>
beorn7 added a commit that referenced this issue May 16, 2019
Signed-off-by: beorn7 <bjoern@rabenste.in>
beorn7 added a commit that referenced this issue May 16, 2019
Fixes #580

Signed-off-by: beorn7 <bjoern@rabenste.in>
beorn7 added a commit that referenced this issue May 16, 2019
Tests are heavily inspired by @shturec, see #584.

Signed-off-by: beorn7 <bjoern@rabenste.in>
beorn7 added a commit that referenced this issue May 16, 2019
Fixes #580

Signed-off-by: beorn7 <bjoern@rabenste.in>
beorn7 added a commit that referenced this issue May 16, 2019
Tests are heavily inspired by @shturec, see #584.

Signed-off-by: beorn7 <bjoern@rabenste.in>
beorn7 added a commit that referenced this issue May 16, 2019
Fixes #580

Signed-off-by: beorn7 <bjoern@rabenste.in>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants