forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsampler_mode_test.go
72 lines (62 loc) · 1.47 KB
/
sampler_mode_test.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package probabilisticsamplerprocessor
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var AllModes = []SamplerMode{HashSeed, Equalizing, Proportional}
func TestUnmarshalText(t *testing.T) {
tests := []struct {
samplerMode string
shouldError bool
}{
{
samplerMode: "hash_seed",
},
{
samplerMode: "equalizing",
},
{
samplerMode: "proportional",
},
{
samplerMode: "",
},
{
samplerMode: "dunno",
shouldError: true,
},
}
for _, tt := range tests {
t.Run(tt.samplerMode, func(t *testing.T) {
temp := modeUnset
err := temp.UnmarshalText([]byte(tt.samplerMode))
if tt.shouldError {
assert.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, temp, SamplerMode(tt.samplerMode))
})
}
}
func TestHashSeedRoundingDown(t *testing.T) {
// The original hash function rounded thresholds down, in the
// direction of zero.
// pct is approximately 75% of the minimum 14-bit probability, so it
// would round up, but it does not.
const pct = 0x3p-16 * 100
require.Equal(t, 1.0, math.Round((pct/100)*numHashBuckets))
for _, isLogs := range []bool{false, true} {
cfg := Config{
Mode: HashSeed,
SamplingPercentage: pct,
HashSeed: defaultHashSeed,
}
_, ok := makeSampler(&cfg, isLogs).(*neverSampler)
require.True(t, ok, "is neverSampler")
}
}