Appearance
Python
Automatic traces, metrics and logs for Django, Flask, FastAPI, requests, httpx, SQLAlchemy, psycopg, Redis, Celery and more, without changing your code.
Before you start
You need a stream and ingest key for each signal. See OpenTelemetry → Before you start.
1. Install
bash
pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a installopentelemetry-bootstrap looks at the packages you already have and installs the matching instrumentation.
2. Configure
bash
OTEL_SERVICE_NAME=checkout-api
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://ws.latency.app/v1/telemetry/traces
OTEL_EXPORTER_OTLP_TRACES_HEADERS=authorization=Bearer%20lit_your_traces_key
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://ws.latency.app/v1/telemetry/metrics
OTEL_EXPORTER_OTLP_METRICS_HEADERS=authorization=Bearer%20lit_your_metrics_key
OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=delta
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://ws.latency.app/v1/telemetry/logs
OTEL_EXPORTER_OTLP_LOGS_HEADERS=authorization=Bearer%20lit_your_logs_key
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=trueDon't skip the protocol
Python sends over gRPC unless told otherwise, and Latency only accepts HTTP. Keep OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf.
Only sending traces? Leave out the other blocks and add OTEL_METRICS_EXPORTER=none and OTEL_LOGS_EXPORTER=none.
3. Run
Put opentelemetry-instrument in front of your usual start command:
bash
opentelemetry-instrument python app.pybash
opentelemetry-instrument uvicorn main:appbash
opentelemetry-instrument flask runbash
opentelemetry-instrument python manage.py runserver --noreloadDjango's auto-reloader starts your app in a second process that skips instrumentation, so use --noreload in development.
4. Check it's working
Make a few requests to your app, then open Telemetry in the Console and pick your traces stream. Traces show up within seconds. Metrics follow within a minute.
Custom spans
python
from opentelemetry import trace
tracer = trace.get_tracer("checkout")
with tracer.start_as_current_span("charge-card") as span:
span.set_attribute("order.id", order.id)
charge_card(order)Custom metrics
python
from opentelemetry import metrics
meter = metrics.get_meter("checkout")
orders = meter.create_counter("orders.placed")
duration = meter.create_histogram("checkout.duration", unit="ms")
orders.add(1, {"plan": "pro"})
duration.record(182, {"route": "/pay"})Logs
Anything you log with Python's standard logging module is sent to your logs stream, linked to the trace it happened in. Nothing else to set up.