Package: opentelemetry-instrumentation-httpx
This library allows tracing HTTP requests made by thehttpxlibrary.Installationpip install opentelemetry-instrumentation-httpxUsageInstrumenting all clientsWhen using the instrumentor, all clients will automatically trace requests.importhttpxfromopentelemetry.instrumentation.httpximportHTTPXClientInstrumentorurl="https://some.url/get"HTTPXClientInstrumentor().instrument()withhttpx.Client()asclient:response=client.get(url)asyncwithhttpx.AsyncClient()asclient:response=awaitclient.get(url)Instrumenting single clientsIf you only want to instrument requests for specific client instances, you can
use theinstrument_clientmethod.importhttpxfromopentelemetry.instrumentation.httpximportHTTPXClientInstrumentorurl="https://some.url/get"withhttpx.Client(transport=telemetry_transport)asclient:HTTPXClientInstrumentor.instrument_client(client)response=client.get(url)asyncwithhttpx.AsyncClient(transport=telemetry_transport)asclient:HTTPXClientInstrumentor.instrument_client(client)response=awaitclient.get(url)UninstrumentIf you need to uninstrument clients, there are two options available.importhttpxfromopentelemetry.instrumentation.httpximportHTTPXClientInstrumentorHTTPXClientInstrumentor().instrument()client=httpx.Client()# Uninstrument a specific clientHTTPXClientInstrumentor.uninstrument_client(client)# Uninstrument all clientsHTTPXClientInstrumentor().uninstrument()Using transports directlyIf you don’t want to use the instrumentor class, you can use the transport classes directly.importhttpxfromopentelemetry.instrumentation.httpximport(AsyncOpenTelemetryTransport,SyncOpenTelemetryTransport,)url="https://some.url/get"transport=httpx.HTTPTransport()telemetry_transport=SyncOpenTelemetryTransport(transport)withhttpx.Client(transport=telemetry_transport)asclient:response=client.get(url)transport=httpx.AsyncHTTPTransport()telemetry_transport=AsyncOpenTelemetryTransport(transport)asyncwithhttpx.AsyncClient(transport=telemetry_transport)asclient:response=awaitclient.get(url)Request and response hooksThe 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.NoteThe 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:fromopentelemetry.instrumentation.httpximportHTTPXClientInstrumentordefrequest_hook(span,request):# method, url, headers, stream, extensions = requestpassdefresponse_hook(span,request,response):# method, url, headers, stream, extensions = request# status_code, headers, stream, extensions = responsepassasyncdefasync_request_hook(span,request):# method, url, headers, stream, extensions = requestpassasyncdefasync_response_hook(span,request,response):# method, url, headers, stream, extensions = request# status_code, headers, stream, extensions = responsepassHTTPXClientInstrumentor().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:fromopentelemetry.instrumentation.httpximportSyncOpenTelemetryTransport,AsyncOpenTelemetryTransportdefrequest_hook(span,request):# method, url, headers, stream, extensions = requestpassdefresponse_hook(span,request,response):# method, url, headers, stream, extensions = request# status_code, headers, stream, extensions = responsepassasyncdefasync_request_hook(span,request):# method, url, headers, stream, extensions = requestpassasyncdefasync_response_hook(span,request,response):# method, url, headers, stream, extensions = request# status_code, headers, stream, extensions = responsepasstransport=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)ReferencesOpenTelemetry HTTPX InstrumentationOpenTelemetry Project