opentelemetry-instrumentation-httpx 0.61b0


pip install opentelemetry-instrumentation-httpx

  Latest version

Released: Mar 04, 2026


Meta
Author: OpenTelemetry Authors
Requires Python: >=3.9

Classifiers

Development Status
  • 4 - Beta

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Programming Language
  • Python
  • Python :: 3
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: 3.14

pypi

This library allows tracing HTTP requests made by the httpx library.

Installation

pip install opentelemetry-instrumentation-httpx

Usage

Instrumenting all clients

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

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.

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.

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.

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.

The hooks can be configured as follows:

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:

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

0.61b0 Mar 04, 2026
0.60b1 Dec 11, 2025
0.60b0 Dec 03, 2025
0.59b0 Oct 16, 2025
0.58b0 Sep 11, 2025
0.57b0 Jul 29, 2025
0.56b0 Jul 11, 2025
0.55b1 Jun 10, 2025
0.55b0 Jun 04, 2025
0.54b1 May 16, 2025
0.54b0 May 09, 2025
0.53b1 Apr 15, 2025
0.53b0 Apr 10, 2025
0.52b1 Mar 20, 2025
0.52b0 Mar 12, 2025
0.51b0 Feb 04, 2025
0.50b0 Dec 11, 2024
0.49b2 Nov 18, 2024
0.49b1 Nov 08, 2024
0.49b0 Nov 05, 2024
0.48b0 Aug 28, 2024
0.47b0 Jul 25, 2024
0.46b0 May 31, 2024
0.45b0 Mar 28, 2024
0.44b0 Feb 23, 2024
0.43b0 Dec 19, 2023
0.42b0 Nov 08, 2023
0.41b0 Sep 11, 2023
0.40b0 Jul 13, 2023
0.39b0 May 19, 2023
0.38b0 Mar 22, 2023
0.37b0 Feb 17, 2023
0.36b0 Dec 09, 2022
0.35b0 Nov 03, 2022
0.34b0 Sep 27, 2022
0.33b0 Aug 09, 2022
0.32b0 Jul 04, 2022
0.31b0 May 17, 2022
0.30b1 Apr 21, 2022
0.30b0 Apr 18, 2022
0.29b0 Mar 11, 2022
0.28b1 Jan 31, 2022
0.28b0 Jan 26, 2022
0.27b0 Dec 17, 2021
0.26b1 Nov 11, 2021
0.25b2 Oct 19, 2021
0.25b1 Oct 18, 2021
0.25b0 Oct 13, 2021
0.24b0 Aug 26, 2021
0.23b2 Jul 28, 2021
0.23b0 Jul 21, 2021
Extras:
Dependencies:
opentelemetry-api (~=1.12)
opentelemetry-instrumentation (==0.61b0)
opentelemetry-semantic-conventions (==0.61b0)
opentelemetry-util-http (==0.61b0)
wrapt (<2.0.0,>=1.0.0)