Stefan
12/16/2025, 9:24 PMMarvin
12/16/2025, 9:24 PMMarvin
12/16/2025, 9:33 PMpip install 'prefect[otel]' 'opentelemetry-instrumentation-logging' 'opentelemetry-exporter-otlp'
Common environment variables (point at Grafana Alloy/Agent or an OTel Collector)
- Change the endpoint/headers for Grafana Cloud per your stack’s instructions.
# Identify the emitting service
export OTEL_SERVICE_NAME=prefect
# Export traces and logs via OTLP
export OTEL_TRACES_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
# Where to send OTLP (gRPC 4317 or HTTP 4318)
export OTEL_EXPORTER_OTLP_ENDPOINT=<http://alloy:4317>
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
# If sending to Grafana Cloud, also set headers (replace with your token/endpoint)
# export OTEL_EXPORTER_OTLP_ENDPOINT=https://<your-otlp-endpoint>
# export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer <your_api_token>"
# Helpful when debugging
export OTEL_LOG_LEVEL=debug
Flows and workers (send both traces and logs)
Option A: Easiest — run processes with OpenTelemetry’s auto-instrumentation wrapper
- Local ad-hoc flow run
export OTEL_SERVICE_NAME=prefect-flows
opentelemetry-instrument python my_flow.py
- Start a worker with instrumentation (polls your work pool and instruments spawned runs)
export OTEL_SERVICE_NAME=prefect-worker
opentelemetry-instrument prefect worker start -p <your-pool-name>
Notes:
- The auto-instrumentation wrapper enables the logging instrumentation without code changes and configures the SDK/exporters from your OTEL_* env vars.
- If you’re using Docker/Kubernetes workers, install the same OTel packages in the image and wrap the entrypoint or command with opentelemetry-instrument. Alternatively, instrument programmatically in your flow entrypoint.
Option B: Programmatic logging instrumentation (if you can’t use the wrapper)
- At the top of your flow’s entrypoint module, initialize the OTel logs SDK and enable the logging instrumentor. Keep it minimal and let env vars drive the exporter. If you want this, I can share a version that matches your OTel SDK version; it’s a bit version-sensitive in Python.
Prefect Server (self-hosted) traces and logs
- Prefect Server can emit OTel telemetry (e.g., FastAPI/SQLAlchemy and Python logs) when the SDK/exporters are installed and enabled.
- Start it under the auto-instrumentation wrapper so both traces and logs are exported:
export OTEL_SERVICE_NAME=prefect-server
opentelemetry-instrument prefect server start
Traces only (no logs)
- If you only want traces from flows/tasks, Prefect already creates spans. Install opentelemetry-exporter-otlp and set the OTEL_* vars (no logging instrumentation needed). Ensure orchestration telemetry is enabled:
prefect config set cloud.enable_orchestration_telemetry=true
Where the signals go in GrafanaMarvin
12/16/2025, 9:33 PM