Instrumenting Flask with OpenTelemetry

Prerequisites

  • Python 3.6+

Instrumenting Flask app

git clone https://github.com/uptrace/uptrace-python.git
cd example/flask
  • Step 2. Install the required dependencies:
pip install -r requirements.txt
  • Step 3. Edit main.py to configure OpenTelemetry to export data to Uptrace:
import uptrace

uptrace.configure_opentelemetry(
    # Copy DSN here or use UPTRACE_DSN env var.
    dsn="",
    service_name="myservice",
    service_version="1.0.0",
)
  • Step 4. Edit main.py to instrument the Flask app:
from opentelemetry.instrumentation.flask import FlaskInstrumentor

app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
UPTRACE_DSN="https://<token>@api.uptrace.dev/<project_id>" ./main.py

What is next?

By now, you should have a Flask app with instrumented views. Next, instrumentopen in new window more operations (for example, database queries) or learn about OpenTelemetry API to create your own instrumentations.