|
59 | 59 | # created spans will now be sampled by the TraceIdRatioBased sampler
|
60 | 60 | with trace.get_tracer(__name__).start_as_current_span("Test Span"):
|
61 | 61 | ...
|
| 62 | +
|
| 63 | +The tracer sampler can also be configured via environment variables ``OTEL_TRACE_SAMPLER`` and ``OTEL_TRACE_SAMPLER_ARG`` (only if applicable). |
| 64 | +The list of known values for ``OTEL_TRACE_SAMPLER`` are: |
| 65 | +
|
| 66 | + * always_on - Sampler that always samples spans, regardless of the parent span's sampling decision. |
| 67 | + * always_off - Sampler that never samples spans, regardless of the parent span's sampling decision. |
| 68 | + * traceidratio - Sampler that samples probabalistically based on rate. |
| 69 | + * parentbased_always_on - (default) Sampler that respects its parent span's sampling decision, but otherwise always samples. |
| 70 | + * parentbased_always_off - Sampler that respects its parent span's sampling decision, but otherwise never samples. |
| 71 | + * parentbased_traceidratio - Sampler that respects its parent span's sampling decision, but otherwise samples probabalistically based on rate. |
| 72 | +
|
| 73 | +Sampling probability can be set with ``OTEL_TRACE_SAMPLER_ARG`` if the sampler is traceidratio or parentbased_traceidratio, when not provided rate will be set to 1.0 (maximum rate possible). |
| 74 | +
|
| 75 | +
|
| 76 | +Prev example but with environment vairables. Please make sure to set the env ``OTEL_TRACE_SAMPLER=traceidratio`` and ``OTEL_TRACE_SAMPLER_ARG=0.001``. |
| 77 | +
|
| 78 | +.. code:: python |
| 79 | +
|
| 80 | + from opentelemetry import trace |
| 81 | + from opentelemetry.sdk.trace import TracerProvider |
| 82 | + from opentelemetry.sdk.trace.export import ( |
| 83 | + ConsoleSpanExporter, |
| 84 | + SimpleExportSpanProcessor, |
| 85 | + ) |
| 86 | +
|
| 87 | + trace.set_tracer_provider(TracerProvider()) |
| 88 | +
|
| 89 | + # set up an exporter for sampled spans |
| 90 | + trace.get_tracer_provider().add_span_processor( |
| 91 | + SimpleExportSpanProcessor(ConsoleSpanExporter()) |
| 92 | + ) |
| 93 | +
|
| 94 | + # created spans will now be sampled by the TraceIdRatioBased sampler with rate 1/1000. |
| 95 | + with trace.get_tracer(__name__).start_as_current_span("Test Span"): |
| 96 | + ... |
62 | 97 | """
|
63 | 98 | import abc
|
64 | 99 | import enum
|
| 100 | +import os |
| 101 | +from logging import getLogger |
65 | 102 | from types import MappingProxyType
|
66 | 103 | from typing import Optional, Sequence
|
67 | 104 |
|
|
71 | 108 | from opentelemetry.trace.span import TraceState
|
72 | 109 | from opentelemetry.util.types import Attributes
|
73 | 110 |
|
| 111 | +_logger = getLogger(__name__) |
| 112 | + |
74 | 113 |
|
75 | 114 | class Decision(enum.Enum):
|
76 | 115 | # IsRecording() == false, span will not be recorded and all events and attributes will be dropped.
|
@@ -307,3 +346,43 @@ def get_description(self):
|
307 | 346 |
|
308 | 347 | DEFAULT_ON = ParentBased(ALWAYS_ON)
|
309 | 348 | """Sampler that respects its parent span's sampling decision, but otherwise always samples."""
|
| 349 | + |
| 350 | + |
| 351 | +class ParentBasedTraceIdRatio(ParentBased): |
| 352 | + """ |
| 353 | + Sampler that respects its parent span's sampling decision, but otherwise |
| 354 | + samples probabalistically based on `rate`. |
| 355 | + """ |
| 356 | + |
| 357 | + def __init__(self, rate: float): |
| 358 | + root = TraceIdRatioBased(rate=rate) |
| 359 | + super().__init__(root=root) |
| 360 | + |
| 361 | + |
| 362 | +_KNOWN_SAMPLERS = { |
| 363 | + "always_on": ALWAYS_ON, |
| 364 | + "always_off": ALWAYS_OFF, |
| 365 | + "parentbased_always_on": DEFAULT_ON, |
| 366 | + "parentbased_always_off": DEFAULT_OFF, |
| 367 | + "traceidratio": TraceIdRatioBased, |
| 368 | + "parentbased_traceidratio": ParentBasedTraceIdRatio, |
| 369 | +} |
| 370 | + |
| 371 | + |
| 372 | +def _get_from_env_or_default() -> Sampler: |
| 373 | + trace_sampler = os.getenv( |
| 374 | + "OTEL_TRACE_SAMPLER", "parentbased_always_on" |
| 375 | + ).lower() |
| 376 | + if trace_sampler not in _KNOWN_SAMPLERS: |
| 377 | + _logger.warning("Couldn't recognize sampler %s.", trace_sampler) |
| 378 | + trace_sampler = "parentbased_always_on" |
| 379 | + |
| 380 | + if trace_sampler in ("traceidratio", "parentbased_traceidratio"): |
| 381 | + try: |
| 382 | + rate = float(os.getenv("OTEL_TRACE_SAMPLER_ARG")) |
| 383 | + except ValueError: |
| 384 | + _logger.warning("Could not convert TRACE_SAMPLER_ARG to float.") |
| 385 | + rate = 1.0 |
| 386 | + return _KNOWN_SAMPLERS[trace_sampler](rate) |
| 387 | + |
| 388 | + return _KNOWN_SAMPLERS[trace_sampler] |
0 commit comments