OpenTelemetry Container Distro for Python
An OpenTelemetry distro which automatically discovers container attributes from the environment. This distribution extends the standard `opentelemetry-distro` to automatically include additional resource detectors tailored for container environments, such as Docker or Kubernetes. It aims to simplify OpenTelemetry setup by providing automatic configuration, while largely preserving the behavior of `opentelemetry-distro`. The current version is 0.2.0, released on September 18, 2022, indicating a slower release cadence compared to core OpenTelemetry components.
Warnings
- gotcha This distro, like `opentelemetry-distro`, primarily configures an OTLP SpanExporter, which typically sends data to a running OpenTelemetry Collector. Users must ensure a Collector is deployed and accessible to actually receive and process the telemetry. The Collector itself has frequent breaking changes and its Docker image registry moved from DockerHub to GitHub Container Registry, requiring updates to deployment configurations.
- gotcha For automatic container attribute detection to work, the application or the underlying OpenTelemetry SDK components need appropriate access to the container runtime's information, typically via the Docker socket (`/var/run/docker.sock`) or similar mechanisms for other container runtimes. Without this access, container attributes will not be automatically populated.
- gotcha The `opentelemetry-container-distro` has seen infrequent updates (last release 0.2.0 in September 2022). In contrast, the upstream OpenTelemetry Python SDK and `opentelemetry-distro` are under active and rapid development (e.g., `opentelemetry-distro 0.62b0` in April 2026). This disparity means that `opentelemetry-container-distro` might depend on older versions of the SDK, potentially missing out on newer features, performance improvements, or important bug fixes available in more recent OpenTelemetry releases.
- gotcha The core concept of an OpenTelemetry 'distro' is to provide auto-configuration, meaning much of the setup happens implicitly upon installation and application startup. New users might expect explicit `init()` or `configure()` calls directly on the distro package, but the primary interaction remains through the standard `opentelemetry` API (e.g., `opentelemetry.trace`).
Install
-
pip install opentelemetry-container-distro
Imports
- get_resource
from opentelemetry.sdk.resources import get_resource
- trace
from opentelemetry import trace
Quickstart
import os
from opentelemetry import trace
from opentelemetry.sdk.resources import get_resource
# Ensure an OTLP collector is available if you want to export traces.
# For example, by running 'docker run -p 4317:4317 -p 4318:4318 otel/opentelemetry-collector:latest'
# or configure your OTLP endpoint via environment variables like OTEL_EXPORTER_OTLP_ENDPOINT.
# The distro is installed and auto-configures the tracer provider and resource detectors.
# We can directly get the tracer and retrieve the resource.
tracer = trace.get_tracer("my-container-app")
with tracer.start_as_current_span("my-container-operation"):
print("Performing an operation within a container context...")
# Retrieve the resource and print its attributes to show container detection
resource = get_resource()
print("\nDetected Resource Attributes (should include container.* if running in a container):")
for key, value in resource.attributes.items():
print(f" {key}: {value}")
print("\nTrace generated. Check your OTLP collector for exported data.")