OpenTelemetry HTTPX Instrumentation
Project description
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
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
Built Distribution
Hashes for opentelemetry_instrumentation_httpx-0.34b0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5961ff28a7cbc4bde49c406b75ae15f0f5c5c8ba1fdb33163a9cabbc2111d8a1 |
|
MD5 | 1788e564ffc68a5080ccf4b76266cfeb |
|
BLAKE2b-256 | 6097265b26e5f14d7bb0235c91720b4ff4da2cbe2e48eccdd8b3b2253bdad09b |
Hashes for opentelemetry_instrumentation_httpx-0.34b0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcc8a717775c997a287a058e07cb64556546e8b0fdb84d5063cba7027e80f38f |
|
MD5 | 89c3f95f905d0adbfeddc7b61af30808 |
|
BLAKE2b-256 | dda9cc367937c5fa277cb9412a045c7227798f743e3e6aeeff4a31ba80517cbc |