Skip to content

Add HTTPX hook access to request and response objects#4564

Open
Harshal96 wants to merge 8 commits into
open-telemetry:mainfrom
Harshal96:feat/httpx-hook-body-access
Open

Add HTTPX hook access to request and response objects#4564
Harshal96 wants to merge 8 commits into
open-telemetry:mainfrom
Harshal96:feat/httpx-hook-body-access

Conversation

@Harshal96
Copy link
Copy Markdown

Description

Add access to the original httpx.Request and httpx.Response objects from HTTPX request and response hook info while preserving the existing tuple fields. This lets hooks read request content and call response.response.read() or response.response.aread() so HTTPX caches response bodies for later application use.

Fixes #2556

Type of change

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

How Has This Been Tested?

  • uv run --no-sync --with tox tox -e py312-test-instrumentation-httpx-1-wrapt2
  • uv run --no-sync --with tox tox -e py312-test-instrumentation-httpx-0-wrapt2
  • uv run --no-sync --with tox tox -e lint-instrumentation-httpx
  • uv 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.rst
  • uv 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.py
  • uv run --no-sync --with pre-commit pre-commit run rstcheck --files instrumentation/opentelemetry-instrumentation-httpx/README.rst

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

@Harshal96 Harshal96 marked this pull request as ready for review May 10, 2026 19:47
@Harshal96 Harshal96 requested a review from a team as a code owner May 10, 2026 19:47
@Harshal96 Harshal96 marked this pull request as draft May 10, 2026 19:49
@emdneto
Copy link
Copy Markdown
Member

emdneto commented May 12, 2026

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

Assisted-by: OpenAI Codex
@Harshal96 Harshal96 marked this pull request as ready for review May 14, 2026 02:10
@Harshal96
Copy link
Copy Markdown
Author

Done!

@emdneto emdneto moved this to Reviewed PRs that need fixes in Python PR digest May 22, 2026
@emdneto emdneto requested a review from Copilot May 22, 2026 16:58
Comment thread CHANGELOG.md
Copy link
Copy Markdown
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

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

Please fix conflicts

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 / ResponseInfo to optionally carry the original httpx.Request / httpx.Response objects 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.

Comment on lines +439 to +443
request: httpx.Request | None = None,
):
info = super().__new__(cls, method, url, headers, stream, extensions)
info.request = request
return info
Comment on lines +465 to +469
response: httpx.Response | None = None,
):
info = super().__new__(cls, status_code, headers, stream, extensions)
info.response = response
return info
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

there's a unit test already that negates this, so this comment incorrect

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

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

HTTPX instrumentation - document how to log request payload and response on response_hook

3 participants