OpenTelemetry HTTPX Instrumentation
Project description
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opentelemetry_instrumentation_httpx-0.65b0.tar.gz.
File metadata
- Download URL: opentelemetry_instrumentation_httpx-0.65b0.tar.gz
- Upload date:
- Size: 26.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4627aa9c6bb99bf4462c8b565b0ef6aeb9ffad95c6c92868be1ef7895de112ee
|
|
| MD5 |
dd43c633c544b67e720c86f27b23a752
|
|
| BLAKE2b-256 |
6103a529140241addd4d0acc73bafbd6f74691651b92fc0ae9b4513cf80f07fa
|
File details
Details for the file opentelemetry_instrumentation_httpx-0.65b0-py3-none-any.whl.
File metadata
- Download URL: opentelemetry_instrumentation_httpx-0.65b0-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
400f1b78afa4ee2332b5debe58e1ed1b317913d58812c952576be76660aeadb1
|
|
| MD5 |
bfdc76dd2ae2987a034700e15a35ce4b
|
|
| BLAKE2b-256 |
9d0fc6144096b4914bbf44b43ba21c962e8f333ff045770b50a3e79ed8bd455f
|