|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +""" |
| 16 | +For general information about sampling, see `the specification <https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#sampling>`_. |
| 17 | +
|
| 18 | +OpenTelemetry provides two types of samplers: |
| 19 | +
|
| 20 | +- `StaticSampler` |
| 21 | +- `ProbabilitySampler` |
| 22 | +
|
| 23 | +A `StaticSampler` always returns the same sampling decision regardless of the conditions. Both possible StaticSamplers are already created: |
| 24 | +
|
| 25 | +- Always sample spans: `ALWAYS_ON` |
| 26 | +- Never sample spans: `ALWAYS_OFF` |
| 27 | +
|
| 28 | +A `ProbabilitySampler` makes a random sampling decision based on the sampling probability given. If the span being sampled has a parent, `ProbabilitySampler` will respect the parent span's sampling decision. |
| 29 | +
|
| 30 | +Currently, sampling decisions are always made during the creation of the span. However, this might not always be the case in the future (see `OTEP #115 <https://github.com/open-telemetry/oteps/pull/115>`_). |
| 31 | +
|
| 32 | +Custom samplers can be created by subclassing `Sampler` and implementing `Sampler.should_sample`. |
| 33 | +
|
| 34 | +To use a sampler, pass it into the tracer provider constructor. For example: |
| 35 | +
|
| 36 | +.. code:: python |
| 37 | +
|
| 38 | + from opentelemetry import trace |
| 39 | + from opentelemetry.trace.sampling import ProbabilitySampler |
| 40 | + from opentelemetry.sdk.trace import TracerProvider |
| 41 | + from opentelemetry.sdk.trace.export import ( |
| 42 | + ConsoleSpanExporter, |
| 43 | + SimpleExportSpanProcessor, |
| 44 | + ) |
| 45 | +
|
| 46 | + # sample 1 in every 1000 traces |
| 47 | + sampler = ProbabilitySampler(1/1000) |
| 48 | +
|
| 49 | + # set the sampler onto the global tracer provider |
| 50 | + trace.set_tracer_provider(TracerProvider(sampler=sampler)) |
| 51 | +
|
| 52 | + # set up an exporter for sampled spans |
| 53 | + trace.get_tracer_provider().add_span_processor( |
| 54 | + SimpleExportSpanProcessor(ConsoleSpanExporter()) |
| 55 | + ) |
| 56 | +
|
| 57 | + # created spans will now be sampled by the ProbabilitySampler |
| 58 | + with trace.get_tracer(__name__).start_as_current_span("Test Span"): |
| 59 | + ... |
| 60 | +""" |
15 | 61 | import abc |
16 | 62 | from typing import Dict, Mapping, Optional, Sequence |
17 | 63 |
|
@@ -78,6 +124,14 @@ def should_sample( |
78 | 124 |
|
79 | 125 |
|
80 | 126 | class ProbabilitySampler(Sampler): |
| 127 | + """ |
| 128 | + Sampler that makes sampling decisions probabalistically based on `rate`, |
| 129 | + while also respecting the parent span sampling decision. |
| 130 | +
|
| 131 | + Args: |
| 132 | + rate: Probability (between 0 and 1) that a span will be sampled |
| 133 | + """ |
| 134 | + |
81 | 135 | def __init__(self, rate: float): |
82 | 136 | self._rate = rate |
83 | 137 | self._bound = self.get_bound_for_rate(self._rate) |
@@ -118,11 +172,15 @@ def should_sample( |
118 | 172 | return Decision(trace_id & self.TRACE_ID_LIMIT < self.bound) |
119 | 173 |
|
120 | 174 |
|
121 | | -# Samplers that ignore the parent sampling decision and never/always sample. |
122 | 175 | ALWAYS_OFF = StaticSampler(Decision(False)) |
| 176 | +"""Sampler that never samples spans, regardless of the parent span's sampling decision.""" |
| 177 | + |
123 | 178 | ALWAYS_ON = StaticSampler(Decision(True)) |
| 179 | +"""Sampler that always samples spans, regardless of the parent span's sampling decision.""" |
| 180 | + |
124 | 181 |
|
125 | | -# Samplers that respect the parent sampling decision, but otherwise |
126 | | -# never/always sample. |
127 | 182 | DEFAULT_OFF = ProbabilitySampler(0.0) |
| 183 | +"""Sampler that respects its parent span's sampling decision, but otherwise never samples.""" |
| 184 | + |
128 | 185 | DEFAULT_ON = ProbabilitySampler(1.0) |
| 186 | +"""Sampler that respects its parent span's sampling decision, but otherwise always samples.""" |
0 commit comments