opentelemetry-instrumentation-httpx

OpenTelemetry HTTPX Instrumentation

55 个版本 Python >=3.10
安装
pip install opentelemetry-instrumentation-httpx
poetry add opentelemetry-instrumentation-httpx
pipenv install opentelemetry-instrumentation-httpx
conda install opentelemetry-instrumentation-httpx
描述

OpenTelemetry HTTPX Instrumentation

|pypi|

.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-httpx.svg :target: https://pypi.org/project/opentelemetry-instrumentation-httpx/

This library allows tracing HTTP requests made by the httpx <https://www.python-httpx.org/>_ library.

Installation

::

 pip install opentelemetry-instrumentation-httpx

Usage

Instrumenting all clients


When using the instrumentor, all clients will automatically trace requests.

.. code-block:: python

import httpx
import asyncio
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://example.com"
HTTPXClientInstrumentor().instrument()

with httpx.Client() as client:
    response = client.get(url)

async def get(url):
    async with httpx.AsyncClient() as client:
        response = await client.get(url)

asyncio.run(get(url))

Instrumenting single clients


If you only want to instrument requests for specific client instances, you can use the HTTPXClientInstrumentor.instrument_client method.

.. code-block:: python

import httpx
import asyncio
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://example.com"

with httpx.Client() as client:
    HTTPXClientInstrumentor.instrument_client(client)
    response = client.get(url)

async def get(url):
    async with httpx.AsyncClient() as client:
        HTTPXClientInstrumentor.instrument_client(client)
        response = await client.get(url)

asyncio.run(get(url))

Uninstrument


If you need to uninstrument clients, there are two options available.

.. code-block:: python

import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

HTTPXClientInstrumentor().instrument()
client = httpx.Client()

# Uninstrument a specific client
HTTPXClientInstrumentor.uninstrument_client(client)

# Uninstrument all clients
HTTPXClientInstrumentor().uninstrument()

Using transports directly


If you don't want to use the instrumentor class, you can use the transport classes directly.

.. code-block:: python

import httpx
import asyncio
from opentelemetry.instrumentation.httpx import (
    AsyncOpenTelemetryTransport,
    SyncOpenTelemetryTransport,
)

url = "https://example.com"
transport = httpx.HTTPTransport()
telemetry_transport = SyncOpenTelemetryTransport(transport)

with httpx.Client(transport=telemetry_transport) as client:
    response = client.get(url)

transport = httpx.AsyncHTTPTransport()
telemetry_transport = AsyncOpenTelemetryTransport(transport)

async def get(url):
    async with httpx.AsyncClient(transport=telemetry_transport) as client:
        response = await client.get(url)

asyncio.run(get(url))

Request and response hooks


The instrumentation supports specifying request and response hooks. These are functions that get called back by the instrumentation right after a span is created for a request 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 hooks can be configured as follows:

.. code-block:: python

from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

def request_hook(span, request):
    # method, url, headers, stream, extensions = request
    pass

def response_hook(span, request, response):
    # method, url, headers, stream, extensions = request
    # status_code, headers, stream, extensions = response
    pass

async def async_request_hook(span, request):
    # method, url, headers, stream, extensions = request
    pass

async def async_response_hook(span, request, response):
    # method, url, headers, stream, extensions = request
    # status_code, headers, stream, extensions = response
    pass

HTTPXClientInstrumentor().instrument(
    request_hook=request_hook,
    response_hook=response_hook,
    async_request_hook=async_request_hook,
    async_response_hook=async_response_hook
)

Or if you are using the transport classes directly:

.. code-block:: python

import httpx
from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport

def request_hook(span, request):
    # method, url, headers, stream, extensions = request
    pass

def response_hook(span, request, response):
    # method, url, headers, stream, extensions = request
    # status_code, headers, stream, extensions = response
    pass

async def async_request_hook(span, request):
    # method, url, headers, stream, extensions = request
    pass

async def async_response_hook(span, request, response):
    # method, url, headers, stream, extensions = request
    # status_code, headers, stream, extensions = response
    pass

transport = httpx.HTTPTransport()
telemetry_transport = SyncOpenTelemetryTransport(
    transport,
    request_hook=request_hook,
    response_hook=response_hook
)

async_transport = httpx.AsyncHTTPTransport()
async_telemetry_transport = AsyncOpenTelemetryTransport(
    async_transport,
    request_hook=async_request_hook,
    response_hook=async_response_hook
)

References

  • OpenTelemetry HTTPX Instrumentation <https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/httpx/httpx.html>_
  • OpenTelemetry Project <https://opentelemetry.io/>_
版本列表
0.63b1 2026-05-21
0.63b0 2026-05-19
0.62b1 2026-04-24
0.62b0 2026-04-09
0.61b0 2026-03-04
0.60b1 2025-12-11
0.60b0 2025-12-03
0.59b0 2025-10-16
0.58b0 2025-09-11
0.57b0 2025-07-29
0.56b0 2025-07-11
0.55b1 2025-06-10
0.55b0 2025-06-04
0.54b1 2025-05-16
0.54b0 2025-05-09
0.53b1 2025-04-15
0.53b0 2025-04-10
0.52b1 2025-03-20
0.52b0 2025-03-12
0.51b0 2025-02-04
0.50b0 2024-12-11
0.49b2 2024-11-18
0.49b1 2024-11-08
0.49b0 2024-11-05
0.48b0 2024-08-28
0.47b0 2024-07-25
0.46b0 2024-05-31
0.45b0 2024-03-28
0.44b0 2024-02-23
0.43b0 2023-12-19
0.42b0 2023-11-08
0.41b0 2023-09-11
0.40b0 2023-07-13
0.39b0 2023-05-19
0.38b0 2023-03-22
0.37b0 2023-02-17
0.36b0 2022-12-09
0.35b0 2022-11-03
0.34b0 2022-09-27
0.33b0 2022-08-09
0.32b0 2022-07-04
0.31b0 2022-05-17
0.30b1 2022-04-21
0.30b0 2022-04-18
0.29b0 2022-03-11
0.28b1 2022-01-31
0.28b0 2022-01-26
0.27b0 2021-12-17
0.26b1 2021-11-11
0.25b2 2021-10-19
0.25b1 2021-10-18
0.25b0 2021-10-13
0.24b0 2021-08-26
0.23b2 2021-07-28
0.23b0 2021-07-21