Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 799aaaf

Browse files
author
Bogdan Drutu
authored
Add details about supporting qps based sampler. (#158)
* Add details about supporting qps based sampler. * Rename the qps to RateLimiting
1 parent 800b084 commit 799aaaf

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

trace/Sampling.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ The Sampling bit is always set only at the start of a Span, using a `Sampler`
1212
* `AlwaysSample` - sampler that makes a "yes" decision every time.
1313
* `NeverSample` - sampler that makes a "no" decision every time.
1414
* `Probability` - sampler that tries to uniformly sample traces with a given probability. When
15-
applied to a child `Span` of a **sampled** parent `Span`, the child `Span` keeps the sampling decision.
15+
applied to a child `Span` of a **sampled** parent `Span`, the child `Span` keeps the sampling
16+
decision.
17+
* `RateLimiting` - sampler that tries to sample with a rate per time window (0.1 traces/second).
18+
When applied to a child `Span` of a **sampled** parent `Span`, the child `Span` keeps the sampling
19+
decision. For implementation details see [this](#ratelimiting-sampler-implementation-details)
1620

1721
### How can users control the Sampler that is used for sampling?
1822
There are 2 ways to control the `Sampler` used when the library samples:
@@ -33,3 +37,26 @@ The OpenCensus library samples based on the following rules:
3337
3. If the span is a child of a local `Span` the sampling decision will be:
3438
* If a "span-scoped" `Sampler` is provided, use it to determine the sampling decision.
3539
* Else keep the sampling decision from the parent.
40+
41+
### RateLimiting sampler implementation details
42+
The problem we are trying to solve is:
43+
1. Getting QPS based sampling.
44+
2. Providing real sampling probabilities.
45+
3. Minimal overhead.
46+
47+
Idea is to store the time that we last made a QPS based sampling decision in an atomic. Then we can
48+
use the elapsed time Z since the coin flip to weight our current coin flip. We choose our
49+
probability function P(Z) such that we get the desired sample QPS. We want P(Z) to be very
50+
cheap to compute.
51+
52+
Let X be the desired QPS. Let Z be the elapsed time since the last sampling decision in seconds.
53+
```
54+
P(Z) = min(Z * X, 1)
55+
```
56+
57+
To see that this is approximately correct, consider the case where we have perfectly distributed
58+
time intervals. Specifically, let X = 1 and Z = 1/N. Then we would have N coin flips per second,
59+
each with probability 1/N, for an expectation of 1 sample per second.
60+
61+
This will under-sample: consider the case where X = 1 and Z alternates between 0.5 and 1.5. It is
62+
possible to get about 1 QPS by always sampling, but this algorithm only gets 0.75 QPS.

0 commit comments

Comments
 (0)