Feature/httpx2 instrumentor#4730
Open
gloshkajian wants to merge 12 commits into
Open
Conversation
…isn't backed by httpx2 classes
herin049
approved these changes
Jun 23, 2026
herin049
reviewed
Jun 23, 2026
There was a problem hiding this comment.
Pull request overview
Adds httpx2 support to the existing opentelemetry-instrumentation-httpx package, enabling independent instrumentation of httpx vs httpx2 (including coexistence), while reusing shared wrapping logic via a parameterized base instrumentor.
Changes:
- Introduces
HTTPX2ClientInstrumentorandhttpx2transport wrapper classes alongside the existinghttpxinstrumentor/transport support. - Adds dedicated
httpx2and coexistence tox environments, CI jobs, and new integration tests forhttpx2. - Updates documentation, bootstrap mapping, and dependency metadata/extras to reflect dual-library support.
Reviewed changes
Copilot reviewed 18 out of 19 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| uv.lock | Adds locked deps for httpx2/httpcore2 and adjusts extras metadata to include instruments-any. |
| tox.ini | Adds httpx2 and coexistence test envs and routes commands per factor (-2, -3). |
| pyproject.toml | Updates dev dependency to use opentelemetry-instrumentation-httpx[instruments-any]. |
| opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py | Adds httpx2 → opentelemetry-instrumentation-httpx mapping for bootstrap generation. |
| instrumentation/README.md | Updates instrumentation table row to list both httpx and httpx2. |
| instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx2_integration.py | New httpx2 integration test suite covering global, client-level, transport-wrapper, hooks, and excluded URLs. |
| instrumentation/opentelemetry-instrumentation-httpx/test-requirements-2.txt | Adds pinned deps for httpx2-only test environment. |
| instrumentation/opentelemetry-instrumentation-httpx/test-requirements-2-wrapt1.txt | Adds wrapt1 variant for httpx2-only test env. |
| instrumentation/opentelemetry-instrumentation-httpx/test-requirements-2-wrapt2.txt | Adds wrapt2 variant for httpx2-only test env. |
| instrumentation/opentelemetry-instrumentation-httpx/test-requirements-3.txt | Adds pinned deps for coexistence (httpx + httpx2) test environment. |
| instrumentation/opentelemetry-instrumentation-httpx/test-requirements-3-wrapt1.txt | Adds wrapt1 variant for coexistence test env. |
| instrumentation/opentelemetry-instrumentation-httpx/test-requirements-3-wrapt2.txt | Adds wrapt2 variant for coexistence test env. |
| instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/package.py | Splits dependency tuples into _instruments_httpx / _instruments_httpx2 and aggregates via _instruments_any. |
| instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/init.py | Refactors shared implementation to parameterize the target module and adds HTTPX2ClientInstrumentor + httpx2-specific transports/entry point support. |
| instrumentation/opentelemetry-instrumentation-httpx/README.rst | Documents httpx2 usage (global/client/transport patterns). |
| instrumentation/opentelemetry-instrumentation-httpx/pyproject.toml | Adds instruments-any extra with httpx + httpx2, and registers httpx2 instrumentor entry point. |
| .github/workflows/test.yml | Adds CI jobs for httpx2-only and coexistence test envs across Python versions/wrapt variants. |
| .github/workflows/core_contrib_test.yml | Adds core-contrib CI jobs for the new httpx2 and coexistence test envs. |
| .changelog/4730.added | Changelog entry announcing httpx2 support for the httpx instrumentation package. |
Contributor
|
@Kludex any chance you can take a look? thanks |
herin049
reviewed
Jun 23, 2026
Kludex
reviewed
Jun 25, 2026
Comment on lines
+424
to
+437
| try: | ||
| import httpx | ||
| except ImportError: | ||
| import httpx2 as httpx | ||
|
|
||
| class _HTTPXModule(typing.Protocol): | ||
| Request: type[httpx.Request] | ||
| Response: type[httpx.Response] | ||
| Headers: type[httpx.Headers] | ||
| URL: type[httpx.URL] | ||
| BaseTransport: type[httpx.BaseTransport] | ||
| AsyncBaseTransport: type[httpx.AsyncBaseTransport] | ||
| HTTPTransport: type[httpx.HTTPTransport] | ||
| AsyncHTTPTransport: type[httpx.AsyncHTTPTransport] |
Member
There was a problem hiding this comment.
I don't think this solves the typing the way you intend, and that's why you have to cast everywhere.
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.
Description
Adds
httpx2support toopentelemetry-instrumentation-httpx.The main goal is to instrument
httpxandhttpx2independently, so users can have either or both libraries installed and enable the matching instrumentor:HTTPXClientInstrumentorforhttpxHTTPX2ClientInstrumentorforhttpx2This felt important because I was reading discussions like pydantic/httpx2#963, which suggest there's potentially some nervousness around staged migrations (i.e. if you're autoinstrumenting, and you have a dependency chain where only some libraries moved to httpx2, but others still use httpx)
The implementation keeps the shared instrumentation logic in a private base instrumentor while parameterizing the target module at runtime. Since
httpx2is API-compatible withhttpx, type annotations continue to usehttpx.*types.This also adds httpx2-specific transport wrapper classes and test environments, including a coexistence environment with both
httpxandhttpx2installed.Fixes #4635
Type of change
How Has This Been Tested?
Tested with the httpx/httpx2 package tox environments:
uv run tox -e py312-test-instrumentation-httpx-1-wrapt2 -- -rahttpxtest suite still passes.170 passed, 9 skippeduv run tox -e py312-test-instrumentation-httpx-2-wrapt2 -- -rahttpx2test suite.9 passeduv run tox -e py312-test-instrumentation-httpx-3-wrapt2 -- -rahttpxandhttpx2are installed.179 passedThe existing
httpxtests continue to userespx. The newhttpx2tests do not use the same strategy becauserespxdoes not yet have stablehttpx2support (was hoping lundberg/respx#317 would have been ready, but alas...) .Instead, the
httpx2tests use a localThreadingHTTPServerfor global instrumentation paths that need to exercise real transports, andhttpx2.MockTransportfor client-level and transport-wrapper coverage.Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.