-
Notifications
You must be signed in to change notification settings - Fork 18
/
sampler.go
38 lines (30 loc) · 986 Bytes
/
sampler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package otel
import (
"hash/crc32"
"math"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
type deterministicSampler struct {
sampleKeyFunc func(map[string]any) string
sampleRates map[string]int
}
// shouldSample means should sample in, returning true if the span should be sampled in (kept)
func (s deterministicSampler) shouldSample(p sdktrace.ReadOnlySpan) bool {
fields := map[string]any{}
for _, attr := range p.Attributes() {
fields[string(attr.Key)] = attr.Value.AsInterface()
}
fields["name"] = p.Name()
key := s.sampleKeyFunc(fields)
rate, ok := s.sampleRates[key] // no rate found means keep
return !ok || shouldKeep(p.SpanContext().SpanID().String(), rate)
}
// shouldKeep deterministically decides whether to sample. True means keep, false means drop
func shouldKeep(determinant string, rate int) bool {
if rate == 1 {
return true
}
threshold := math.MaxUint32 / uint32(rate)
v := crc32.ChecksumIEEE([]byte(determinant))
return v < threshold
}