OpenTelemetry for Flask
OpenTelemetry instruments Flask, SQLAlchemy, psycopg, requests, Redis and Celery when the app starts under opentelemetry-instrument. Each request becomes a trace named after its route, such as POST /orders/<order_id>/pay.
What you get
- Every request as a trace, with its database calls and outgoing HTTP calls, in Traces.
- Requests matched to your business flows by service and route on our side, so you see "checkout is failing at place order" rather than a list of endpoints.
- Exceptions grouped into Issues, slow and N+1 database queries on each database's page, and deploys found from
service.version. - Alerts, incidents and the on-call agent, which reads these traces when something breaks.
Set it up
1. Install
pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install # adds instrumentation for the libraries you use
2. Point it at Flowhawk
First create an ingest key: in the app, open Settings > API keys and pick Services. In the examples below, KEY stands for that key. Keep it out of your repository, like any other secret.
OTEL_SERVICE_NAME=orders
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.flowhawk.dev
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer%20KEY
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=production,service.version=1.4.2
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
The space in the header is written %20. Set service.version to your release, so a deploy shows as a change next to the flows it made better or worse.
3. Start the app under opentelemetry-instrument
opentelemetry-instrument gunicorn app:app -w 4 -b 0.0.0.0:8000
For flask run during development, add --no-reload: the reloader starts a new process that isn't instrumented.
Check it arrived
Open Setup in the app. It lists each service that sent data in the last 15 minutes. If yours isn't there, Help and troubleshooting says why telemetry can be refused (a wrong key, an empty balance) and how to see it.
Or use the Flowhawk SDK
The Flowhawk Python SDK (pip install flowhawk-sdk, Python 3.10 or later) loads the same instrumentation, also tags each request with its business flow and step in your service, sends logging records with their trace, and takes sampling settings from Flowhawk. Call it once, before Flask is imported:
import flowhawk_sdk
flowhawk_sdk.init(service="orders")
from flask import Flask
app = Flask(__name__)
It reads FLOWHAWK_API_KEY, FLOWHAWK_CONTROL_URL=https://api.flowhawk.dev, OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.flowhawk.dev and FLOWHAWK_ENVIRONMENT (Python guide).