This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 288
/
sampler_v2_test.go
225 lines (187 loc) Β· 8.43 KB
/
sampler_v2_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
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
// Copyright (c) 2019 The Jaeger Authors.
//
// Licensed 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 jaeger
import (
"fmt"
"testing"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
_ Sampler = new(SamplerV2Base)
_ SamplerV2 = new(ConstSampler)
_ SamplerV2 = new(ProbabilisticSampler)
_ SamplerV2 = new(RateLimitingSampler)
_ SamplerV2 = new(PerOperationSampler)
// Note: GuaranteedThroughputProbabilisticSampler is currently not V2
_ Sampler = new(retryableTestSampler)
_ SamplerV2 = new(retryableTestSampler)
)
// retryableTestSampler leaves the sampling unfinalized (retryable) after OnCreateSpan,
// and makes it final after OnSetOperationName.
type retryableTestSampler struct {
SamplerV2Base
decision bool
tags []Tag
}
func newRetryableSampler(decision bool) *retryableTestSampler {
return &retryableTestSampler{
decision: decision,
tags: makeSamplerTags("retryable", decision),
}
}
func (s *retryableTestSampler) OnCreateSpan(span *Span) SamplingDecision {
return SamplingDecision{Sample: s.decision, Retryable: true, Tags: s.tags}
}
func (s *retryableTestSampler) OnSetOperationName(span *Span, operationName string) SamplingDecision {
return SamplingDecision{Sample: s.decision, Retryable: false, Tags: s.tags}
}
func (s *retryableTestSampler) OnSetTag(span *Span, key string, value interface{}) SamplingDecision {
return SamplingDecision{Sample: s.decision, Retryable: true, Tags: s.tags}
}
func (s *retryableTestSampler) OnFinishSpan(span *Span) SamplingDecision {
return SamplingDecision{Sample: s.decision, Retryable: true, Tags: s.tags}
}
func TestSpanRemainsWriteable(t *testing.T) {
tracer, closer := NewTracer("test-service", newRetryableSampler(false), NewNullReporter())
defer closer.Close()
span := tracer.StartSpan("op1").(*Span)
assert.True(t, span.context.isWriteable(), "span is writeable when created")
assert.False(t, span.context.isSamplingFinalized(), "span is not finalized when created")
span.SetTag("k1", "v1")
assert.True(t, span.context.isWriteable(), "span is writeable after setting tags")
assert.Equal(t, opentracing.Tags{"k1": "v1"}, span.Tags())
span.SetOperationName("op2")
assert.False(t, span.context.isWriteable(), "span is not writeable after sampler returns retryable=true")
assert.True(t, span.context.isSamplingFinalized(), "span is finalized after sampler returns retryable=true")
}
func TestSpanSharesSamplingStateWithChildren(t *testing.T) {
tracer, closer := NewTracer("test-service", newRetryableSampler(false), NewNullReporter())
defer closer.Close()
sp1 := tracer.StartSpan("op1").(*Span)
assert.False(t, sp1.context.isSamplingFinalized(), "span is not finalized when created")
sp2 := tracer.StartSpan("op2", opentracing.ChildOf(sp1.Context())).(*Span)
assert.False(t, sp2.context.isSamplingFinalized(), "child span is not finalized when created")
sp2.SetOperationName("op3")
assert.True(t, sp2.context.isSamplingFinalized(), "child span is finalized after changing operation name")
assert.True(t, sp1.context.isSamplingFinalized(), "parent span is also finalized after child is finalized")
}
func TestSamplingIsFinalizedOnSamplingPriorityTag(t *testing.T) {
tracer, closer := NewTracer("test-service", newRetryableSampler(false), NewNullReporter())
defer closer.Close()
tests := []struct {
name string
priority uint16
expectedSampled bool
expectedTags opentracing.Tags
}{
{
name: "sampling.priority=1",
priority: 1,
expectedSampled: true,
expectedTags: opentracing.Tags{"sampling.priority": uint16(1)},
},
{
name: "sampling.priority=0",
expectedSampled: false,
priority: 0,
expectedTags: opentracing.Tags{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
span := tracer.StartSpan("op1").(*Span)
assert.False(t, span.context.isSamplingFinalized(), "span is not finalized when created")
ext.SamplingPriority.Set(span, tt.priority)
assert.True(t, span.context.isSamplingFinalized(), "span is finalized after sampling.priority tag")
assert.Equal(t, tt.expectedSampled, span.context.IsSampled(), "span is sampled after sampling.priority tag")
assert.Equal(t, tt.expectedTags, span.Tags(), "sampling.priority tag in the span")
})
}
}
func TestSamplingIsFinalizedWithV1Samplers(t *testing.T) {
probabilistic, err := NewProbabilisticSampler(0.5)
require.NoError(t, err)
samplers := []Sampler{
NewConstSampler(false),
probabilistic,
NewRateLimitingSampler(1),
}
for _, sampler := range samplers {
t.Run(fmt.Sprintf("%s", sampler), func(t *testing.T) {
tracer, closer := NewTracer("test-service", sampler, NewNullReporter())
defer closer.Close()
span := tracer.StartSpan("op1").(*Span)
assert.True(t, span.context.isSamplingFinalized(), "span is finalized when created with V1 sampler")
})
}
}
func TestSamplingIsNotFinalizedWhenContextIsInjected(t *testing.T) {
tracer, closer := NewTracer("test-service", newRetryableSampler(false), NewNullReporter())
defer closer.Close()
span := tracer.StartSpan("op1").(*Span)
assert.False(t, span.context.isSamplingFinalized(), "span is not finalized when created")
err := tracer.Inject(span.Context(), opentracing.TextMap, opentracing.TextMapCarrier{})
require.NoError(t, err)
assert.False(t, span.context.isSamplingFinalized(), "span is not finalized after context is serialized")
}
func TestSamplingIsFinalizedInChildSpanOfRemoteParent(t *testing.T) {
for _, sampled := range []bool{false, true} {
t.Run(fmt.Sprintf("sampled=%t", sampled), func(t *testing.T) {
tracer, closer := NewTracer("test-service", newRetryableSampler(sampled), NewNullReporter())
defer closer.Close()
span := tracer.StartSpan("parent").(*Span)
assert.False(t, span.context.isSamplingFinalized(), "span is not finalized when created")
assert.Equal(t, sampled, span.context.IsSampled())
carrier := opentracing.TextMapCarrier{}
err := tracer.Inject(span.Context(), opentracing.TextMap, carrier)
require.NoError(t, err)
parentContext, err := tracer.Extract(opentracing.TextMap, carrier)
require.NoError(t, err)
childSpan := tracer.StartSpan("child", opentracing.ChildOf(parentContext)).(*Span)
assert.True(t, childSpan.context.isSamplingFinalized(), "child span is finalized")
assert.Equal(t, sampled, childSpan.context.IsSampled())
})
}
}
func TestSpanIsWriteableIfSampledOrNotFinalized(t *testing.T) {
tracer, closer := NewTracer("test-service", newRetryableSampler(false), NewNullReporter())
defer closer.Close()
span := tracer.StartSpan("span").(*Span)
assert.False(t, span.context.isSamplingFinalized(), "span is not finalized when created")
assert.False(t, span.context.IsSampled(), "span is not sampled")
assert.True(t, span.context.isWriteable(), "span is writeable")
tracer.(*Tracer).sampler = NewConstSampler(true)
span = tracer.StartSpan("span").(*Span)
assert.True(t, span.context.isSamplingFinalized(), "span is finalized when created")
assert.True(t, span.context.IsSampled(), "span is sampled")
assert.True(t, span.context.isWriteable(), "span is writeable")
}
func TestSetOperationOverridesOperationOnSampledSpan(t *testing.T) {
tracer, closer := NewTracer("test-service", NewConstSampler(true), NewNullReporter())
defer closer.Close()
span := tracer.StartSpan("op1").(*Span)
assert.True(t, span.context.isSamplingFinalized(), "span is finalized when created")
assert.True(t, span.context.IsSampled(), "span is sampled")
assert.Equal(t, "op1", span.OperationName())
assert.Equal(t, makeSamplerTags("const", true), span.tags)
span.tags = []Tag{} // easier to check that tags are not re-added
span.SetOperationName("op2")
assert.True(t, span.context.isSamplingFinalized(), "span is finalized when created")
assert.True(t, span.context.IsSampled(), "span is sampled")
assert.Equal(t, "op2", span.OperationName())
assert.Equal(t, []Tag{}, span.tags, "sampling tags are not re-added")
}