Skip to main content

OpenTelemetry HTTPX Instrumentation

Project description

pypi

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


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.28b1.tar.gz.

File metadata

  • Download URL: opentelemetry-instrumentation-httpx-0.28b1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for opentelemetry-instrumentation-httpx-0.28b1.tar.gz
Algorithm Hash digest
SHA256 1f79578ce7cc6d47d3fb56e65066dac260d08fe51d92c985d42003bfc0600e3d
MD5 fd6f27633ddc98d73195194665e5a67a
BLAKE2b-256 97de06590d2ae7a79f19bff245c946d72f6eb3041d90d448cabe183e8cb67bcb

See more details on using hashes here.

File details

Details for the file opentelemetry_instrumentation_httpx-0.28b1-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_httpx-0.28b1-py3-none-any.whl
Algorithm Hash digest
SHA256 83102f6657ef4f9cd4a40924748457fc34b5c514c07e630b16b5a7508e018098
MD5 e5958c04b28ffa76f45a718162c4386a
BLAKE2b-256 808517658e11c830495047528404f3191f770020c64e647c43876408127b03b9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page