Exploring Manual Context Propagation, Custom Decorators, and Sampling Techniques
ThreadPoolExecutor
or any concurrency mechanism, each task runs in a separate thread. The tracer’s current context (which stores the active span or baggage) doesn’t automatically follow tasks to worker threads. By capturing the context in the main thread and attaching it in each worker thread, you maintain the association between tasks and the original trace context.
Example:
Below is a detailed, annotated example to show how you can:
attach
).
detach
).
Sampler
interface from opentelemetry.sdk.trace.sampling
. Implement:
should_sample(...)
should_sample
, you must return a SamplingResult
, which indicates:
Decision.RECORD_AND_SAMPLE
, Decision.RECORD_ONLY
, or Decision.DROP
SamplingResult
(e.g., a reason for sampling)