Skip to content

Instrument urllib3: see outgoing HTTP requests

See outgoing HTTP requests made with urllib3: the URL, the response status, how long it took, and any errors, as a span (one unit of work: a single operation, with a name, a start, and a duration) in Logfire. Related spans link together into a trace (the full journey of one request), so a slow outgoing call shows up next to the code that triggered it.

What you’ll capture

  • Each request as a span, with its URL, method, response status, and duration
  • Errors that occur before urllib3 returns a response
  • Metrics for request duration and body sizes

Logfire does not capture request or response bodies.

Before you start

You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token to copy) and, for production or other languages, creates a write token (the credential your app uses to send data). New to Logfire? Start with Getting Started.

Installation

Install Logfire with the urllib3 extra.

Terminal
pip install 'logfire[urllib3]'

Usage

Add two lines to your app: logfire.configure() to connect to your project, and logfire.instrument_urllib3() to record every request.

main.py
import urllib3

import logfire

logfire.configure()
logfire.instrument_urllib3()

http = urllib3.PoolManager()
http.request('GET', 'https://httpbin.org/get')

Run it with python main.py.

Verify it worked

Run your program, then open your project in the Logfire web app and go to the Live view. Within a few seconds you should see a span for the GET request. Click it to see the URL, response status, and how long it took.

Troubleshooting

Not seeing your urllib3 requests in Logfire? Check these first:

  • logfire.configure() runs before logfire.instrument_urllib3(). Configure the connection first, then instrument.
  • Check whether urllib3 is already instrumented. Installation applies across the process, including to pools created before installation. If OpenTelemetry already instrumented urllib3, later calls keep the first installation’s settings.
  • Your write token is set. In local development, install uv if needed, then run uvx logfire-cli auth if needed, followed by uvx logfire-cli init use from your application’s directory; in production, set the LOGFIRE_TOKEN environment variable. See Getting Started.
  • You actually made a request. Spans appear only after a request completes.

Advanced

Capture controls

Filter recorded URLs

OpenTelemetry records URLs by default, including embedded credentials and query parameters. To redact embedded credentials and recognized sensitive query parameters, pass its URL helper:

from opentelemetry.util.http import redact_url

import logfire

logfire.instrument_urllib3(url_filter=redact_url)

To remove only embedded credentials, use remove_url_credentials from the same module. Use a custom url_filter for sensitive parameters that OpenTelemetry does not recognize.

If a filter raises an exception, Logfire logs the error and records the unfiltered URL. The HTTP request continues.

Capture headers

Header capture is off by default. Select the headers to record with these options:

  • captured_request_headers selects request headers to record, for example ['content-type']. When omitted, reads the OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUEST environment variable.
  • captured_response_headers selects response headers to record. When omitted, reads the OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSE environment variable.
  • sensitive_headers selects captured header names whose values OpenTelemetry replaces with [REDACTED]. When omitted, reads the OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS environment variable.

All three options accept case-insensitive regular expressions for header names. For example, ['x-.*'] matches names starting with x-. The environment variables accept comma-separated patterns.

Pass [] to either capture option to disable capture even when an environment variable enables it. Pass sensitive_headers=[] to disable OpenTelemetry’s header redaction even when the environment variable enables it.

Prefer Logfire’s scrubbing settings to redact captured headers consistently with other telemetry. Use sensitive_headers when you need OpenTelemetry to redact specific captured headers before they reach Logfire.

Configure exclusions and hooks

The integration passes additional keyword arguments to OpenTelemetry. See OpenTelemetry’s configuration reference for exclusions, hooks, and environment settings.

If a request or response hook raises an exception, Logfire logs it without interrupting the HTTP request. An error from the request itself still reaches your application.

Streaming responses

For streamed responses, the span ends when urllib3 returns the response object. Errors while reading the body later are outside that span.

Using Requests as well

Requests uses urllib3 internally. If you enable both this integration and logfire.instrument_requests(), OpenTelemetry records a Requests span for a Requests call and suppresses the inner urllib3 span. Calls made directly through urllib3 still produce urllib3 spans.

Reference

Excluding URLs from instrumentation