Flask
See every request your Flask app handles (the route, how long it took, the response status, and any errors) as a trace (the full journey of one request, made of nested spans, where each span is one unit of work with a name, a start, and a duration) in Logfire.
- Each request as a span, with its HTTP status and duration
- The matched route and method
- Any errors raised while handling the request
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 flask extra:
pip install 'logfire[flask]'
uv add 'logfire[flask]'
Add two lines to your app: logfire.configure() to connect to your project, and
logfire.instrument_flask() to record every request.
from flask import Flask
import logfire
logfire.configure()
app = Flask(__name__)
logfire.instrument_flask(app)
@app.route('/')
def hello():
return 'Hello!'
if __name__ == '__main__':
app.run(debug=True)
Run it with python main.py.
With the app running, open http://localhost:5000/ in your browser.
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 request. Click it to see its duration, the matched route, and the response status.
Not seeing your requests in Logfire? Check these first:
logfire.configure()runs beforelogfire.instrument_flask(). Configure the connection first, then instrument the app.- You call
instrument_flask(app)exactly once, on the sameappobject you serve. - Your write token is set. In local development, run
logfire projects use <your-project>; in production, set theLOGFIRE_TOKENenvironment variable. See Getting Started. - You actually sent a request. Spans appear only after a route is hit; reload the URL above.
logfire.instrument_flask() accepts additional keyword arguments
and passes them to the OpenTelemetry FlaskInstrumentor().instrument_app() method. See
their documentation for the full list.
If you run your Flask application with Gunicorn, you can also configure Logfire in Gunicorn.
- API reference:
logfire.instrument_flask() - Underlying OpenTelemetry package: Flask instrumentation