Skip to main content

OpenTelemetry HTTPX Instrumentation

Project description

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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opentelemetry_instrumentation_httpx-0.61b0.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file opentelemetry_instrumentation_httpx-0.61b0.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_httpx-0.61b0.tar.gz
Algorithm Hash digest
SHA256 6569ec097946c5551c2a4252f74c98666addd1bf047c1dde6b4ef426719ff8dd
MD5 45bfc5e018ab86e9442013ae505ca0cc
BLAKE2b-256 cd2ae2becd55e33c29d1d9ef76e2579040ed1951cb33bacba259f6aff2fdd2a6

See more details on using hashes here.

File details

Details for the file opentelemetry_instrumentation_httpx-0.61b0-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_httpx-0.61b0-py3-none-any.whl
Algorithm Hash digest
SHA256 dee05c93a6593a5dc3ae5d9d5c01df8b4e2c5d02e49275e5558534ee46343d5e
MD5 a6b6004d586824c524b16de05525f109
BLAKE2b-256 af88dde310dce56e2d85cf1a09507f5888544955309edc4b8d22971d6d3d1417

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page