OpenTelemetry Instrumentation for Python

raw JSON →
0.61b0 verified Tue May 12 auth: no python install: verified quickstart: stale

Provides automatic instrumentation tools for OpenTelemetry in Python, enabling seamless tracing and monitoring of Python applications. Current version: 0.61b0. Release cadence: Regular updates with new features and support for additional Python versions.

pip install opentelemetry-instrumentation
error ModuleNotFoundError: No module named 'opentelemetry.instrumentation'
cause This error occurs when the core `opentelemetry-instrumentation` package or a specific instrumentation library (e.g., `opentelemetry-instrumentation-flask`) is not installed, or there is a mismatch in Python environments between where the package is installed and where the application is run.
fix
Install the necessary OpenTelemetry instrumentation packages using pip install opentelemetry-instrumentation and any specific instrumentation packages for your frameworks (e.g., pip install opentelemetry-instrumentation-flask). Always ensure your application is running in the correct Python environment where these packages are installed.
error ImportError: cannot import name '_SUPPRESS_HTTP_INSTRUMENTATION_KEY' from 'opentelemetry.context'
cause This issue typically arises from version incompatibilities between the `opentelemetry-api` package and specific `opentelemetry-instrumentation-*` packages, where an instrumentation package attempts to import a non-existent key from an older or incompatible API version.
fix
Upgrade all related OpenTelemetry packages to compatible versions. The most reliable fix is often to update opentelemetry-api, opentelemetry-sdk, and all opentelemetry-instrumentation-* packages to their latest stable releases using pip install --upgrade opentelemetry-api opentelemetry-sdk opentelemetry-instrumentation-requests (and any other relevant instrumentation packages).
error AttributeError: 'ProxyTracerProvider' object has no attribute 'resource'
cause This error indicates that the OpenTelemetry SDK and its tracing pipeline, including the `TracerProvider` and its associated `Resource`, have not been fully or correctly initialized before an instrumentation attempts to access these components.
fix
Ensure that the OpenTelemetry SDK, including the TracerProvider and a Resource, is fully configured and set as the global tracer provider *before* any instrumentors are initialized or any instrumented application code begins execution. If using programmatic instrumentation, place SDK initialization at the very top of your application's entry point.
error No telemetry is produced
cause This common symptom, rather than a specific error message, occurs when your application is instrumented, but no trace, metric, or log data appears in your backend. This can be due to incorrect SDK initialization order, misconfigured or missing exporters (e.g., OTLP exporter), network connectivity problems to the OpenTelemetry Collector, or incorrect environment variable settings for auto-instrumentation.
fix
Verify that the OpenTelemetry SDK is initialized before any instrumented code runs. Add a ConsoleExporter to your pipeline for debugging purposes to confirm data generation locally. Check all relevant environment variables, such as OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_TRACES_EXPORTER, for correct values. Ensure the OpenTelemetry Collector is running and network accessible at the configured endpoint.
breaking The 'instrumentation' module has been refactored; ensure imports are updated accordingly.
fix Update import statements to reflect the new module structure.
gotcha Automatic instrumentation may not support all third-party libraries out of the box.
fix Manually instrument unsupported libraries by importing the appropriate instrumentor classes and calling their 'instrument()' methods.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.30s 22.0M
3.10 slim (glibc) - - 0.20s 23M
3.11 alpine (musl) - - 0.51s 24.2M
3.11 slim (glibc) - - 0.39s 25M
3.12 alpine (musl) - - 0.64s 16.0M
3.12 slim (glibc) - - 0.60s 17M
3.13 alpine (musl) - - 0.28s 15.6M
3.13 slim (glibc) - - 0.28s 16M
3.9 alpine (musl) - - 0.26s 21.5M
3.9 slim (glibc) - - 0.23s 22M

Quickstart guide to instrumenting ASGI and WSGI applications using OpenTelemetry.

import os
from opentelemetry.instrumentation.asgi import AsgiInstrumentor
from opentelemetry.instrumentation.wsgi import WSGIInstrumentor

# Initialize instrumentation
AsgiInstrumentor().instrument()
WSGIInstrumentor().instrument()

# Your application code here
# For example, using ASGI:
# app = ...
# app.run()

# For WSGI:
# from wsgi_app import app
# app.run()