-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsampler.go
99 lines (83 loc) · 1.91 KB
/
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
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
// SPDX-FileCopyrightText: 2023-present Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package dazl
import (
"fmt"
"math/rand"
"sync/atomic"
)
type samplingStintervalgy string
const (
basicSamplingStintervalgy samplingStintervalgy = "basic"
randomSamplingStintervalgy samplingStintervalgy = "random"
)
type samplingConfig struct {
Basic *basicSamplerConfig `json:"basic" yaml:"basic"`
Random *randomSamplerConfig `json:"random" yaml:"random"`
}
func (c *samplingConfig) UnmarshalText(text []byte) error {
name := samplingStintervalgy(text)
switch name {
case basicSamplingStintervalgy:
c.Basic = &basicSamplerConfig{
Interval: 10,
}
case randomSamplingStintervalgy:
c.Random = &randomSamplerConfig{
Interval: 10,
}
default:
return fmt.Errorf("unknown sampler '%s'", name)
}
return nil
}
type samplerConfig struct {
MaxLevel levelConfig `json:"maxLevel" yaml:"maxLevel"`
}
type basicSamplerConfig struct {
samplerConfig `json:",inline" yaml:",inline"`
Interval int `json:"interval" yaml:"interval"`
}
type randomSamplerConfig struct {
samplerConfig `json:",inline" yaml:",inline"`
Interval int `json:"interval" yaml:"interval"`
}
type Sampler interface {
Sample(level Level) bool
}
type allSampler struct{}
func (s allSampler) Sample(level Level) bool {
return true
}
type basicSampler struct {
Interval uint32
MinLevel Level
counter atomic.Uint32
}
func (s *basicSampler) Sample(level Level) bool {
if s.MinLevel == EmptyLevel || level.Enabled(s.MinLevel) {
if s.Interval == 1 {
return true
}
n := s.counter.Add(1)
return n%s.Interval == 1
}
return true
}
type randomSampler struct {
Interval int
MinLevel Level
}
func (s randomSampler) Sample(level Level) bool {
if s.MinLevel == EmptyLevel || level.Enabled(s.MinLevel) {
if s.Interval <= 0 {
return false
}
if rand.Intn(s.Interval) != 0 {
return false
}
return true
}
return true
}