Skip to content

Flask OpenTelemetry instrumentation

Instrument a Flask application with OpenTelemetry using the opentelemetry-instrument wrapper. Requests, view functions, database queries, and outbound HTTP calls are traced with no changes to your application code.

  • A Flask app, typically served by Gunicorn or uWSGI in production.
  • Python 3.8 or newer.
  • Your Osuite ingest token and region.
  1. Install the distro and OTLP exporter, then let bootstrap add the Flask instrumentor.

    Terminal window
    pip install opentelemetry-distro opentelemetry-exporter-otlp
    opentelemetry-bootstrap -a install
  2. Set the exporter environment variables.

    Terminal window
    export OTEL_SERVICE_NAME="<service-name>"
    export OTEL_RESOURCE_ATTRIBUTES="service.environment=production"
    export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.<region>.osuite.io:443"
    export OTEL_EXPORTER_OTLP_HEADERS="x-osuite-ingest-token=<your-ingest-token>"
    export OTEL_LOGS_EXPORTER="otlp"
    export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED="true"

These examples assume the app object is app in app.py.

For local development, run the built-in server with the reloader disabled:

Terminal window
opentelemetry-instrument flask --app app run --no-reload

In production, Gunicorn is a single instrumented process only with one worker. With multiple workers, use the post_fork hook described in the gotchas:

Terminal window
opentelemetry-instrument gunicorn app:app --bind 0.0.0.0:8000

What you should see

Send a request to any route. The service appears in APM within a minute with a trace per request, showing the view function, database queries, and outbound calls as nested spans. Application logs appear in the Logs explorer with the matching trace ID.

No data in Osuite after a few minutes? Work through these checks.

  • Endpoint — confirm the exporter targets exactly ingest.<region>.osuite.io:443 for your region, over TLS.
  • Token — confirm the ingest header carries a valid <your-ingest-token> and has not been rotated.
  • Pipeline — confirm the signal you expect (traces, logs, or metrics) is wired into an active pipeline with the otlp/osuite exporter attached.
  • Export errors — check the application or Collector logs for OTLP export failures (auth, DNS, TLS, connection refused).
  • Network — confirm the host has outbound access to the ingest endpoint on port 443.
  • Timing — allow up to a minute for the first data to appear before assuming a failure.

Still stuck? Ask the Investigation Agent or contact support.