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
File details
Details for the file opentelemetry-instrumentation-httpx-0.31b0.tar.gz
.
File metadata
- Download URL: opentelemetry-instrumentation-httpx-0.31b0.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 730ef636f6974afcee3bcae2b45e776d980647a4fe6b5c3e85b45dbc2bb371a6 |
|
MD5 | 352d5d1f56684ae873ccc23dba9fa458 |
|
BLAKE2b-256 | c45026fca00805cadf2931657bda9ab7f6f0cf48357820b53693bb26f6228143 |
File details
Details for the file opentelemetry_instrumentation_httpx-0.31b0-py3-none-any.whl
.
File metadata
- Download URL: opentelemetry_instrumentation_httpx-0.31b0-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.7.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a47918a275796e8be72860875bab9f16aef1c87cd1cdf4b93d1a4cb817607a8 |
|
MD5 | adad893c129b344259fa43941d6966f4 |
|
BLAKE2b-256 | 4b35a101971eef95826f21645b1de16f261a55a4a48ec7314e866cb48d06d607 |