-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampling_header_encoder.go
58 lines (50 loc) · 1.22 KB
/
sampling_header_encoder.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
package bugsnagperformance
import (
"fmt"
"sort"
"strconv"
"strings"
)
type samplingHeaderEncoder struct{}
func (enc *samplingHeaderEncoder) encode(spans []managedSpan) string {
if len(spans) == 0 {
return "1.0:0"
}
mappedValues := map[float64]int{}
for _, span := range spans {
attributes := span.span.Attributes()
found := false
for _, keyVal := range attributes {
if keyVal.Key == samplingAttribute {
// was resampled
if span.samplingProbability != nil {
mappedValues[*span.samplingProbability] += 1
} else {
mappedValues[keyVal.Value.AsFloat64()] += 1
}
found = true
break
}
}
if !found {
// Bail if the atrribute is missing; we'll warn about this later as it
// means something has gone wrong
return ""
}
}
// Sort the keys so the result is deterministic
keysSlice := []float64{}
for key := range mappedValues {
keysSlice = append(keysSlice, key)
}
sort.Float64s(keysSlice)
valuesSlice := []string{}
for _, key := range keysSlice {
keyStr := strconv.FormatFloat(key, 'g', -1, 64)
if keyStr == "1" {
keyStr = "1.0"
}
valuesSlice = append(valuesSlice, fmt.Sprintf("%+v:%+v", keyStr, mappedValues[key]))
}
return strings.Join(valuesSlice[:], ";")
}