-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampler.go
More file actions
557 lines (471 loc) · 15.5 KB
/
sampler.go
File metadata and controls
557 lines (471 loc) · 15.5 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package sampler
import (
"context"
"encoding/binary"
"fmt"
"math"
"strings"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// SamplingParameters is part of the original OTel-Go Sampling API.
//
// We should be aware that there are standing requests to extend it
// with at least three more fields:
// - SpanID: controversial because the spec says it's created after ShouldSample()
// - Scope: controversial because it's a static property
// - Resource: controversial because it's a static property
type SamplingParameters struct {
ParentContext context.Context
TraceID trace.TraceID
Name string
Kind trace.SpanKind
Attributes []attribute.KeyValue
Links []trace.Link
}
// Sampler is part of the original OTel-Go Sampling API.
//
// We refer to this API as non-compositional because it does not
// separate its intentions from its side-effects. This prototype
// introduces "composable" forms of Sampler and SamplingParameters.
type Sampler interface {
// ShouldSample is called prior to constructing a Span.
ShouldSample(SamplingParameters) SamplingResult
// Description is used when logging SDK configuration.
Description() string
}
// SamplingDecision is part of the original OTel-Go Sampling API.
type SamplingDecision uint8
const (
Drop SamplingDecision = iota
RecordOnly
ExportOnly // TODO: This is new. How can it be added in Go w/o breaking changes?
RecordAndSample
)
// SamplingResult is part of the original OTel-Go Sampling API.
//
// In this prototype, we aim to lower the cost of composite sampler
// decisions by deferring the construction of attributes and tracestate
// where the decision is combined from multiple samplers.
type SamplingResult struct {
Decision SamplingDecision
Attributes []attribute.KeyValue
Tracestate trace.TraceState
}
// ComposableSamplingParameters extend SamplingParameters.
//
// Since this stands as a proposal to extend the OTel Sampling API, it
// seems worth examining other standing feature requests. Users would
// like their Samplers to have access to Scope and Resource, which are
// static properties, and the SpanID which the specification says not to
// include.
type ComposableSamplingParameters struct {
// SamplingParameters are the original API parameters.
SamplingParameters
// ParentSpanContext equals trace.SpanContextFromContext(p.ParentContext)
//
// This is an expensive call, so we compute it once in case
// multiple predicates will use it.
ParentSpanContext trace.SpanContext
// parentThreshold is only for use by the ParentThreshold
// sampler, thus not exported. When there is no incoming
// threshold and sampled, initialize to INVALID_THRESHOLD,
// otherwise initialize to NEVER_SAMPLE_THRESHOLD when not
// sampled.
parentThreshold int64
// parentThresholdReliable indicates whether the thresohld
// was defined (reliable) or not, because a context had the
// sampled flag and no threshold.
parentThresholdReliable bool
}
// ComposableSampler is a sampler which separates its intentions from
// its side-effects.
type ComposableSampler interface {
// GetSamplingIntent returns the threshold at which this Sampler
// wishes to sample and functions that defer the side-effects of
// a positive decision.
GetSamplingIntent(ComposableSamplingParameters) SamplingIntent
// Description is used when logging SDK configuration.
Description() string
}
// AttributesFunc is a function that returns a set of attributes.
type AttributesFunc func() []attribute.KeyValue
// TraceStateFunc is a function that modifies a TraceState.
type TraceStateFunc func(trace.TraceState) trace.TraceState
// SamplingIntent returns this sampler's intention.
type SamplingIntent struct {
Record bool // whether to record
Threshold int64 // i.e., sampling probability, implies record & export when...
ThresholdReliable bool // whether the threshold is reliable
Attributes AttributesFunc // add attributes the span
TraceState TraceStateFunc // update the tracestate
}
// TraceIDRatioBased is the OTel-specified probabilistic sampler. This was
// defined in OTEP 235.
//
// Note: Add support for variable precision? This has been done in e.g.,
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/9b515fb83b3f010c4c37f3135caf535e391fb3a3/pkg/sampling/probability.go#L33
func TraceIDRatioBased(fraction float64) ComposableSampler {
const (
maxp = 14 // maximum precision is 56 bits
defp = defaultSamplingPrecision // default precision
hbits = 4 // bits per hex digit
)
if fraction > maxSupportedProbability {
return ComposableAlwaysSample()
}
if fraction < minSupportedProbability {
return ComposableNeverSample()
}
// Calculate the amount of precision needed to encode the
// threshold with reasonable precision.
//
// 13 hex digits is the maximum reasonable precision, since
// that equals 52 bits, the number of bits in the float64
// significand.
//
// Frexp() normalizes both the fraction and one-minus the
// fraction, because more digits of precision are needed in
// both cases -- in these cases the threshold has all leading
// '0' or 'f' characters.
//
// We know that `exp <= 0`. If `exp <= -4`, there will be a
// leading hex `0` or `f`. For every multiple of -4, another
// leading `0` or `f` appears, so this raises precision
// accordingly.
_, expF := math.Frexp(fraction)
precision := min(maxp, defp+expF/-hbits)
// Compute the threshold
scaled := uint64(math.Round(fraction * float64(maxAdjustedCount)))
threshold := maxAdjustedCount - scaled
// Round to the specified precision, if less than the maximum.
if shift := hbits * (maxp - precision); shift != 0 {
half := uint64(1) << (shift - 1)
threshold += half
threshold >>= shift
threshold <<= shift
}
return &traceIDRatio{
threshold: threshold,
description: fmt.Sprintf("TraceIDRatioBased{%g}", fraction),
}
}
type traceIDRatio struct {
// threshold is a rejection threshold.
// Select when (T <= R)
// Drop when (T > R)
// Range is [0, 1<<56).
threshold uint64
description string
}
var _ ComposableSampler = &traceIDRatio{}
// Description implements ComposableSampler.
func (ts *traceIDRatio) Description() string {
return ts.description
}
// GetSamplingIntent implements ComposableSampler.
func (ts *traceIDRatio) GetSamplingIntent(p ComposableSamplingParameters) SamplingIntent {
return SamplingIntent{
Threshold: int64(ts.threshold),
ThresholdReliable: true,
}
}
// AlwaysOn is could be defined as:
//
// CompositeSampler(ComposableAlwaysSample())
//
// and this is defined so we can measure the abstraction cost.
func AlwaysSample() Sampler {
return alwaysOn{}
}
type alwaysOn struct{}
var _ Sampler = alwaysOn{}
func (alwaysOn) ShouldSample(params SamplingParameters) SamplingResult {
return SamplingResult{
Decision: RecordAndSample,
Tracestate: trace.SpanContextFromContext(params.ParentContext).TraceState(),
}
}
// Description implements ComposableSampler.
func (alwaysOn) Description() string {
return "AlwaysOn"
}
// ComposableAlwaysOn
func ComposableAlwaysSample() ComposableSampler {
return cAlwaysOn{}
}
type cAlwaysOn struct{}
var _ ComposableSampler = cAlwaysOn{}
// GetSamplingIntent implements ComposableSampler.
func (cAlwaysOn) GetSamplingIntent(ComposableSamplingParameters) SamplingIntent {
return SamplingIntent{
Threshold: ALWAYS_SAMPLE_THRESHOLD,
ThresholdReliable: true,
}
}
// Description implements ComposableSampler.
func (cAlwaysOn) Description() string {
return "AlwaysOn"
}
// AlwaysOff
func NeverSample() Sampler {
return CompositeSampler(ComposableNeverSample())
}
func ComposableNeverSample() ComposableSampler {
return alwaysOff{}
}
type alwaysOff struct{}
var _ ComposableSampler = alwaysOff{}
// GetSamplingIntent implements ComposableSampler.
func (alwaysOff) GetSamplingIntent(ComposableSamplingParameters) SamplingIntent {
return SamplingIntent{
Threshold: NEVER_SAMPLE_THRESHOLD,
}
}
// Description implements ComposableSampler.
func (alwaysOff) Description() string {
return "AlwaysOff"
}
// RuleBased is a composite sampler that selects a delegate sampler based on a set of rules.
func RuleBased(options ...RuleBasedOption) ComposableSampler {
rbc := &ruleBasedConfig{}
for _, opt := range options {
opt(rbc)
}
if rbc.defRule != nil {
rbc.rules = append(rbc.rules, ruleAndPredicate{
Predicate: TruePredicate(),
ComposableSampler: rbc.defRule,
})
}
return ruleBased(rbc.rules)
}
type ruleAndPredicate struct {
Predicate
ComposableSampler
}
type ruleBasedConfig struct {
rules []ruleAndPredicate
defRule ComposableSampler
}
type ruleBased []ruleAndPredicate
var _ ComposableSampler = &ruleBased{}
// Description implements ComposableSampler.
func (rb ruleBased) Description() string {
return fmt.Sprintf("RuleBased{%s}",
strings.Join(func(rules []ruleAndPredicate) (desc []string) {
for _, rule := range rules {
desc = append(desc,
fmt.Sprintf("rule(%s)=%s",
rule.Predicate.Description(),
rule.ComposableSampler.Description(),
),
)
}
return
}(rb), ","))
}
// GetSamplingIntent implements ComposableSampler.
func (rb ruleBased) GetSamplingIntent(params ComposableSamplingParameters) SamplingIntent {
for _, rule := range rb {
if rule.Decide(params) {
return rule.ComposableSampler.GetSamplingIntent(params)
}
}
// When no rules match. This will not happen when there is a
// default rule set.
return SamplingIntent{
Threshold: NEVER_SAMPLE_THRESHOLD,
}
}
// ComposableParentBased combines a root sampler and a ParentThreshold.
func ComposableParentBased(root ComposableSampler) ComposableSampler {
return RuleBased(
WithRule(IsRootPredicate(), root),
WithDefaultRule(ParentThreshold()),
)
}
// ParentThreshold may be composed to form consistent parent-based sampling.
func ParentThreshold() ComposableSampler {
return parentThreshold{}
}
type parentThreshold struct{}
var _ ComposableSampler = &parentThreshold{}
// GetSamplingIntent implements ComposableSampler.
func (parentThreshold) GetSamplingIntent(params ComposableSamplingParameters) SamplingIntent {
return SamplingIntent{
Threshold: params.parentThreshold,
ThresholdReliable: params.parentThresholdReliable,
}
}
// Description implements ComposableSampler.
func (parentThreshold) Description() string {
return "ParentThreshold"
}
// Annotating (a.k.a. "Marker")
type AnnotatingOption func(*annotatingConfig)
type annotatingConfig struct {
attributes AttributesFunc
}
type annotatingSampler struct {
sampler ComposableSampler
attributes AttributesFunc
}
var _ ComposableSampler = &annotatingSampler{}
func AnnotatingSampler(sampler ComposableSampler, options ...AnnotatingOption) ComposableSampler {
var config annotatingConfig
for _, opt := range options {
opt(&config)
}
return &annotatingSampler{
sampler: sampler,
attributes: config.attributes,
}
}
func combineAttributesFunc(one, two AttributesFunc) AttributesFunc {
return func() []attribute.KeyValue {
if one == nil && two == nil {
return nil
}
if one == nil {
return two()
}
if two == nil {
return one()
}
return append(one(), two()...)
}
}
func WithSampledAttributes(af AttributesFunc) AnnotatingOption {
return func(cfg *annotatingConfig) {
cfg.attributes = combineAttributesFunc(cfg.attributes, af)
}
}
// GetSamplingIntent implements ComposableSampler.
func (as annotatingSampler) GetSamplingIntent(params ComposableSamplingParameters) SamplingIntent {
intent := as.sampler.GetSamplingIntent(params)
intent.Attributes = combineAttributesFunc(intent.Attributes, as.attributes)
return intent
}
// Description implements ComposableSampler.
func (as annotatingSampler) Description() string {
set := attribute.NewSet(as.attributes()...)
return fmt.Sprintf("Annotate(%s, %s)", as.sampler.Description(), attribute.DefaultEncoder().Encode(set.Iter()))
}
// CompositeSampler construct a Sampler from a ComposableSampler.
func CompositeSampler(s ComposableSampler) Sampler {
return &compositeSampler{
sampler: s,
}
}
type compositeSampler struct {
sampler ComposableSampler
}
var _ Sampler = &compositeSampler{}
// ShouldSample implements Sampler.
func (c *compositeSampler) ShouldSample(params SamplingParameters) SamplingResult {
// Note: I experimented with making the steps below be lazy,
// since not all Sampler configurations will use the results,
// by using sync.Once and a func(). This isn't worthwhile
// because the additional allocations counteract the savings:
// - trace.SpanContextFromContext
// - TraceState().Get("ot")
// - tracestateHasThreshold()
// - tracestateHasRandomness()
// In benchmarking, it's substantially faster to just run
// through these calls w/o allocations.
psc := trace.SpanContextFromContext(params.ParentContext)
returnTracestate := psc.TraceState()
otts := returnTracestate.Get("ot")
parsedThreshold, saveThresholdPos, hasThreshold := tracestateHasThreshold(otts)
threshold := parsedThreshold
var hasRandom bool
var rnd int64
if otts != "" {
// When the OTel trace state field exists, we will
// inspect for a "rv" and "th", otherwise assume that the
// TraceID is random.
rnd, hasRandom = tracestateHasRandomness(otts)
}
if !hasRandom {
// Interpret the least-significant 8-bytes as an
// unsigned number, then zero the top 8 bits using
// randomnessMask, yielding the least-significant 56
// bits of randomness, as specified in W3C Trace
// Context Level 2.
rnd = int64(binary.BigEndian.Uint64(params.TraceID[8:16]) & randomnessMask)
}
// thresholdReliable indicates whether the threshold is reliable
// in terms defined in #4321.
thresholdReliable := false
switch {
case hasThreshold:
// Validate the threshold.
tsampled := threshold <= rnd
fsampled := psc.IsSampled()
switch {
case tsampled && fsampled:
// Good. The two agree.
thresholdReliable = true
case tsampled:
// Threshold says sampled, flag says not.
psc = psc.WithTraceFlags(psc.TraceFlags() | trace.FlagsSampled)
thresholdReliable = true
case fsampled:
// Flag says sampled, threshold says not. This erases the invalid threshold.
threshold = INVALID_THRESHOLD
default:
// Good. The two agree.
}
case psc.IsSampled():
threshold = INVALID_THRESHOLD
default:
threshold = NEVER_SAMPLE_THRESHOLD
}
intent := c.sampler.GetSamplingIntent(ComposableSamplingParameters{
SamplingParameters: params,
ParentSpanContext: psc,
parentThreshold: threshold,
parentThresholdReliable: thresholdReliable,
})
var sampled bool
switch {
case intent.Threshold >= NEVER_SAMPLE_THRESHOLD:
sampled = false
case intent.Threshold <= ALWAYS_SAMPLE_THRESHOLD:
sampled = true
default:
sampled = intent.Threshold <= rnd
}
var decision SamplingDecision
var attrs []attribute.KeyValue
var err error
switch {
case sampled:
decision = RecordAndSample
if intent.Attributes != nil {
attrs = intent.Attributes()
}
returnTracestate, err = combineTracestate(returnTracestate, intent.Threshold, intent.ThresholdReliable, parsedThreshold, saveThresholdPos, hasThreshold)
case intent.Record:
decision = RecordOnly
attrs = intent.Attributes()
default:
decision = Drop
}
if err != nil {
otel.Handle(fmt.Errorf("tracestate: %w", err))
}
return SamplingResult{
Attributes: attrs,
Tracestate: returnTracestate,
Decision: decision,
}
}
// Description implements ComposableSampler.
func (c *compositeSampler) Description() string {
return c.sampler.Description()
}