Skip to content

Commit 5cb01d6

Browse files
authored
docs: Documentation for sampling (open-telemetry#882)
1 parent 1dde6ff commit 5cb01d6

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

docs/api/trace.sampling.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
opentelemetry.trace.sampling
2-
============================
1+
Sampling Traces
2+
===============
33

44
.. automodule:: opentelemetry.trace.sampling
55
:members:

opentelemetry-api/src/opentelemetry/trace/sampling.py

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,52 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

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+
"""
1561
import abc
1662
from typing import Dict, Mapping, Optional, Sequence
1763

@@ -78,6 +124,14 @@ def should_sample(
78124

79125

80126
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+
81135
def __init__(self, rate: float):
82136
self._rate = rate
83137
self._bound = self.get_bound_for_rate(self._rate)
@@ -118,11 +172,15 @@ def should_sample(
118172
return Decision(trace_id & self.TRACE_ID_LIMIT < self.bound)
119173

120174

121-
# Samplers that ignore the parent sampling decision and never/always sample.
122175
ALWAYS_OFF = StaticSampler(Decision(False))
176+
"""Sampler that never samples spans, regardless of the parent span's sampling decision."""
177+
123178
ALWAYS_ON = StaticSampler(Decision(True))
179+
"""Sampler that always samples spans, regardless of the parent span's sampling decision."""
180+
124181

125-
# Samplers that respect the parent sampling decision, but otherwise
126-
# never/always sample.
127182
DEFAULT_OFF = ProbabilitySampler(0.0)
183+
"""Sampler that respects its parent span's sampling decision, but otherwise never samples."""
184+
128185
DEFAULT_ON = ProbabilitySampler(1.0)
186+
"""Sampler that respects its parent span's sampling decision, but otherwise always samples."""

0 commit comments

Comments
 (0)