Skip to content

Feature/httpx2 instrumentor#4730

Open
gloshkajian wants to merge 12 commits into
open-telemetry:mainfrom
gloshkajian:feature/httpx2_instrumentor
Open

Feature/httpx2 instrumentor#4730
gloshkajian wants to merge 12 commits into
open-telemetry:mainfrom
gloshkajian:feature/httpx2_instrumentor

Conversation

@gloshkajian

@gloshkajian gloshkajian commented Jun 23, 2026

Copy link
Copy Markdown

Description

Adds httpx2 support to opentelemetry-instrumentation-httpx.

The main goal is to instrument httpx and httpx2 independently, so users can have either or both libraries installed and enable the matching instrumentor:

  • HTTPXClientInstrumentor for httpx
  • HTTPX2ClientInstrumentor for httpx2

This 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 httpx2 is API-compatible with httpx, type annotations continue to use httpx.* types.

This also adds httpx2-specific transport wrapper classes and test environments, including a coexistence environment with both httpx and httpx2 installed.

Fixes #4635

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Tested with the httpx/httpx2 package tox environments:

  • uv run tox -e py312-test-instrumentation-httpx-1-wrapt2 -- -ra

    • Verifies the existing httpx test suite still passes.
    • Result: 170 passed, 9 skipped
  • uv run tox -e py312-test-instrumentation-httpx-2-wrapt2 -- -ra

    • Verifies the dedicated httpx2 test suite.
    • Result: 9 passed
  • uv run tox -e py312-test-instrumentation-httpx-3-wrapt2 -- -ra

    • Verifies the combined environment where both httpx and httpx2 are installed.
    • Result: 179 passed

The existing httpx tests continue to use respx. The new httpx2 tests do not use the same strategy because respx does not yet have stable httpx2 support (was hoping lundberg/respx#317 would have been ready, but alas...) .

Instead, the httpx2 tests use a local ThreadingHTTPServer for global instrumentation paths that need to exercise real transports, and httpx2.MockTransport for client-level and transport-wrapper coverage.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@gloshkajian gloshkajian requested a review from a team as a code owner June 23, 2026 01:33
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 23, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 HTTPX2ClientInstrumentor and httpx2 transport wrapper classes alongside the existing httpx instrumentor/transport support.
  • Adds dedicated httpx2 and coexistence tox environments, CI jobs, and new integration tests for httpx2.
  • 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 httpx2opentelemetry-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.

@xrmx

xrmx commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@Kludex any chance you can take a look? thanks

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]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this solves the typing the way you intend, and that's why you have to cast everywhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

httpx2 instrumentor/httpx instrumentor support for httpx2

5 participants