Add HTTPX hook access to request and response objects#4564
Conversation
Assisted-by: OpenAI Codex
|
Thanks for the PR! Just a heads-up: we no longer update Please add the appropriate changelog fragment for this change instead of editing |
Assisted-by: OpenAI Codex
|
Done! |
Assisted-by: ChatGPT 5
There was a problem hiding this comment.
Pull request overview
This PR enhances the opentelemetry-instrumentation-httpx hook APIs by exposing the original httpx.Request / httpx.Response objects (when available) via request.request and response.response, while keeping the existing tuple-style fields for backward compatibility. This addresses the need to read/cache bodies in hooks (Issue #2556) and documents safe usage patterns.
Changes:
- Extend
RequestInfo/ResponseInfoto optionally carry the originalhttpx.Request/httpx.Responseobjects for hook consumers. - Update integration tests to exercise reading request/response content via the newly exposed objects with a fallback to legacy tuple fields.
- Update HTTPX instrumentation docs (module docs + README) with examples and a sensitivity warning; add changelog entry.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/init.py | Adds request.request / response.response plumbing and documents hook usage; introduces new RequestInfo/ResponseInfo implementations. |
| instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py | Updates hooks in tests to validate new request/response object access with backward-compatible fallbacks. |
| instrumentation/opentelemetry-instrumentation-httpx/README.rst | Documents how to access and read request/response bodies via hook info, with a warning about sensitive data. |
| CHANGELOG.md | Minor formatting adjustment in the Unreleased/Added section. |
| .changelog/4564.added | Adds a changelog fragment for the new HTTPX hook request/response object access feature. |
| request: httpx.Request | None = None, | ||
| ): | ||
| info = super().__new__(cls, method, url, headers, stream, extensions) | ||
| info.request = request | ||
| return info |
| response: httpx.Response | None = None, | ||
| ): | ||
| info = super().__new__(cls, status_code, headers, stream, extensions) | ||
| info.response = response | ||
| return info |
There was a problem hiding this comment.
there's a unit test already that negates this, so this comment incorrect
Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com>
…hal96/opentelemetry-python-contrib into feat/httpx-hook-body-access
|
@emdneto conflicts fixed and comments are handled, can you give it another review? |
|
@emdneto is this good to review? |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
|
@emdneto PR keeps getting stale, good for another review? |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
Description
Add access to the original
httpx.Requestandhttpx.Responseobjects from HTTPX request and response hook info while preserving the existing tuple fields. This lets hooks read request content and callresponse.response.read()orresponse.response.aread()so HTTPX caches response bodies for later application use.Fixes #2556
Type of change
How Has This Been Tested?
uv run --no-sync --with tox tox -e py312-test-instrumentation-httpx-1-wrapt2uv run --no-sync --with tox tox -e py312-test-instrumentation-httpx-0-wrapt2uv run --no-sync --with tox tox -e lint-instrumentation-httpxuv run --no-sync --with pre-commit pre-commit run ruff --files instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.py instrumentation/opentelemetry-instrumentation-httpx/README.rstuv run --no-sync --with pre-commit pre-commit run ruff-format --files instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py instrumentation/opentelemetry-instrumentation-httpx/tests/test_httpx_integration.pyuv run --no-sync --with pre-commit pre-commit run rstcheck --files instrumentation/opentelemetry-instrumentation-httpx/README.rstDoes This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.