-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle all the datatypes in the EMA sampler (#186)
Now that we support msgpack the types that might be in the data hash are more varied than they used to be. Adds the ability to format any type as a string in order to handle this
- Loading branch information
Showing
5 changed files
with
272 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package sample | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strconv" | ||
|
||
"github.com/honeycombio/refinery/types" | ||
) | ||
|
||
type traceKey struct { | ||
fields []string | ||
useTraceLength bool | ||
addDynsampleKey bool | ||
addDynsampleField string | ||
} | ||
|
||
func newTraceKey(fields []string, useTraceLength, addDynsampleKey bool, addDynsampleField string) *traceKey { | ||
// always put the field list in sorted order for easier comparison | ||
sort.Strings(fields) | ||
return &traceKey{ | ||
fields: fields, | ||
useTraceLength: useTraceLength, | ||
addDynsampleKey: addDynsampleKey, | ||
addDynsampleField: addDynsampleField, | ||
} | ||
} | ||
|
||
// buildAndAdd, builds the trace key and adds it to the trace if configured to | ||
// do so | ||
func (d *traceKey) buildAndAdd(trace *types.Trace) string { | ||
key := d.build(trace) | ||
|
||
if d.addDynsampleKey { | ||
for _, span := range trace.GetSpans() { | ||
span.Data[d.addDynsampleField] = key | ||
} | ||
} | ||
|
||
return key | ||
} | ||
|
||
// build, builds the trace key based on the configuration of the traceKeyGenerator | ||
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{} | ||
|
||
// for each field, for each span, get the value of that field | ||
spans := trace.GetSpans() | ||
for _, field := range d.fields { | ||
for _, span := range spans { | ||
if val, ok := span.Data[field]; ok { | ||
fieldCollector[field] = append(fieldCollector[field], fmt.Sprintf("%v", val)) | ||
} | ||
} | ||
} | ||
// ok, now we have a map of fields to a list of all values for that field. | ||
|
||
var key string | ||
for _, field := range d.fields { | ||
// sort and collapse list | ||
sort.Strings(fieldCollector[field]) | ||
var prevStr string | ||
for _, str := range fieldCollector[field] { | ||
if str != prevStr { | ||
key += str + "•" | ||
} | ||
prevStr = str | ||
} | ||
// get ready for the next element | ||
key += "," | ||
} | ||
|
||
if d.useTraceLength { | ||
key += strconv.FormatInt(int64(len(spans)), 10) | ||
} | ||
|
||
return key | ||
} |
Oops, something went wrong.