AWS OpenTelemetry Python Distro
The AWS OpenTelemetry Python Distro (`aws-opentelemetry-distro`) is a distribution of the OpenTelemetry Python SDK, pre-configured for AWS environments. It includes AWS-specific resource detectors, X-Ray compatible trace ID generation, and automatic instrumentation for AWS SDK (boto3/botocore) and popular frameworks. It aims to simplify the adoption of OpenTelemetry for applications running on AWS, providing a streamlined path to send traces, metrics, and logs to services like AWS X-Ray and Amazon CloudWatch. The current version is `0.17.0`, with releases typically aligning with upstream OpenTelemetry component updates and AWS-specific feature additions.
Warnings
- breaking Version `0.13.0` was explicitly deprecated due to a critical issue and should not be used. All users on this version should revert to an older stable release or upgrade to `0.14.0` or newer.
- gotcha The distro heavily relies on environment variables for configuration (e.g., `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_RESOURCE_ATTRIBUTES`, `OTEL_PYTHON_CONFIGURATOR`, `AWS_LAMBDA_EXEC_WRAPPER`). For full observability, ensure these are correctly set for your deployment environment (e.g., pointing to an ADOT Collector or X-Ray daemon). Without a configured exporter, traces will be generated but not sent anywhere.
- gotcha When using Django instrumentation, the `DJANGO_SETTINGS_MODULE` environment variable must be correctly set, otherwise Django auto-instrumentation will be disabled. This prevents issues when running Django-based applications where the settings module is not immediately available.
- gotcha Updating `aws-opentelemetry-distro` bundles updates to upstream OpenTelemetry Python components (SDK, API, instrumentations). While this simplifies dependency management, new versions of these upstream components might introduce breaking changes or behavioral shifts that you should be aware of, even if the `aws-opentelemetry-distro` itself has no direct breaking changes.
Install
-
pip install aws-opentelemetry-distro -
pip install aws-opentelemetry-distro[all]
Imports
- start_tracing
from aws_opentelemetry_distro import start_tracing
- AwsResourceDetector
from aws_opentelemetry_distro.aws_resource_detector import AwsResourceDetector
Quickstart
import os
from aws_opentelemetry_distro import start_tracing
from opentelemetry import trace
# 1. Initialize the AWS OpenTelemetry Distro.
# This sets up the global trace provider, resource detectors, and default exporters
# based primarily on environment variables (e.g., OTEL_EXPORTER_OTLP_ENDPOINT).
# In AWS Lambda or ECS/EC2 with the ADOT Collector, these environment variables are often pre-configured.
start_tracing()
# 2. Get a tracer and create a span.
tracer = trace.get_tracer("my-application-tracer")
with tracer.start_as_current_span("my-aws-operation") as span:
span.set_attribute("service.name", "my-app")
span.set_attribute("aws.region", os.environ.get("AWS_REGION", "us-east-1"))
print("Performing some instrumented work with AWS OpenTelemetry Distro...")
# Simulate some work
import time
time.sleep(0.1)
print("Application finished. Spans were sent to the configured OTLP endpoint.")
print("To see output, ensure OTEL_EXPORTER_OTLP_ENDPOINT is set (e.g., to a local ADOT Collector or X-Ray daemon).")