Skip to content

feat(observability): auto-instrument httpx for outbound trace propagation#4148

Open
ecthelion77 wants to merge 1 commit intoIBM:mainfrom
forterro:feat/otel-httpx-instrumentation-upstream
Open

feat(observability): auto-instrument httpx for outbound trace propagation#4148
ecthelion77 wants to merge 1 commit intoIBM:mainfrom
forterro:feat/otel-httpx-instrumentation-upstream

Conversation

@ecthelion77
Copy link
Copy Markdown
Contributor

@ecthelion77 ecthelion77 commented Apr 13, 2026

✨ 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 propagate traceparent headers to upstream services.

Changes:

  1. mcpgateway/observability.py: Adds opentelemetry-instrumentation-httpx initialization in setup_observability(), calling HTTPXClientInstrumentor().instrument() to auto-instrument all httpx.AsyncClient and httpx.Client instances.

  2. gunicorn.config.py: Re-applies httpx instrumentation in post_fork(). Gunicorn's pre-fork model requires re-initialization of OTel instrumentors in each worker process. The implementation calls uninstrument() first to avoid double-instrumentation warnings, then instrument().

  3. pyproject.toml: Adds opentelemetry-instrumentation-httpx as a dependency.


🧪 Checks

  • make lint passes
  • make test passes
  • CHANGELOG updated (if user-facing)

📓 Notes

Why uninstrument() before instrument() in post_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 is uninstrument() + instrument() to ensure clean re-initialization in each worker.

@ecthelion77
Copy link
Copy Markdown
Contributor Author

Suggested labels: enhancement, COULD, python, observability

@ecthelion77 ecthelion77 force-pushed the feat/otel-httpx-instrumentation-upstream branch from e15f90a to d2ff2f1 Compare April 14, 2026 12:46
@ecthelion77 ecthelion77 force-pushed the feat/otel-httpx-instrumentation-upstream branch 2 times, most recently from bd16bed to 2c1a113 Compare April 14, 2026 15:34
…tion

Signed-off-by: Olivier Gintrand <olivier.gintrand@forterro.com>
@ecthelion77 ecthelion77 force-pushed the feat/otel-httpx-instrumentation-upstream branch from 2c1a113 to 4322a24 Compare April 14, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE][OBSERVABILITY]: Auto-instrument httpx for outbound trace propagation to upstream servers

1 participant