OpenTelemetry Python Distro

0.61b0 · active · verified Sun Mar 29

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

Install

Imports

Quickstart

To quickly get started, install `opentelemetry-distro[otlp]` and then run `opentelemetry-bootstrap -a install` to automatically install instrumentations for detected libraries. Then, run your application using the `opentelemetry-instrument` wrapper script. This example demonstrates a simple Flask application that makes an HTTP request, which will be automatically traced. Ensure an OpenTelemetry Collector is running to receive telemetry data. For Flask/Django, set `use_reloader=False` (Flask) or use `--noreload` (Django) to avoid instrumentation issues.

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)

view raw JSON →