feat(observability): auto-instrument httpx for outbound trace propagation#4148
Open
ecthelion77 wants to merge 1 commit intoIBM:mainfrom
Open
feat(observability): auto-instrument httpx for outbound trace propagation#4148ecthelion77 wants to merge 1 commit intoIBM:mainfrom
ecthelion77 wants to merge 1 commit intoIBM:mainfrom
Conversation
Contributor
Author
|
Suggested labels: |
e15f90a to
d2ff2f1
Compare
bd16bed to
2c1a113
Compare
…tion Signed-off-by: Olivier Gintrand <olivier.gintrand@forterro.com>
2c1a113 to
4322a24
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ Feature / Enhancement PR
🔗 Epic / Issue
Closes #4155
Improves observability for outbound HTTP calls made by the gateway to upstream MCP servers and OAuth providers.
🚀 Summary
This PR adds automatic OpenTelemetry instrumentation for
httpx, the HTTP client used by the gateway for all outbound requests (MCP server communication, OAuth token exchanges, health checks). Without this, outbound HTTP calls are invisible in distributed traces — the trace stops at the gateway and does not propagatetraceparentheaders to upstream services.Changes:
mcpgateway/observability.py: Addsopentelemetry-instrumentation-httpxinitialization insetup_observability(), callingHTTPXClientInstrumentor().instrument()to auto-instrument allhttpx.AsyncClientandhttpx.Clientinstances.gunicorn.config.py: Re-applies httpx instrumentation inpost_fork(). Gunicorn's pre-fork model requires re-initialization of OTel instrumentors in each worker process. The implementation callsuninstrument()first to avoid double-instrumentation warnings, theninstrument().pyproject.toml: Addsopentelemetry-instrumentation-httpxas a dependency.🧪 Checks
make lintpassesmake testpasses📓 Notes
Why
uninstrument()beforeinstrument()inpost_fork?Gunicorn forks workers from the master process. If the master already instrumented httpx, the forked worker inherits the instrumented state. Calling
instrument()again would raise warnings. The safe pattern isuninstrument()+instrument()to ensure clean re-initialization in each worker.