Registry / observability / opentelemetry-propagator-aws-xray

opentelemetry-propagator-aws-xray

JSON →
library1.0.2pypypi✓ verified 24d ago

The `opentelemetry-propagator-aws-xray` library provides an OpenTelemetry `TextMapPropagator` that enables propagation of trace context in AWS X-Ray compatible headers. It allows OpenTelemetry Python applications to integrate with existing AWS X-Ray traces by injecting and extracting the `X-Amzn-Trace-Id` header. The current version is 1.0.2, and it follows the release cadence of the broader `opentelemetry-python-contrib` project, which typically sees updates aligned with core OpenTelemetry Python releases.

pip install opentelemetry-propagator-aws-xray
INSTALL
IMPORT
SIG · OPENTELEMETRY-PROP
O
opentelemetry-propagator-aws-xray
observabilitypythonv1.0.2
Install
1.7s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.000s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AWSXRayPropagator
from opentelemetry.propagators.aws_xray import AWSXRayPropagator
from opentelemetry.propagators.aws import AWSXRayPropagator

This quickstart demonstrates how to configure OpenTelemetry to use the AWS X-Ray Propagator and `AwsXRayIdGenerator`. It shows how to initialize the `TracerProvider` for X-Ray compatible trace IDs, set the global `AWSXRayPropagator`, and simulate injecting and extracting X-Ray trace context for distributed tracing.

from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor from opentelemetry.sdk.trace.id_generator import AwsXRayIdGenerator from opentelemetry.propagators.aws.xray import AWSXRayPropagator from opentelemetry.propagate import set_global_textmap_propagator from opentelemetry.trace import get_tracer_provider, set_tracer_provider, get_tracer, get_current_span # 1. Configure TracerProvider with AwsXRayIdGenerator # This ensures new traces generated by this application use AWS X-Ray compatible IDs. provider = TracerProvider(id_generator=AwsXRayIdGenerator()) # For demonstration, we'll use a ConsoleSpanExporter. # In a real application, you'd use an OTLPSpanExporter or similar. processor = SimpleSpanProcessor(ConsoleSpanExporter()) provider.add_span_processor(processor) set_tracer_provider(provider) # 2. Set the global propagator to AWSXRayPropagator # This enables automatic injection/extraction of X-Ray headers. set_global_textmap_propagator(AWSXRayPropagator()) # 3. Obtain a tracer and create spans tracer = get_tracer(__name__) print("\n--- Creating an initial span and injecting context ---") with tracer.start_as_current_span("my-xray-root-span") as root_span: print(f"Root Span ID: {root_span.context.span_id:x}") print(f"Root Trace ID: {root_span.context.trace_id:x}") # Simulate injecting trace context into outgoing headers carrier = {} AWSXRayPropagator().inject(carrier) print("Injected headers (simulating HTTP request outgoing):", carrier) print("\n--- Simulating a new request receiving injected headers ---") # Simulate receiving injected headers in a new context # We start a new span, but its context will be derived from the extracted headers. # Let's assume 'carrier' contains the headers from the previous step received_carrier = carrier.copy() # When a new span is started, OpenTelemetry automatically extracts context # from the global propagator if it's available in the current context (e.g., from a request). # For this example, we manually extract and then demonstrate a child span. # Manually extract context for clarity, though it's often done automatically by instrumentations. extracted_context = AWSXRayPropagator().extract(received_carrier) with tracer.start_as_current_span("my-xray-child-span", context=extracted_context) as child_span: print(f"Child Span ID: {child_span.context.span_id:x}") print(f"Child Trace ID: {child_span.context.trace_id:x}") print(f"Is child span linked to root? {root_span.context.trace_id == child_span.context.trace_id}") # Further nested span with tracer.start_as_current_span("nested-operation"): print(" Inside nested operation.") print("\n--- End of quickstart ---")
Debug
Known issues
gotchaThe `AWSXRayPropagator` primarily handles the `X-Amzn-Trace-Id` header format. For OpenTelemetry to generate trace IDs that are natively compatible with AWS X-Ray (i.e., epoch time + 24-byte unique identifier), you *must* configure your `TracerProvider` with `AwsXRayIdGenerator`.
fix
Ensure `provider = TracerProvider(id_generator=AwsXRayIdGenerator())` is used when setting up your tracing.
affects: All versions
gotchaIf you are relying on automatic instrumentation or environment variables for configuration, ensure the `OTEL_PROPAGATORS` environment variable is set correctly. The value `awsxray` needs to be included.
fix
Set `OTEL_PROPAGATORS="tracecontext,baggage,awsxray"` (or similar, including other desired propagators) in your environment to enable it without programmatic setup.
affects: All versions
gotchaAWS X-Ray trace IDs have a different format (version, timestamp, random ID) compared to W3C Trace Context trace IDs (16-byte random ID). While `AWSXRayPropagator` translates between these formats when headers are exchanged, ensure your entire tracing setup (ID generation, sampling, exporter) is compatible with X-Ray if deep integration is required.
fix
Use `AwsXRayIdGenerator` for new traces to ensure native X-Ray IDs are generated. If traces originate outside your OTel application (e.g., from an AWS service), the propagator will extract the X-Ray ID and continue the trace correctly.
affects: All versions
Upgrade
Version history
1.0.2latest on PyPI · released Aug 5, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
27 hits · last 30 days
node
24
Amazon
1
OpenAI (training)
1
Resources
opentelemetry-propagator-aws-xray — pip install opentelemetry-propagator-aws-xray · libregistry