Skip to content

Commit 0465f5e

Browse files
nandshaalexmojaki
andauthored
docs: add custom sampling example for different span names (#1121)
Co-authored-by: Alex Hall <[email protected]>
1 parent d1a6f4c commit 0465f5e

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

docs/how-to-guides/sampling.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ from opentelemetry.sdk.trace.sampling import (
273273
ALWAYS_ON,
274274
ParentBased,
275275
Sampler,
276+
TraceIdRatioBased,
276277
)
277278

278279
import logfire
@@ -289,6 +290,10 @@ class MySampler(Sampler):
289290
):
290291
if name == 'exclude me':
291292
sampler = ALWAYS_OFF
293+
elif name == 'include me minimally':
294+
sampler = TraceIdRatioBased(0.01) # 1% sampling
295+
elif name == 'include me partially':
296+
sampler = TraceIdRatioBased(0.5) # 50% sampling
292297
else:
293298
sampler = ALWAYS_ON
294299
return sampler.should_sample(
@@ -314,6 +319,14 @@ logfire.configure(
314319
with logfire.span('keep me'):
315320
logfire.info('kept child')
316321

322+
for i in range(5):
323+
with logfire.span('include me partially'):
324+
logfire.info(f'partial sample {i}')
325+
326+
for i in range(270):
327+
with logfire.span('include me minimally'):
328+
logfire.info(f'minimal sample {i}')
329+
317330
with logfire.span('exclude me'):
318331
logfire.info('excluded child')
319332
```
@@ -323,12 +336,26 @@ This will output something like:
323336
```
324337
10:37:30.897 keep me
325338
10:37:30.898 kept child
339+
10:37:30.899 include me partially
340+
10:37:30.900 partial sample 0
341+
10:37:30.901 include me partially
342+
10:37:30.902 partial sample 3
343+
10:37:30.905 include me minimally
344+
10:37:30.906 minimal sample 47
345+
10:37:30.910 include me minimally
346+
10:37:30.911 minimal sample 183
326347
```
327348

328-
Note that the sampler explicitly excluded only the span named `exclude me`. The reason that the `excluded child` log is
329-
not included is that `MySampler` was wrapped in a `ParentBased` sampler, which excludes spans whose parents are
330-
excluded. If you remove that and simply pass `head=MySampler()`, the `excluded child` log will be included, resulting in
331-
an incomplete trace.
349+
The sampler applies different strategies based on span names:
350+
351+
- `exclude me`: Never sampled (0% using `ALWAYS_OFF`)
352+
- `include me partially`: 50% sampling (roughly half appear)
353+
- `include me minimally`: 1% sampling (roughly 1 in a 100 appears)
354+
- `keep me` and all others: Always sampled (100% using `ALWAYS_ON`)
355+
356+
The sampler is wrapped in a `ParentBased` sampler, which ensures child spans follow their parent's sampling decision.
357+
If you remove that and simply pass `head=MySampler()`, child spans might be included even when their parents are
358+
excluded, resulting in incomplete traces.
332359

333360
You can also pass a `Sampler` to the `head` argument of `SamplingOptions.level_or_duration` to combine tail sampling
334361
with custom head sampling.

0 commit comments

Comments
 (0)