This document is about the sampling bit, sampling decision, samplers and how and when OpenCensus samples traces. A sampled trace is one that gets exported via the configured exporters.
The Sampling bit is always set only at the start of a Span, using a Sampler
AlwaysSample
- sampler that makes a “yes” decision every time.NeverSample
- sampler that makes a “no” decision every time.Probability
- sampler that tries to uniformly sample traces with a given probability. When
applied to a child Span
of a sampled parent Span
, the child Span
keeps the sampling
decision.RateLimiting
- sampler that tries to sample with a rate per time window (0.1 traces/second).
When applied to a child Span
of a sampled parent Span
, the child Span
keeps the sampling
decision. For implementation details see thisThere are 2 ways to control the Sampler
used when the library samples:
Sampler
via TraceConfig.Sampler
when starting the Span
(a.k.a. “span-scoped”).
AlwaysSample
and NeverSample
can be used to implement request-specific
decisions such as those based on http paths.The OpenCensus library samples based on the following rules:
Span
, then a Sampler
will be used to make the sampling decision:
Sampler
is provided, use it to determine the sampling decision.Sampler
to determine the sampling decision.Span
the sampling decision will be:
Sampler
is provided, use it to determine the sampling decision.Sampler
to determine the sampling decision.Span
the sampling decision will be:
Sampler
is provided, use it to determine the sampling decision.The problem we are trying to solve is:
Idea is to store the time that we last made a QPS based sampling decision in an atomic. Then we can use the elapsed time Z since the coin flip to weight our current coin flip. We choose our probability function P(Z) such that we get the desired sample QPS. We want P(Z) to be very cheap to compute.
Let X be the desired QPS. Let Z be the elapsed time since the last sampling decision in seconds.
P(Z) = min(Z * X, 1)
To see that this is approximately correct, consider the case where we have perfectly distributed time intervals. Specifically, let X = 1 and Z = 1/N. Then we would have N coin flips per second, each with probability 1/N, for an expectation of 1 sample per second.
This will under-sample: consider the case where X = 1 and Z alternates between 0.5 and 1.5. It is possible to get about 1 QPS by always sampling, but this algorithm only gets 0.75 QPS.