forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampler_test.go
104 lines (91 loc) · 2.98 KB
/
sampler_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
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package apm_test
import (
"encoding/binary"
"math/rand"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"go.elastic.co/apm/v2"
)
func TestRatioSampler(t *testing.T) {
ratio := 0.75
s := apm.NewRatioSampler(ratio)
const (
numGoroutines = 100
numIterations = 1000
)
sampled := make([]int, numGoroutines)
var wg sync.WaitGroup
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
// fixed seed to avoid intermittent failures
rng := rand.New(rand.NewSource(int64(i)))
for j := 0; j < numIterations; j++ {
var traceContext apm.TraceContext
binary.LittleEndian.PutUint64(traceContext.Span[:], rng.Uint64())
result := s.Sample(apm.SampleParams{TraceContext: traceContext})
assert.Equal(t, ratio, result.SampleRate)
if result.Sampled {
sampled[i]++
}
}
}(i)
}
wg.Wait()
var total int
for i := 0; i < numGoroutines; i++ {
total += sampled[i]
}
assert.InDelta(t, ratio, float64(total)/(numGoroutines*numIterations), 0.1)
}
func TestRatioSamplerAlways(t *testing.T) {
s := apm.NewRatioSampler(1.0)
assert.False(t, s.Sample(apm.SampleParams{TraceContext: apm.TraceContext{}}).Sampled) // invalid span ID
assert.True(t, s.Sample(apm.SampleParams{TraceContext: apm.TraceContext{
Span: apm.SpanID{0, 0, 0, 0, 0, 0, 0, 1},
}}).Sampled)
assert.True(t, s.Sample(apm.SampleParams{TraceContext: apm.TraceContext{
Span: apm.SpanID{255, 255, 255, 255, 255, 255, 255, 255},
}}).Sampled)
}
func TestRatioSamplerNever(t *testing.T) {
s := apm.NewRatioSampler(0)
assert.False(t, s.Sample(apm.SampleParams{TraceContext: apm.TraceContext{}}).Sampled) // invalid span ID
assert.False(t, s.Sample(apm.SampleParams{TraceContext: apm.TraceContext{
Span: apm.SpanID{0, 0, 0, 0, 0, 0, 0, 1},
}}).Sampled)
assert.False(t, s.Sample(apm.SampleParams{TraceContext: apm.TraceContext{
Span: apm.SpanID{255, 255, 255, 255, 255, 255, 255, 255},
}}).Sampled)
}
func TestRatioSamplerPrecision(t *testing.T) {
ratios := map[float64]float64{
0.00001: 0.0001,
0.55554: 0.5555,
0.55555: 0.5556,
0.55556: 0.5556,
}
for r, want := range ratios {
s := apm.NewRatioSampler(r)
got := s.Sample(apm.SampleParams{}).SampleRate
assert.Equal(t, want, got)
}
}