Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow root. in field list for dynamic sampler #1275

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions sample/dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,41 @@ func TestDynamicAddSampleRateKeyToTrace(t *testing.T) {

sampler := &DynamicSampler{
Config: &config.DynamicSamplerConfig{
FieldList: []string{"http.status_code"},
FieldList: []string{"http.status_code", "root.service_name", "root.url"},
SampleRate: 1,
},
Logger: &logger.NullLogger{},
Metrics: &metrics,
}

trace := &types.Trace{}
for i := 0; i < spanCount; i++ {
if i == spanCount-1 {
trace.RootSpan = &types.Span{
Event: types.Event{
Data: map[string]interface{}{
"http.status_code": "200",
"service_name": "test",
},
},
}
}
trace.AddSpan(&types.Span{
Event: types.Event{
Data: map[string]interface{}{
"http.status_code": "200",
"url": "/test",
},
},
})
}
sampler.Start()
sampler.GetSampleRate(trace)
rate, keep, reason, key := sampler.GetSampleRate(trace)

spans := trace.GetSpans()
assert.Len(t, spans, spanCount, "should have the same number of spans as input")
assert.Equal(t, uint(1), rate)
assert.True(t, keep)
assert.Equal(t, "dynamic", reason)
assert.Equal(t, "200•,test,", key)
}
27 changes: 25 additions & 2 deletions sample/trace_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,34 @@ import (
"fmt"
"sort"
"strconv"
"strings"

"github.com/honeycombio/refinery/types"
)

type traceKey struct {
fields []string
rootOnlyFields []string
useTraceLength bool
}

func newTraceKey(fields []string, useTraceLength bool) *traceKey {
// always put the field list in sorted order for easier comparison
sort.Strings(fields)
rootOnlyFields := make([]string, 0, len(fields)/2)
nonRootFields := make([]string, 0, len(fields)/2)
for _, field := range fields {
if strings.HasPrefix(field, RootPrefix) {
rootOnlyFields = append(rootOnlyFields, field[len(RootPrefix):])
continue
}

nonRootFields = append(nonRootFields, field)
}

return &traceKey{
fields: fields,
fields: nonRootFields,
rootOnlyFields: rootOnlyFields,
useTraceLength: useTraceLength,
}
}
Expand All @@ -26,7 +40,7 @@ func newTraceKey(fields []string, useTraceLength bool) *traceKey {
func (d *traceKey) build(trace *types.Trace) string {
// fieldCollector gets all values from the fields listed in the config, even
// if they happen multiple times.
fieldCollector := map[string][]string{}
fieldCollector := make(map[string][]string)

// for each field, for each span, get the value of that field
spans := trace.GetSpans()
Expand Down Expand Up @@ -54,6 +68,15 @@ func (d *traceKey) build(trace *types.Trace) string {
key += ","
}

if trace.RootSpan != nil {
for _, field := range d.rootOnlyFields {

if val, ok := trace.RootSpan.Data[field]; ok {
key += fmt.Sprintf("%v,", val)
}
}
}

if d.useTraceLength {
key += strconv.FormatInt(int64(len(spans)), 10)
}
Expand Down
22 changes: 8 additions & 14 deletions sample/trace_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ func TestKeyGeneration(t *testing.T) {

assert.Equal(t, expected, generator.build(trace))

// now test that multiple values across spans in a different order are condensed the same
fields = []string{"http.status_code"}
// test field list with root prefix, only include the field from on the root span
// if it exists
fields = []string{"http.status_code", "root.service_name", "root.another_field"}
useTraceLength = true

generator = newTraceKey(fields, useTraceLength)
Expand All @@ -133,31 +134,24 @@ func TestKeyGeneration(t *testing.T) {
},
})

trace.AddSpan(&types.Span{
Event: types.Event{
Data: map[string]interface{}{
"http.status_code": 404,
},
},
})

trace.AddSpan(&types.Span{
Event: types.Event{
Data: map[string]interface{}{
"http.status_code": 200,
"service_name": "another",
},
},
})

trace.AddSpan(&types.Span{
trace.RootSpan = &types.Span{
Event: types.Event{
Data: map[string]interface{}{
"http.status_code": 200,
"service_name": "test",
},
},
})
}

expected = "200•404•,4"
expected = "200•404•,test,2"

assert.Equal(t, expected, generator.build(trace))
}
Loading