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.
- 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.
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.
Install Logfire with the urllib3 extra.
pip install 'logfire[urllib3]'
uv add 'logfire[urllib3]'
Add two lines to your app: logfire.configure() to connect to your project, and
logfire.instrument_urllib3() to record every request.
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.
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.
Not seeing your urllib3 requests in Logfire? Check these first:
logfire.configure()runs beforelogfire.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 authif needed, followed byuvx logfire-cli init usefrom your application’s directory; in production, set theLOGFIRE_TOKENenvironment variable. See Getting Started. - You actually made a request. Spans appear only after a request completes.
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.
Header capture is off by default. Select the headers to record with these options:
captured_request_headersselects request headers to record, for example['content-type']. When omitted, reads theOTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_REQUESTenvironment variable.captured_response_headersselects response headers to record. When omitted, reads theOTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_CLIENT_RESPONSEenvironment variable.sensitive_headersselects captured header names whose values OpenTelemetry replaces with[REDACTED]. When omitted, reads theOTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDSenvironment 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.
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.
For streamed responses, the span ends when urllib3 returns the response object. Errors while reading the body later are outside that span.
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.
- API reference:
logfire.instrument_urllib3() - Underlying OpenTelemetry package: urllib3 instrumentation