Last updated
Copyright © 2023 Arize AI, Inc
Last updated
This documentation provides advanced use cases and examples, including manual context propagation, custom decorators, custom sampling/filtering, and more. These scenarios address real-world needs such as asynchronous execution, multi-service flows and specialized exporters or decorators for observability platforms like Arize.
Context Propagation in OpenTelemetry ensures that the current tracing context (i.e., the currently active span and its metadata) is available whenever you switch threads, tasks, or processes. This is particularly relevant if your code spans across asynchronous tasks or crosses microservice boundaries.
In typical usage, OTEL instrumentation libraries handle context propagation automatically. However, there are cases where you need to do it manually, especially in asynchronous workflows or custom instrumentation.
When dealing with Python async/await
code, you can manually pass context if an automated instrumentation doesn’t handle it, or if you have custom logic. The steps are:
Extract the current context (e.g., from an incoming HTTP request).
Create a new span as a child of that context.
Pass or embed the context into the async function so it can be reused.
When making HTTP or gRPC calls to another microservice, we typically propagate the current tracing context through HTTP headers. If you’re using the built-in instrumentation (like opentelemetry-instrumentation-requests
or opentelemetry-instrumentation-httpx
), it’s handled automatically. For a custom approach, you do the following:
Inject the current span context into HTTP headers before sending the request.
On the receiving microservice, extract the context from the incoming headers.
Example: Service A sends a request to Service B.
Service A:
Service B:
When you submit tasks to a ThreadPoolExecutor
or any other concurrency mechanism, each task runs in a separate thread. If you rely on a tracer’s current context (which stores the active span or baggage), it won’t automatically follow your tasks to those worker threads. By manually capturing the context in the main thread and then attaching it in each worker thread, you preserve the association between the tasks and the original trace context.
Below is a detailed, annotated example to show how you can:
Capture the current context before submitting tasks to the executor.
Attach that context within each worker thread (using attach
).
Run your task logic (e.g., processing questions).
Detach the context when the task is complete (using detach
).
Decorators are a convenient way to instrument functions and methods across your codebase without having to insert tracing calls repeatedly. A custom decorator can:
Start a new span before the function call.
Add attributes/events with function arguments (inputs).
Return the function’s result (outputs) and annotate or log it in the span.
End the span.
Example Decorator Implementation:
In large-scale applications, you may not need to record every single span. Instead, you might want to selectively sample:
Spans of a particular service or component.
Spans that meet certain business criteria (e.g., user.id
in a specific subset).
Only error or slow spans.
By creating a custom sampler, you can dynamically control which spans get recorded/exported based on their attributes or names. This approach helps control telemetry volume and cost, while ensuring you capture the traces most relevant for debugging or analysis.
In OTEL Python, you create a custom sampler by subclassing the Sampler
interface from opentelemetry.sdk.trace.sampling
. You then implement:
should_sample(...)
Decides whether the span is recorded (Sampled) or dropped (NotSampled).
You can look at the attributes, span name, span kind, parent context, etc.
When implementing should_sample
, you must return a SamplingResult
, which indicates:
Sampling Decision: Decision.RECORD_AND_SAMPLE
, Decision.RECORD_ONLY
, or Decision.DROP
.
Attributes: You can optionally modify or add attributes in the returned SamplingResult
(e.g., a reason for sampling).
Example:
Let's create an example sampling mechanism where spans with a specific user ID is dropped.
You then pass your custom sampler into your tracer provider.
Manual Context Propagation, Custom Decorators, Custom Exports and Sampling