-
Notifications
You must be signed in to change notification settings - Fork 91
/
sampling.go
298 lines (262 loc) · 8.6 KB
/
sampling.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package auto
import (
"errors"
"strconv"
"strings"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe/sampling"
)
// Sampler decides whether a trace should be sampled and exported.
type Sampler interface {
validate() error
convert() (*sampling.Config, error)
}
// OpenTelemetry spec-defined sampler names and environment variables for configuration.
const (
tracesSamplerKey = "OTEL_TRACES_SAMPLER"
tracesSamplerArgKey = "OTEL_TRACES_SAMPLER_ARG"
samplerNameAlwaysOn = "always_on"
samplerNameAlwaysOff = "always_off"
samplerNameTraceIDRatio = "traceidratio"
samplerNameParentBasedAlwaysOn = "parentbased_always_on"
samplerNameParsedBasedAlwaysOff = "parentbased_always_off"
samplerNameParentBasedTraceIDRatio = "parentbased_traceidratio"
)
// AlwaysOnSampler is a Sampler that samples every trace.
// Be careful about using this sampler in a production application with
// significant traffic: a new trace will be started and exported for every
// request.
type AlwaysOnSampler struct{}
var _ Sampler = AlwaysOnSampler{}
func (AlwaysOnSampler) validate() error {
return nil
}
func (AlwaysOnSampler) convert() (*sampling.Config, error) {
return &sampling.Config{
Samplers: map[sampling.SamplerID]sampling.SamplerConfig{
sampling.AlwaysOnID: {
SamplerType: sampling.SamplerAlwaysOn,
},
},
ActiveSampler: sampling.AlwaysOnID,
}, nil
}
// AlwaysOffSampler returns a Sampler that samples no traces.
type AlwaysOffSampler struct{}
var _ Sampler = AlwaysOffSampler{}
func (AlwaysOffSampler) validate() error {
return nil
}
func (AlwaysOffSampler) convert() (*sampling.Config, error) {
return &sampling.Config{
Samplers: map[sampling.SamplerID]sampling.SamplerConfig{
sampling.AlwaysOffID: {
SamplerType: sampling.SamplerAlwaysOff,
},
},
ActiveSampler: sampling.AlwaysOffID,
}, nil
}
// TraceIDRatioSampler samples a given fraction of traces. Fraction should be in the closed interval [0, 1].
// To respect the parent trace's SampledFlag, the TraceIDRatioSampler sampler should be used
// as a delegate of a [ParentBased] sampler.
type TraceIDRatioSampler struct {
// Fraction is the fraction of traces to sample. This value needs to be in the interval [0, 1].
Fraction float64
}
var _ Sampler = TraceIDRatioSampler{}
func (t TraceIDRatioSampler) validate() error {
if t.Fraction < 0 || t.Fraction > 1 {
return errors.New("fraction in TraceIDRatio must be in the range [0, 1]")
}
return nil
}
func (t TraceIDRatioSampler) convert() (*sampling.Config, error) {
tidConfig, err := sampling.NewTraceIDRatioConfig(t.Fraction)
if err != nil {
return nil, err
}
return &sampling.Config{
Samplers: map[sampling.SamplerID]sampling.SamplerConfig{
sampling.TraceIDRatioID: {
SamplerType: sampling.SamplerTraceIDRatio,
Config: tidConfig,
},
},
ActiveSampler: sampling.TraceIDRatioID,
}, nil
}
// ParentBasedSampler is a [Sampler] which behaves differently,
// based on the parent of the span. If the span has no parent,
// the Root sampler is used to make sampling decision. If the span has
// a parent, depending on whether the parent is remote and whether it
// is sampled, one of the following samplers will apply:
// - RemoteSampled (default: [AlwaysOn])
// - RemoteNotSampled (default: [AlwaysOff])
// - LocalSampled (default: [AlwaysOn])
// - LocalNotSampled (default: [AlwaysOff])
type ParentBasedSampler struct {
// Root is the Sampler used when a span is created without a parent.
Root Sampler
// RemoteSampled is the Sampler used when the span parent is remote and sampled.
RemoteSampled Sampler
// RemoteNotSampled is the Sampler used when the span parent is remote and not sampled.
RemoteNotSampled Sampler
// LocalSampled is the Sampler used when the span parent is local and sampled.
LocalSampled Sampler
// LocalNotSampled is the Sampler used when the span parent is local and not sampled.
LocalNotSampled Sampler
}
var _ Sampler = ParentBasedSampler{}
func validateParentBasedComponent(s Sampler) error {
if s == nil {
return nil
}
if _, ok := s.(ParentBasedSampler); ok {
return errors.New("parent-based sampler cannot wrap parent-based sampler")
}
return s.validate()
}
func (p ParentBasedSampler) validate() error {
var err error
return errors.Join(err,
validateParentBasedComponent(p.LocalNotSampled),
validateParentBasedComponent(p.LocalSampled),
validateParentBasedComponent(p.RemoteNotSampled),
validateParentBasedComponent(p.RemoteSampled),
validateParentBasedComponent(p.Root))
}
func (p ParentBasedSampler) convert() (*sampling.Config, error) {
pbc := sampling.DefaultParentBasedSampler()
samplers := make(map[sampling.SamplerID]sampling.SamplerConfig)
rootSampler, err := convertSamplerToConfig(p.Root)
if err != nil {
return nil, err
}
if rootSampler != nil {
pbc.Root = rootSampler.ActiveSampler
for id, config := range rootSampler.Samplers {
samplers[id] = config
}
}
remoteSampledSampler, err := convertSamplerToConfig(p.RemoteSampled)
if err != nil {
return nil, err
}
if remoteSampledSampler != nil {
pbc.RemoteSampled = remoteSampledSampler.ActiveSampler
for id, config := range remoteSampledSampler.Samplers {
samplers[id] = config
}
}
remoteNotSampledSampler, err := convertSamplerToConfig(p.RemoteNotSampled)
if err != nil {
return nil, err
}
if remoteNotSampledSampler != nil {
pbc.RemoteNotSampled = remoteNotSampledSampler.ActiveSampler
for id, config := range remoteNotSampledSampler.Samplers {
samplers[id] = config
}
}
localSampledSamplers, err := convertSamplerToConfig(p.LocalSampled)
if err != nil {
return nil, err
}
if localSampledSamplers != nil {
pbc.LocalSampled = localSampledSamplers.ActiveSampler
for id, config := range localSampledSamplers.Samplers {
samplers[id] = config
}
}
localNotSampledSampler, err := convertSamplerToConfig(p.LocalNotSampled)
if err != nil {
return nil, err
}
if localNotSampledSampler != nil {
pbc.LocalNotSampled = localNotSampledSampler.ActiveSampler
for id, config := range localNotSampledSampler.Samplers {
samplers[id] = config
}
}
samplers[sampling.ParentBasedID] = sampling.SamplerConfig{
SamplerType: sampling.SamplerParentBased,
Config: pbc,
}
return &sampling.Config{
Samplers: samplers,
ActiveSampler: sampling.ParentBasedID,
}, nil
}
// DefaultSampler returns a ParentBased sampler with the following defaults:
// - Root: AlwaysOn
// - RemoteSampled: AlwaysOn
// - RemoteNotSampled: AlwaysOff
// - LocalSampled: AlwaysOn
// - LocalNotSampled: AlwaysOff
func DefaultSampler() Sampler {
return ParentBasedSampler{
Root: AlwaysOnSampler{},
RemoteSampled: AlwaysOnSampler{},
RemoteNotSampled: AlwaysOffSampler{},
LocalSampled: AlwaysOnSampler{},
LocalNotSampled: AlwaysOffSampler{},
}
}
// newSamplerFromEnv creates a Sampler based on the environment variables.
// If the environment variables are not set, it returns a nil Sampler.
func newSamplerFromEnv(lookupEnv func(string) (string, bool)) (Sampler, error) {
samplerName, ok := lookupEnv(tracesSamplerKey)
if !ok {
return nil, nil
}
defaultSampler := DefaultSampler().(ParentBasedSampler)
samplerName = strings.ToLower(strings.TrimSpace(samplerName))
samplerArg, hasSamplerArg := lookupEnv(tracesSamplerArgKey)
samplerArg = strings.TrimSpace(samplerArg)
switch samplerName {
case samplerNameAlwaysOn:
return AlwaysOnSampler{}, nil
case samplerNameAlwaysOff:
return AlwaysOffSampler{}, nil
case samplerNameTraceIDRatio:
if hasSamplerArg {
ratio, err := strconv.ParseFloat(samplerArg, 64)
if err != nil {
return nil, err
}
return TraceIDRatioSampler{Fraction: ratio}, nil
}
return TraceIDRatioSampler{Fraction: 1}, nil
case samplerNameParentBasedAlwaysOn:
defaultSampler.Root = AlwaysOnSampler{}
return defaultSampler, nil
case samplerNameParsedBasedAlwaysOff:
defaultSampler.Root = AlwaysOffSampler{}
return defaultSampler, nil
case samplerNameParentBasedTraceIDRatio:
if !hasSamplerArg {
defaultSampler.Root = TraceIDRatioSampler{Fraction: 1}
return defaultSampler, nil
}
ratio, err := strconv.ParseFloat(samplerArg, 64)
if err != nil {
return nil, err
}
defaultSampler.Root = TraceIDRatioSampler{Fraction: ratio}
return defaultSampler, nil
default:
return nil, errors.New("unknown sampler name")
}
}
// convertSamplerToConfig converts a Sampler its internal representation.
func convertSamplerToConfig(s Sampler) (*sampling.Config, error) {
if s == nil {
return nil, nil
}
if err := s.validate(); err != nil {
return nil, err
}
return s.convert()
}