Skip to content

Flask

The logfire.instrument_flask() method will create a span for every request to your Flask application.

Install

Install logfire with the flask extra:

pip install 'logfire[flask]'
uv add 'logfire[flask]'
poetry add 'logfire[flask]'

Usage

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

main.py
import logfire
from flask import Flask


logfire.configure()

app = Flask(__name__)
logfire.instrument_flask(app)


@app.route("/")
def hello():
    return "Hello!"


if __name__ == "__main__":
    app.run(debug=True)

The keyword arguments of logfire.instrument_flask() are passed to the FlaskInstrumentor().instrument_app() method of the OpenTelemetry Flask Instrumentation package, read more about it here.

In case you are using Gunicorn to run your Flask application, you can configure Logfire in Gunicorn as well.

Excluding URLs from instrumentation

Capturing request and response headers