Skip to main content
Pre-release

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

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
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://httpbin.org/get"
HTTPXClientInstrumentor().instrument()

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

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

Instrumenting single clients

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

import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor

url = "https://httpbin.org/get"

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

async with httpx.AsyncClient(transport=telemetry_transport) as client:
    HTTPXClientInstrumentor.instrument_client(client)
    response = await client.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
from opentelemetry.instrumentation.httpx import (
    AsyncOpenTelemetryTransport,
    SyncOpenTelemetryTransport,
)

url = "https://httpbin.org/get"
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 with httpx.AsyncClient(transport=telemetry_transport) as client:
    response = await client.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

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

Or if you are using the transport classes directly:

from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport

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

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

References

Release files for opentelemetry-instrumentation-httpx 0.24b0

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.24b0
File Size Uploaded
opentelemetry-instrumentation-httpx-0.24b0.tar.gz 14.0 kB Details

Built distribution (wheel)

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

Total release size: 25.5 kB

Release files / opentelemetry-instrumentation-httpx-0.24b0.tar.gz

Download URL opentelemetry-instrumentation-httpx-0.24b0.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0a0af6ca7717cee7a1b30fc4311652de4a9acfe3831bd99df574fd8bf2fdd339
BLAKE2b-256 checksum
How to use checksums
49d45d26652bfd630d8aa5a764c0baa71503d3c3eac0086d4fc0bd1c62599e86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.7.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.11

Release files / opentelemetry_instrumentation_httpx-0.24b0-py3-none-any.whl

Download URL opentelemetry_instrumentation_httpx-0.24b0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa762f3d3a11f6eab919109daeac2835b13b55a6198ef1db963c9b0455fbfb00
BLAKE2b-256 checksum
How to use checksums
14b0ba6ab2a2716a9d63158a45ecfb02e36044f5e90c5cc0753228e9fddae32e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.7.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.11

Release history Release notifications | RSS feed

This release

0.24b0 This release

2 release files

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