Skip to content

Commit f6c4be1

Browse files
authored
Move span.GetSamplerParams out of model/ into sampling/aggregator (jaegertracing#6583)
## Which problem is this PR solving? - Part of jaegertracing#6571 - Follow-up to jaegertracing/jaeger-idl#118 ## Description of the changes - Separate model files into more independent pieces that are easier to move to jaeger-idl ## How was this change tested? - `go test ./model/` - `go test ./internal/sampling/samplingstrategy/adaptive/` ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [ ] I have added unit tests for the new functionality - [ ] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `npm run lint` and `npm run test` --------- Signed-off-by: nabil salah <[email protected]>
1 parent 22952b8 commit f6c4be1

File tree

4 files changed

+158
-161
lines changed

4 files changed

+158
-161
lines changed

internal/sampling/samplingstrategy/adaptive/aggregator.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package adaptive
55

66
import (
7+
"strconv"
78
"sync"
89
"time"
910

@@ -150,9 +151,42 @@ func (a *aggregator) HandleRootSpan(span *span_model.Span) {
150151
if service == "" || span.OperationName == "" {
151152
return
152153
}
153-
samplerType, samplerParam := span.GetSamplerParams(a.postAggregator.logger)
154+
samplerType, samplerParam := getSamplerParams(span, a.postAggregator.logger)
154155
if samplerType == span_model.SamplerTypeUnrecognized {
155156
return
156157
}
157158
a.RecordThroughput(service, span.OperationName, samplerType, samplerParam)
158159
}
160+
161+
// GetSamplerParams returns the sampler.type and sampler.param value if they are valid.
162+
func getSamplerParams(s *span_model.Span, logger *zap.Logger) (span_model.SamplerType, float64) {
163+
samplerType := s.GetSamplerType()
164+
if samplerType == span_model.SamplerTypeUnrecognized {
165+
return span_model.SamplerTypeUnrecognized, 0
166+
}
167+
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))
177+
return span_model.SamplerTypeUnrecognized, 0
178+
}
179+
return samplerType, samplerParam
180+
}
181+
182+
func samplerParamToFloat(samplerParamTag span_model.KeyValue) (float64, error) {
183+
// The param could be represented as a string, an int, or a float
184+
switch samplerParamTag.VType {
185+
case span_model.Float64Type:
186+
return samplerParamTag.Float64(), nil
187+
case span_model.Int64Type:
188+
return float64(samplerParamTag.Int64()), nil
189+
default:
190+
return strconv.ParseFloat(samplerParamTag.AsString(), 64)
191+
}
192+
}

internal/sampling/samplingstrategy/adaptive/aggregator_test.go

+122
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package adaptive
55

66
import (
77
"net/http"
8+
"strconv"
89
"testing"
910
"time"
1011

@@ -186,3 +187,124 @@ func TestRecordThroughputFunc(t *testing.T) {
186187
a.HandleRootSpan(span)
187188
assert.EqualValues(t, 1, a.(*aggregator).currentThroughput["A"][http.MethodGet].Count)
188189
}
190+
191+
func TestGetSamplerParams(t *testing.T) {
192+
logger := zap.NewNop()
193+
194+
tests := []struct {
195+
tags model.KeyValues
196+
expectedType model.SamplerType
197+
expectedParam float64
198+
}{
199+
{
200+
tags: model.KeyValues{
201+
model.String("sampler.type", "probabilistic"),
202+
model.String("sampler.param", "1e-05"),
203+
},
204+
expectedType: model.SamplerTypeProbabilistic,
205+
expectedParam: 0.00001,
206+
},
207+
{
208+
tags: model.KeyValues{
209+
model.String("sampler.type", "probabilistic"),
210+
model.Float64("sampler.param", 0.10404450002098709),
211+
},
212+
expectedType: model.SamplerTypeProbabilistic,
213+
expectedParam: 0.10404450002098709,
214+
},
215+
{
216+
tags: model.KeyValues{
217+
model.String("sampler.type", "probabilistic"),
218+
model.String("sampler.param", "0.10404450002098709"),
219+
},
220+
expectedType: model.SamplerTypeProbabilistic,
221+
expectedParam: 0.10404450002098709,
222+
},
223+
{
224+
tags: model.KeyValues{
225+
model.String("sampler.type", "probabilistic"),
226+
model.Int64("sampler.param", 1),
227+
},
228+
expectedType: model.SamplerTypeProbabilistic,
229+
expectedParam: 1.0,
230+
},
231+
{
232+
tags: model.KeyValues{
233+
model.String("sampler.type", "ratelimiting"),
234+
model.String("sampler.param", "1"),
235+
},
236+
expectedType: model.SamplerTypeRateLimiting,
237+
expectedParam: 1,
238+
},
239+
{
240+
tags: model.KeyValues{
241+
model.Float64("sampler.type", 1.5),
242+
},
243+
expectedType: model.SamplerTypeUnrecognized,
244+
expectedParam: 0,
245+
},
246+
{
247+
tags: model.KeyValues{
248+
model.String("sampler.type", "probabilistic"),
249+
},
250+
expectedType: model.SamplerTypeUnrecognized,
251+
expectedParam: 0,
252+
},
253+
{
254+
tags: model.KeyValues{},
255+
expectedType: model.SamplerTypeUnrecognized,
256+
expectedParam: 0,
257+
},
258+
{
259+
tags: model.KeyValues{
260+
model.String("sampler.type", "lowerbound"),
261+
model.String("sampler.param", "1"),
262+
},
263+
expectedType: model.SamplerTypeLowerBound,
264+
expectedParam: 1,
265+
},
266+
{
267+
tags: model.KeyValues{
268+
model.String("sampler.type", "lowerbound"),
269+
model.Int64("sampler.param", 1),
270+
},
271+
expectedType: model.SamplerTypeLowerBound,
272+
expectedParam: 1,
273+
},
274+
{
275+
tags: model.KeyValues{
276+
model.String("sampler.type", "lowerbound"),
277+
model.Float64("sampler.param", 0.5),
278+
},
279+
expectedType: model.SamplerTypeLowerBound,
280+
expectedParam: 0.5,
281+
},
282+
{
283+
tags: model.KeyValues{
284+
model.String("sampler.type", "lowerbound"),
285+
model.String("sampler.param", "not_a_number"),
286+
},
287+
expectedType: model.SamplerTypeUnrecognized,
288+
expectedParam: 0,
289+
},
290+
{
291+
tags: model.KeyValues{
292+
model.String("sampler.type", "not_a_type"),
293+
model.String("sampler.param", "not_a_number"),
294+
},
295+
expectedType: model.SamplerTypeUnrecognized,
296+
expectedParam: 0,
297+
},
298+
}
299+
300+
for i, test := range tests {
301+
tt := test
302+
t.Run(strconv.Itoa(i), func(t *testing.T) {
303+
span := &model.Span{}
304+
span.Tags = tt.tags
305+
actualType, actualParam := getSamplerParams(span, logger)
306+
assert.Equal(t, tt.expectedType, actualType)
307+
assert.InDelta(t, tt.expectedParam, actualParam, 0.01)
308+
})
309+
}
310+
}

model/span.go

-36
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ package model
77
import (
88
"encoding/gob"
99
"io"
10-
"strconv"
11-
12-
"go.uber.org/zap"
1310
)
1411

1512
type SamplerType int
@@ -142,36 +139,3 @@ func (s *Span) ReplaceParentID(newParentID SpanID) {
142139
}
143140
s.References = MaybeAddParentSpanID(s.TraceID, newParentID, s.References)
144141
}
145-
146-
// GetSamplerParams returns the sampler.type and sampler.param value if they are valid.
147-
func (s *Span) GetSamplerParams(logger *zap.Logger) (SamplerType, float64) {
148-
samplerType := s.GetSamplerType()
149-
if samplerType == SamplerTypeUnrecognized {
150-
return SamplerTypeUnrecognized, 0
151-
}
152-
tag, ok := KeyValues(s.Tags).FindByKey(SamplerParamKey)
153-
if !ok {
154-
return SamplerTypeUnrecognized, 0
155-
}
156-
samplerParam, err := samplerParamToFloat(tag)
157-
if err != nil {
158-
logger.
159-
With(zap.String("traceID", s.TraceID.String())).
160-
With(zap.String("spanID", s.SpanID.String())).
161-
Warn("sampler.param tag is not a number", zap.Any("tag", tag))
162-
return SamplerTypeUnrecognized, 0
163-
}
164-
return samplerType, samplerParam
165-
}
166-
167-
func samplerParamToFloat(samplerParamTag KeyValue) (float64, error) {
168-
// The param could be represented as a string, an int, or a float
169-
switch samplerParamTag.VType {
170-
case Float64Type:
171-
return samplerParamTag.Float64(), nil
172-
case Int64Type:
173-
return float64(samplerParamTag.Int64()), nil
174-
default:
175-
return strconv.ParseFloat(samplerParamTag.AsString(), 64)
176-
}
177-
}

model/span_test.go

+1-124
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ package model_test
77
import (
88
"bytes"
99
"fmt"
10-
"strconv"
1110
"testing"
1211
"time"
1312

1413
"github.com/gogo/protobuf/jsonpb"
1514
"github.com/gogo/protobuf/proto"
1615
"github.com/stretchr/testify/assert"
1716
"github.com/stretchr/testify/require"
18-
"go.opentelemetry.io/otel/trace"
19-
"go.uber.org/zap"
2017

2118
"github.com/jaegertracing/jaeger/model"
2219
)
@@ -165,7 +162,7 @@ func TestSpanIDUnmarshalJSONErrors(t *testing.T) {
165162
func TestIsRPCClientServer(t *testing.T) {
166163
span1 := &model.Span{
167164
Tags: model.KeyValues{
168-
model.String(model.SpanKindKey, trace.SpanKindClient.String()),
165+
model.String(model.SpanKindKey, "client"),
169166
},
170167
}
171168
assert.True(t, span1.IsRPCClient())
@@ -335,123 +332,3 @@ func BenchmarkBatchSerialization(b *testing.B) {
335332
}
336333
})
337334
}
338-
339-
func TestGetSamplerParams(t *testing.T) {
340-
logger := zap.NewNop()
341-
tests := []struct {
342-
tags model.KeyValues
343-
expectedType model.SamplerType
344-
expectedParam float64
345-
}{
346-
{
347-
tags: model.KeyValues{
348-
model.String("sampler.type", "probabilistic"),
349-
model.String("sampler.param", "1e-05"),
350-
},
351-
expectedType: model.SamplerTypeProbabilistic,
352-
expectedParam: 0.00001,
353-
},
354-
{
355-
tags: model.KeyValues{
356-
model.String("sampler.type", "probabilistic"),
357-
model.Float64("sampler.param", 0.10404450002098709),
358-
},
359-
expectedType: model.SamplerTypeProbabilistic,
360-
expectedParam: 0.10404450002098709,
361-
},
362-
{
363-
tags: model.KeyValues{
364-
model.String("sampler.type", "probabilistic"),
365-
model.String("sampler.param", "0.10404450002098709"),
366-
},
367-
expectedType: model.SamplerTypeProbabilistic,
368-
expectedParam: 0.10404450002098709,
369-
},
370-
{
371-
tags: model.KeyValues{
372-
model.String("sampler.type", "probabilistic"),
373-
model.Int64("sampler.param", 1),
374-
},
375-
expectedType: model.SamplerTypeProbabilistic,
376-
expectedParam: 1.0,
377-
},
378-
{
379-
tags: model.KeyValues{
380-
model.String("sampler.type", "ratelimiting"),
381-
model.String("sampler.param", "1"),
382-
},
383-
expectedType: model.SamplerTypeRateLimiting,
384-
expectedParam: 1,
385-
},
386-
{
387-
tags: model.KeyValues{
388-
model.Float64("sampler.type", 1.5),
389-
},
390-
expectedType: model.SamplerTypeUnrecognized,
391-
expectedParam: 0,
392-
},
393-
{
394-
tags: model.KeyValues{
395-
model.String("sampler.type", "probabilistic"),
396-
},
397-
expectedType: model.SamplerTypeUnrecognized,
398-
expectedParam: 0,
399-
},
400-
{
401-
tags: model.KeyValues{},
402-
expectedType: model.SamplerTypeUnrecognized,
403-
expectedParam: 0,
404-
},
405-
{
406-
tags: model.KeyValues{
407-
model.String("sampler.type", "lowerbound"),
408-
model.String("sampler.param", "1"),
409-
},
410-
expectedType: model.SamplerTypeLowerBound,
411-
expectedParam: 1,
412-
},
413-
{
414-
tags: model.KeyValues{
415-
model.String("sampler.type", "lowerbound"),
416-
model.Int64("sampler.param", 1),
417-
},
418-
expectedType: model.SamplerTypeLowerBound,
419-
expectedParam: 1,
420-
},
421-
{
422-
tags: model.KeyValues{
423-
model.String("sampler.type", "lowerbound"),
424-
model.Float64("sampler.param", 0.5),
425-
},
426-
expectedType: model.SamplerTypeLowerBound,
427-
expectedParam: 0.5,
428-
},
429-
{
430-
tags: model.KeyValues{
431-
model.String("sampler.type", "lowerbound"),
432-
model.String("sampler.param", "not_a_number"),
433-
},
434-
expectedType: model.SamplerTypeUnrecognized,
435-
expectedParam: 0,
436-
},
437-
{
438-
tags: model.KeyValues{
439-
model.String("sampler.type", "not_a_type"),
440-
model.String("sampler.param", "not_a_number"),
441-
},
442-
expectedType: model.SamplerTypeUnrecognized,
443-
expectedParam: 0,
444-
},
445-
}
446-
447-
for i, test := range tests {
448-
tt := test
449-
t.Run(strconv.Itoa(i), func(t *testing.T) {
450-
span := &model.Span{}
451-
span.Tags = tt.tags
452-
actualType, actualParam := span.GetSamplerParams(logger)
453-
assert.Equal(t, tt.expectedType, actualType)
454-
assert.InDelta(t, tt.expectedParam, actualParam, 0.01)
455-
})
456-
}
457-
}

0 commit comments

Comments
 (0)