Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/4564.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`opentelemetry-instrumentation-httpx`: add request and response objects to hooks
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

### Added

- Add `BaggageLogProcessor` to `opentelemetry-processor-baggage`
([#4371](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4371))
- `opentelemetry-instrumentation-system-metrics`: Add support for `process.disk.io` metric in system-metrics instrumentation
Expand Down
33 changes: 32 additions & 1 deletion instrumentation/opentelemetry-instrumentation-httpx/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,38 @@ and right before the span is finished while processing a response.

.. note::

The request hook receives the raw arguments provided to the transport layer. The response hook receives the raw return values from the transport layer.
The request hook receives the raw arguments provided to the transport layer.
The response hook receives the raw return values from the transport layer.
For HTTPX versions that pass ``httpx.Request`` and ``httpx.Response``
objects to the transport layer, these original objects are available as
``request.request`` and ``response.response``. Older HTTPX transport APIs may
set these attributes to ``None``.

Request and response bodies can contain sensitive data. If you log or add them
to spans, make sure to redact and limit the captured content.

.. code-block:: python

import logging

logger = logging.getLogger(__name__)

def response_hook(span, request, response):
if request.request is not None:
logger.debug("HTTPX request body: %r", request.request.content)

if response.response is not None:
logger.debug("HTTPX response body: %r", response.response.read())

async def async_response_hook(span, request, response):
if request.request is not None:
logger.debug("HTTPX request body: %r", request.request.content)

if response.response is not None:
logger.debug(
"HTTPX response body: %r",
await response.response.aread(),
)

The hooks can be configured as follows:

Expand Down
Loading
Loading