Skip to content

Commit 4ddc782

Browse files
committed
Created e2e integration tests for Adaptive Sampling jaegertracing#5717
Signed-off-by: sAchin-680 <[email protected]>
1 parent 7a2db6b commit 4ddc782

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

internal/sampling/samplingstrategy/adaptive/aggregator.go

+28-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const (
2323
maxProbabilities = 10
2424
)
2525

26+
var EnableAdaptiveSamplingWithoutTags = false // Default: false, requires explicit opt-in
27+
2628
// aggregator is a kind of trace processor that watches for root spans
2729
// and calculates how many traces per service / per endpoint are being
2830
// produced. It periodically flushes these stats ("throughput") to storage.
@@ -161,21 +163,37 @@ func (a *aggregator) HandleRootSpan(span *span_model.Span) {
161163
// GetSamplerParams returns the sampler.type and sampler.param value if they are valid.
162164
func getSamplerParams(s *span_model.Span, logger *zap.Logger) (span_model.SamplerType, float64) {
163165
samplerType := s.GetSamplerType()
166+
samplerParam := 1.0 // Default probability
167+
168+
// If sampler.type is missing and feature flag is enabled, use default type
164169
if samplerType == span_model.SamplerTypeUnrecognized {
165-
return span_model.SamplerTypeUnrecognized, 0
170+
if !EnableAdaptiveSamplingWithoutTags {
171+
return span_model.SamplerTypeUnrecognized, 0
172+
}
173+
samplerType = span_model.SamplerTypeProbabilistic
166174
}
175+
176+
// Fetch sampler.param tag
167177
tag, ok := span_model.KeyValues(s.Tags).FindByKey(span_model.SamplerParamKey)
168-
if !ok {
169-
return span_model.SamplerTypeUnrecognized, 0
170-
}
171-
samplerParam, err := samplerParamToFloat(tag)
172-
if err != nil {
173-
logger.
174-
With(zap.String("traceID", s.TraceID.String())).
175-
With(zap.String("spanID", s.SpanID.String())).
176-
Warn("sampler.param tag is not a number", zap.Any("tag", tag))
178+
if ok {
179+
var err error
180+
samplerParam, err = samplerParamToFloat(tag)
181+
if err != nil {
182+
logger.With(
183+
zap.String("traceID", s.TraceID.String()),
184+
zap.String("spanID", s.SpanID.String()),
185+
zap.Any("tag", tag),
186+
).Warn("sampler.param tag is not a number")
187+
188+
// Fall back to default probability if flag is enabled
189+
if !EnableAdaptiveSamplingWithoutTags {
190+
return span_model.SamplerTypeUnrecognized, 0
191+
}
192+
}
193+
} else if !EnableAdaptiveSamplingWithoutTags {
177194
return span_model.SamplerTypeUnrecognized, 0
178195
}
196+
179197
return samplerType, samplerParam
180198
}
181199

internal/sampling/samplingstrategy/adaptive/aggregator_test.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,14 @@ func TestGetSamplerParams(t *testing.T) {
192192
logger := zap.NewNop()
193193

194194
tests := []struct {
195+
name string
195196
tags model.KeyValues
197+
enableFlag bool
196198
expectedType model.SamplerType
197199
expectedParam float64
198200
}{
199201
{
202+
name: "Valid probabilistic sampler with string param",
200203
tags: model.KeyValues{
201204
model.String("sampler.type", "probabilistic"),
202205
model.String("sampler.param", "1e-05"),
@@ -205,6 +208,7 @@ func TestGetSamplerParams(t *testing.T) {
205208
expectedParam: 0.00001,
206209
},
207210
{
211+
name: "Valid probabilistic sampler with float64 param",
208212
tags: model.KeyValues{
209213
model.String("sampler.type", "probabilistic"),
210214
model.Float64("sampler.param", 0.10404450002098709),
@@ -213,6 +217,7 @@ func TestGetSamplerParams(t *testing.T) {
213217
expectedParam: 0.10404450002098709,
214218
},
215219
{
220+
name: "Valid probabilistic sampler with stringified float param",
216221
tags: model.KeyValues{
217222
model.String("sampler.type", "probabilistic"),
218223
model.String("sampler.param", "0.10404450002098709"),
@@ -221,6 +226,7 @@ func TestGetSamplerParams(t *testing.T) {
221226
expectedParam: 0.10404450002098709,
222227
},
223228
{
229+
name: "Probabilistic sampler with int64 param",
224230
tags: model.KeyValues{
225231
model.String("sampler.type", "probabilistic"),
226232
model.Int64("sampler.param", 1),
@@ -229,6 +235,7 @@ func TestGetSamplerParams(t *testing.T) {
229235
expectedParam: 1.0,
230236
},
231237
{
238+
name: "Valid rate limiting sampler",
232239
tags: model.KeyValues{
233240
model.String("sampler.type", "ratelimiting"),
234241
model.String("sampler.param", "1"),
@@ -237,25 +244,37 @@ func TestGetSamplerParams(t *testing.T) {
237244
expectedParam: 1,
238245
},
239246
{
247+
name: "Invalid sampler.type (float instead of string)",
240248
tags: model.KeyValues{
241249
model.Float64("sampler.type", 1.5),
242250
},
243251
expectedType: model.SamplerTypeUnrecognized,
244252
expectedParam: 0,
245253
},
246254
{
255+
name: "Probabilistic sampler missing param",
247256
tags: model.KeyValues{
248257
model.String("sampler.type", "probabilistic"),
249258
},
250259
expectedType: model.SamplerTypeUnrecognized,
251260
expectedParam: 0,
252261
},
253262
{
254-
tags: model.KeyValues{},
263+
name: "No tags, feature flag disabled",
264+
tags: model.KeyValues{},
265+
enableFlag: false,
255266
expectedType: model.SamplerTypeUnrecognized,
256267
expectedParam: 0,
257268
},
258269
{
270+
name: "No tags, feature flag enabled",
271+
tags: model.KeyValues{},
272+
enableFlag: true,
273+
expectedType: model.SamplerTypeProbabilistic,
274+
expectedParam: 1.0,
275+
},
276+
{
277+
name: "Lowerbound sampler with string param",
259278
tags: model.KeyValues{
260279
model.String("sampler.type", "lowerbound"),
261280
model.String("sampler.param", "1"),
@@ -264,6 +283,7 @@ func TestGetSamplerParams(t *testing.T) {
264283
expectedParam: 1,
265284
},
266285
{
286+
name: "Lowerbound sampler with int64 param",
267287
tags: model.KeyValues{
268288
model.String("sampler.type", "lowerbound"),
269289
model.Int64("sampler.param", 1),
@@ -272,6 +292,7 @@ func TestGetSamplerParams(t *testing.T) {
272292
expectedParam: 1,
273293
},
274294
{
295+
name: "Lowerbound sampler with float64 param",
275296
tags: model.KeyValues{
276297
model.String("sampler.type", "lowerbound"),
277298
model.Float64("sampler.param", 0.5),
@@ -280,6 +301,7 @@ func TestGetSamplerParams(t *testing.T) {
280301
expectedParam: 0.5,
281302
},
282303
{
304+
name: "Invalid sampler.param (not a number) for lowerbound",
283305
tags: model.KeyValues{
284306
model.String("sampler.type", "lowerbound"),
285307
model.String("sampler.param", "not_a_number"),
@@ -288,6 +310,7 @@ func TestGetSamplerParams(t *testing.T) {
288310
expectedParam: 0,
289311
},
290312
{
313+
name: "Invalid sampler.type and invalid sampler.param",
291314
tags: model.KeyValues{
292315
model.String("sampler.type", "not_a_type"),
293316
model.String("sampler.param", "not_a_number"),
@@ -299,12 +322,15 @@ func TestGetSamplerParams(t *testing.T) {
299322

300323
for i, test := range tests {
301324
tt := test
302-
t.Run(strconv.Itoa(i), func(t *testing.T) {
303-
span := &model.Span{}
304-
span.Tags = tt.tags
325+
t.Run(strconv.Itoa(i)+"_"+tt.name, func(t *testing.T) {
326+
// Set the feature flag for this test case
327+
EnableAdaptiveSamplingWithoutTags = tt.enableFlag
328+
329+
span := &model.Span{Tags: tt.tags}
305330
actualType, actualParam := getSamplerParams(span, logger)
306-
assert.Equal(t, tt.expectedType, actualType)
307-
assert.InDelta(t, tt.expectedParam, actualParam, 0.01)
331+
332+
assert.Equal(t, tt.expectedType, actualType, "Test failed for case: %s", tt.name)
333+
assert.InDelta(t, tt.expectedParam, actualParam, 0.01, "Test failed for case: %s", tt.name)
308334
})
309335
}
310336
}

0 commit comments

Comments
 (0)