|
| 1 | +# Sampling |
| 2 | + |
| 3 | +This document is about the sampling bit, sampling decision, samplers and how and when does |
| 4 | +OpenCensus sample traces. A sampled traces is a traces that gets exported via the configured |
| 5 | +exporters. |
| 6 | + |
| 7 | +## Sampling Bit (propagated via TraceOptions) |
| 8 | + |
| 9 | +Sampling bit is always set only at the start of a Span, and is using a `Sampler` |
| 10 | + |
| 11 | +### What kind of samplers does OpenCensus support? |
| 12 | +* `AlwaysSample` - sampler that always makes a "yes" decision every time. |
| 13 | +* `NeverSample` - sampler that always makes a "no" decision every time. |
| 14 | +* `Probability` - sampler that tries to uniformly sample traces with a given probability. When |
| 15 | +applied to a child `Span` of a **sampled** parent `Span` then it keeps the sampling decision. |
| 16 | + |
| 17 | +### How can users control the Sampler that is used for sampling? |
| 18 | +There are 2 ways to control the `Sampler` used when the library does sampling: |
| 19 | +* Controlling the global default `Sampler` via [TraceConfig](https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/TraceConfig.md). |
| 20 | +* Pass a specific `Sampler` when start the [Span](https://github.com/census-instrumentation/opencensus-specs/blob/master/trace/Span.md) |
| 21 | +(a.k.a. "span-scoped"). |
| 22 | + * For example `AlwaysSample` and `NeverSample` can be used to implement request-specific |
| 23 | + decisions such as those based on http paths. |
| 24 | + |
| 25 | +### When does OpenCensus sample traces? |
| 26 | +The OpenCensus library does sampling based on the following rules: |
| 27 | +1. If the span is a root `Span` then a `Sampler` will be used to make the sampling decision: |
| 28 | + * If a "span-scoped" `Sampler` is provided, use it to determine the sampling decision. |
| 29 | + * Else use the global default `Sampler` to determine the sampling decision. |
| 30 | +2. If the span is a child of a remote `Span` the sampling decision will be: |
| 31 | + * If a "span-scoped" `Sampler` is provided, use it to determine the sampling decision. |
| 32 | + * Else use the global default `Sampler` to determine the sampling decision. |
| 33 | +2. If the span is a child of a local `Span` the sampling decision will be: |
| 34 | + * If a "span-scoped" `Sampler` is provided, use it to determine the sampling decision. |
| 35 | + * Else keep the sampling from the parent. |
0 commit comments