Skip to content

HTTPX

The logfire.instrument_httpx() method can be used to instrument HTTPX with Logfire.

Installation

Install logfire with the httpx extra:

pip install 'logfire[httpx]'
uv add 'logfire[httpx]'
rye add logfire -E httpx
poetry add 'logfire[httpx]'

Usage

Let's see a minimal example below. You can run it with python main.py:

main.py
import asyncio

import httpx
import logfire

logfire.configure()
logfire.instrument_httpx()

url = "https://httpbin.org/get"

with httpx.Client() as client:
    client.get(url)


async def main():
    async with httpx.AsyncClient() as client:
        await client.get(url)


asyncio.run(main())
main.py
import asyncio

import httpx
import logfire

logfire.configure()

url = 'https://httpbin.org/get'

with httpx.Client() as client:
    logfire.instrument_httpx(client)
    client.get(url)


async def main():
    async with httpx.AsyncClient() as client:
        logfire.instrument_httpx(client)
        await client.get(url)


asyncio.run(main())

logfire.instrument_httpx() uses the OpenTelemetry HTTPX Instrumentation package, which you can find more information about here.

Fine Tuning

The logfire.instrument_httpx() method accepts different parameters to fine-tune the instrumentation.

Capture HTTP Headers

By default, Logfire doesn't capture HTTP headers. You can enable it by setting the capture_headers parameter to True.

import httpx
import logfire

logfire.configure()
logfire.instrument_httpx(capture_headers=True)

client = httpx.Client()
client.get("https://httpbin.org/get")

Capture Only Request Headers

Instead of capturing both request and response headers, you can create a request hook to capture only the request headers:

import httpx
import logfire
from logfire.integrations.httpx import RequestInfo
from opentelemetry.trace import Span


def capture_request_headers(span: Span, request: RequestInfo):
    headers = request.headers
    span.set_attributes(
        {
            f'http.request.header.{header_name}': headers.get_list(header_name)
            for header_name in headers.keys()
        }
    )


logfire.configure()
logfire.instrument_httpx(request_hook=capture_request_headers)

client = httpx.Client()
client.get("https://httpbin.org/get")

Capture Only Response Headers

Similarly, you can create a response hook to capture only the response headers:

import httpx
import logfire
from opentelemetry.trace import Span
from logfire.integrations.httpx import RequestInfo, ResponseInfo


def capture_response_headers(span: Span, request: RequestInfo, response: ResponseInfo):
    headers = response.headers
    span.set_attributes(
        {f'http.response.header.{header_name}': headers.get_list(header_name)
        for header_name in headers.keys()}
    )


logfire.configure()
logfire.instrument_httpx(response_hook=capture_response_headers)

client = httpx.Client()
client.get('https://httpbin.org/get')

You can also use the hooks to filter headers or modify them before capturing them.