Skip to content

Requests

See every HTTP request your app makes with requests: the URL, the response status, how long it took, and any errors, as a span (one unit of work 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 right next to the code that triggered it.

What you’ll capture

  • Each request as a span, with its URL, method, response status, and duration
  • Any errors that occurred during the request

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 requests extra:

Terminal
pip install 'logfire[requests]'

Usage

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

main.py
import requests

import logfire

logfire.configure()
logfire.instrument_requests()

requests.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 requests in Logfire? Check these first:

  • logfire.configure() runs before logfire.instrument_requests(). Configure the connection first, then instrument.
  • You call instrument_requests() exactly once.
  • Your write token is set. In local development, run logfire projects use <your-project>; in production, set the LOGFIRE_TOKEN environment variable. See Getting Started.
  • You actually made a request. Spans appear only after a request completes.

Advanced

Passing options to the OpenTelemetry instrumentor

logfire.instrument_requests() accepts additional keyword arguments and passes them to the OpenTelemetry Requests instrumentation. See their documentation for the full list.

Reference

Excluding URLs from instrumentation