Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

OpenTelemetry HTTPX Instrumentation

pypi

This library allows tracing HTTP requests made by the httpx and httpx2 libraries.

If both libraries are installed, use HTTPXClientInstrumentor for httpx clients and HTTPX2ClientInstrumentor for httpx2 clients. The instrumentors can be enabled independently.

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))

The same package also supports httpx2 using the HTTPX2ClientInstrumentor:

import httpx2
import asyncio
from opentelemetry.instrumentation.httpx import HTTPX2ClientInstrumentor

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

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

async def get(url):
    async with httpx2.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))

For httpx2 clients, use HTTPX2ClientInstrumentor.instrument_client:

import httpx2
from opentelemetry.instrumentation.httpx import HTTPX2ClientInstrumentor

with httpx2.Client() as client:
    HTTPX2ClientInstrumentor.instrument_client(client)
    response = client.get("https://example.com")

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))

For httpx2 transports, use SyncOpenTelemetryTransportHttpx2 and AsyncOpenTelemetryTransportHttpx2:

import httpx2
from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransportHttpx2

transport = httpx2.HTTPTransport()
telemetry_transport = SyncOpenTelemetryTransportHttpx2(transport)

with httpx2.Client(transport=telemetry_transport) as client:
    response = client.get("https://example.com")

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

Metadata

Release files for opentelemetry-instrumentation-httpx 0.66b2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opentelemetry-instrumentation-httpx 0.66b2
File Size Uploaded Provenance Actions
opentelemetry_instrumentation_httpx-0.66b2.tar.gz 26.6 kB Without provenance Details for opentelemetry_instrumentation_httpx-0.66b2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opentelemetry-instrumentation-httpx 0.66b2
File Interpreter ABI Platform Actions
opentelemetry_instrumentation_httpx-0.66b2-py3-none-any.whl Python 3 none any Details for opentelemetry_instrumentation_httpx-0.66b2-py3-none-any.whl

Total release size: 44.4 kB

Release files / opentelemetry_instrumentation_httpx-0.66b2.tar.gz

Download URL opentelemetry_instrumentation_httpx-0.66b2.tar.gz
Size 26.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b4c81e4a6dc7b8d7cb2d3088eb8848f6db6bd4abdc443ac9586ec176f1460187
BLAKE2b-256 checksum
How to use checksums
41433204ca7e7d91bd9e1b569a98a5f9252c98ae70ba3bb562d2770c34e21d49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Release files / opentelemetry_instrumentation_httpx-0.66b2-py3-none-any.whl

Download URL opentelemetry_instrumentation_httpx-0.66b2-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93e2b6ab1a505d76ec3d98abcf7e8527d4712167d6c3b5ebbb9c68d9309d9ba5
BLAKE2b-256 checksum
How to use checksums
534037d6161dcd95285c9c3eb75e2dbeb1dbc6e868551532735374918a22e298
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.22

Security for opentelemetry-instrumentation-httpx 0.66b2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page