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.

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:

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
)

API

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

When instrumenting httpx2 clients, use 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 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.

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:

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
)

Configuration

Exclude lists

To exclude certain URLs from tracking, set the environment variable OTEL_PYTHON_HTTPX_EXCLUDED_URLS (or OTEL_PYTHON_EXCLUDED_URLS to cover all instrumentations) to a string of comma delimited regexes that match the URLs.

For example,

export OTEL_PYTHON_HTTPX_EXCLUDED_URLS="client/.*/info,healthcheck"

will exclude requests such as https://site/client/123/info and https://site/xyz/healthcheck.

Capture HTTP request and response headers

You can configure the agent to capture specified HTTP headers as span attributes, according to the semantic conventions.

Request headers

To capture HTTP request headers as span attributes, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST to a comma delimited list of HTTP header names.

For example using the environment variable,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST="content-type,custom_request_header"

will extract content-type and custom_request_header from the request headers and add them as span attributes.

Request header names in HttpX are case-insensitive. So, giving the header name as CUStom-Header in the environment variable will capture the header named custom-header.

Regular expressions may also be used to match multiple headers that correspond to the given pattern. For example:

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST="Accept.*,X-.*"

Would match all request headers that start with Accept and X-.

To capture all request headers, set OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST to ".*".

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST=".*"

The name of the added span attribute will follow the format http.request.header.<header_name> where <header_name> is the normalized HTTP header name (lowercase, with - replaced by _). The value of the attribute will be a single item list containing all the header values.

For example: http.request.header.custom_request_header = ["<value1>", "<value2>"]

Response headers

To capture HTTP response headers as span attributes, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE to a comma delimited list of HTTP header names.

For example using the environment variable,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE="content-type,custom_response_header"

will extract content-type and custom_response_header from the response headers and add them as span attributes.

Response header names in HttpX are case-insensitive. So, giving the header name as CUStom-Header in the environment variable will capture the header named custom-header.

Regular expressions may also be used to match multiple headers that correspond to the given pattern. For example:

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE="Content.*,X-.*"

Would match all response headers that start with Content and X-.

To capture all response headers, set OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE to ".*".

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE=".*"

The name of the added span attribute will follow the format http.response.header.<header_name> where <header_name> is the normalized HTTP header name (lowercase, with - replaced by _). The value of the attribute will be a list containing the header values.

For example: http.response.header.custom_response_header = ["<value1>", "<value2>"]

Sanitizing headers

In order to prevent storing sensitive data such as personally identifiable information (PII), session keys, passwords, etc, set the environment variable OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS to a comma delimited list of HTTP header names to be sanitized.

Regexes may be used, and all header names will be matched in a case-insensitive manner.

For example using the environment variable,

export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS=".*session.*,set-cookie"

will replace the value of headers such as session-id and set-cookie with [REDACTED] in the span.

Note

The environment variable names used to capture HTTP headers are still experimental, and thus are subject to change.

API

class opentelemetry.instrumentation.httpx.RequestInfo(method, url, headers, stream, extensions)[source]

Bases: NamedTuple

method: bytes

Alias for field number 0

url: httpx.URL

Alias for field number 1

headers: httpx.Headers | None

Alias for field number 2

stream: httpx.SyncByteStream | httpx.AsyncByteStream | None

Alias for field number 3

extensions: dict[str, typing.Any] | None

Alias for field number 4

count(value, /)

Return number of occurrences of value.

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

class opentelemetry.instrumentation.httpx.ResponseInfo(status_code, headers, stream, extensions)[source]

Bases: NamedTuple

status_code: int

Alias for field number 0

headers: httpx.Headers | None

Alias for field number 1

stream: httpx.SyncByteStream | httpx.AsyncByteStream

Alias for field number 2

extensions: dict[str, typing.Any] | None

Alias for field number 3

count(value, /)

Return number of occurrences of value.

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

class opentelemetry.instrumentation.httpx.SyncOpenTelemetryTransport(transport, tracer_provider=None, meter_provider=None, request_hook=None, response_hook=None)[source]

Bases: _SyncOpenTelemetryTransportBase, BaseTransport

Sync transport class that traces requests made with httpx.

close()
Return type:

None

handle_request(*args, **kwargs)

Add request info to span.

Return type:

tuple[int, Headers, SyncByteStream, dict[str, Any]] | Response

class opentelemetry.instrumentation.httpx.AsyncOpenTelemetryTransport(transport, tracer_provider=None, meter_provider=None, request_hook=None, response_hook=None)[source]

Bases: _AsyncOpenTelemetryTransportBase, AsyncBaseTransport

Async transport class that traces requests made with httpx.

async aclose()
Return type:

None

async handle_async_request(*args, **kwargs)

Add request info to span.

Return type:

tuple[int, Headers, AsyncByteStream, dict[str, Any]] | Response

class opentelemetry.instrumentation.httpx.HTTPXClientInstrumentor(*args, **kwargs)[source]

Bases: _BaseHTTPXClientInstrumentor

An instrumentor for httpx Client and AsyncClient.

instrument(**kwargs)

Instrument the library

This method will be called without any optional arguments by the opentelemetry-instrument command.

This means that calling this method directly without passing any optional values should do the very same thing that the opentelemetry-instrument command does.

classmethod instrument_client(client, tracer_provider=None, meter_provider=None, request_hook=None, response_hook=None)

Instrument an httpx API-compatible Client or AsyncClient.

Parameters:
Return type:

None

instrumentation_dependencies()

Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or pyproject.toml.

For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection[str]

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.

property is_instrumented_by_opentelemetry
uninstrument(**kwargs)

Uninstrument the library

See BaseInstrumentor.instrument for more information regarding the usage of kwargs.

static uninstrument_client(client)

Disables instrumentation for the given client instance

Parameters:

client (Client | AsyncClient) – The Client or AsyncClient instance

Return type:

None

class opentelemetry.instrumentation.httpx.HTTPX2ClientInstrumentor(*args, **kwargs)[source]

Bases: _BaseHTTPXClientInstrumentor

An instrumentor for httpx2 Client and AsyncClient.

instrument(**kwargs)

Instrument the library

This method will be called without any optional arguments by the opentelemetry-instrument command.

This means that calling this method directly without passing any optional values should do the very same thing that the opentelemetry-instrument command does.

classmethod instrument_client(client, tracer_provider=None, meter_provider=None, request_hook=None, response_hook=None)

Instrument an httpx API-compatible Client or AsyncClient.

Parameters:
Return type:

None

instrumentation_dependencies()

Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or pyproject.toml.

For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection[str]

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.

property is_instrumented_by_opentelemetry
uninstrument(**kwargs)

Uninstrument the library

See BaseInstrumentor.instrument for more information regarding the usage of kwargs.

static uninstrument_client(client)

Disables instrumentation for the given client instance

Parameters:

client (Client | AsyncClient) – The Client or AsyncClient instance

Return type:

None