OpenTelemetry Container Distro for Python

0.2.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to use the `opentelemetry-container-distro` by simply installing it and then interacting with the standard OpenTelemetry Python API. The key takeaway is how to retrieve and inspect the resource attributes, which should automatically include container-specific metadata if the application is running within a recognized container environment (e.g., Docker, Kubernetes). An OpenTelemetry Collector is typically needed to receive and export telemetry data.

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.")

view raw JSON →