OpenTelemetry Python Distro
OpenTelemetry Distro provides a mechanism to automatically configure some of the more common options for users, making OpenTelemetry and auto-instrumentation as quick as possible without sacrificing flexibility. It configures the SDK TracerProvider, a BatchSpanProcessor, and the OTLP SpanExporter to send data to an OpenTelemetry Collector. The current version is 0.61b0, and it is part of the `opentelemetry-python-contrib` project with frequent beta releases.
Warnings
- gotcha OpenTelemetry Python, including `opentelemetry-distro`, is largely in beta. While tracing APIs are relatively stable, metrics and logging APIs are still evolving. This means breaking changes can occur between minor versions.
- gotcha The `opentelemetry-distro` configures a default OTLP SpanExporter, which attempts to send data to an OpenTelemetry Collector (by default on `http://localhost:4317` or `grpc://localhost:4317`). If no Collector is running or accessible, telemetry will not be exported.
- breaking When using web frameworks like Flask or Django, running in debug mode with an enabled reloader (e.g., `app.run(debug=True)`) can conflict with auto-instrumentation, causing it to fail.
- gotcha The generic `opentelemetry-distro` provides default configurations. If you intend to use a vendor-specific OpenTelemetry distribution (e.g., AWS Distro for OpenTelemetry, Azure Monitor OpenTelemetry Distro), ensure you install and use *that specific distro* as it often includes tailored exporters, resource detectors, and instrumentations for the target backend. Mixing distros or expecting generic distro to have vendor-specific features is a common mistake.
Install
-
pip install opentelemetry-distro[otlp] opentelemetry-instrumentation opentelemetry-bootstrap -a install
Imports
- Auto-instrumentation via opentelemetry-instrument
opentelemetry-instrument python my_app.py
Quickstart
import os
import requests
from flask import Flask
app = Flask(__name__)
@app.route("/hello")
def hello_world():
return "Hello, World!"
@app.route("/external")
def call_external():
# This call should be traced automatically if requests is instrumented
response = requests.get("http://localhost:5001/hello")
return f"Called external service: {response.text}"
if __name__ == '__main__':
# Run this app with: opentelemetry-instrument python app.py
# Ensure an OpenTelemetry Collector is running on localhost:4317 or configure OTEL_EXPORTER_OTLP_ENDPOINT
# (e.g., docker run -p 4317:4317 -p 4318:4318 otel/opentelemetry-collector:latest --config=/etc/otel-collector-config.yaml)
# The collector config should include otlp receiver and a logging/debug exporter.
app.run(port=5001, debug=False, use_reloader=False)