-
Notifications
You must be signed in to change notification settings - Fork 24
/
sdk_labels.go
75 lines (63 loc) · 2.53 KB
/
sdk_labels.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package internal
import (
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"github.com/fluxninja/aperture/pkg/log"
otelconsts "github.com/fluxninja/aperture/pkg/otelcollector/consts"
)
// AddSDKSpecificLabels adds labels specific to SDK data source.
func AddSDKSpecificLabels(attributes pcommon.Map) {
// Compute durations
flowStart, flowStartExists := getSDKLabelTimestampValue(attributes, otelconsts.ApertureFlowStartTimestampLabel)
workloadStart, workloadStartExists := getSDKLabelTimestampValue(attributes, otelconsts.ApertureWorkloadStartTimestampLabel)
flowEnd, flowEndExists := getSDKLabelTimestampValue(attributes, otelconsts.ApertureFlowEndTimestampLabel)
// Add ResponseReceivedLabel based on whether flowEnd is present
if flowEndExists {
attributes.PutStr(otelconsts.ResponseReceivedLabel, otelconsts.ResponseReceivedTrue)
} else {
attributes.PutStr(otelconsts.ResponseReceivedLabel, otelconsts.ResponseReceivedFalse)
}
if flowStartExists && flowEndExists {
flowDuration := flowEnd.Sub(flowStart)
attributes.PutDouble(otelconsts.FlowDurationLabel, float64(flowDuration.Milliseconds()))
}
if workloadStartExists && flowEndExists {
workloadDuration := flowEnd.Sub(workloadStart)
attributes.PutDouble(otelconsts.WorkloadDurationLabel, float64(workloadDuration.Milliseconds()))
}
}
func getSDKLabelTimestampValue(attributes pcommon.Map, labelKey string) (time.Time, bool) {
return getLabelTimestampValue(attributes, labelKey, otelconsts.ApertureSourceSDK)
}
func getLabelTimestampValue(attributes pcommon.Map, labelKey, source string) (time.Time, bool) {
value, exists := getLabelValue(attributes, labelKey, source)
if !exists {
return time.Time{}, false
}
return _getLabelTimestampValue(value, labelKey, source)
}
func getLabelValue(attributes pcommon.Map, labelKey, source string) (pcommon.Value, bool) {
value, exists := attributes.Get(labelKey)
if !exists {
log.Sample(noLabelSampler).
Warn().Str("source", source).Str("key", labelKey).Msg("Label not found")
return pcommon.Value{}, false
}
return value, exists
}
func _getLabelTimestampValue(value pcommon.Value, labelKey, source string) (time.Time, bool) {
var valueInt int64
if value.Type() == pcommon.ValueTypeInt {
valueInt = value.Int()
} else {
log.Sample(badTimestampSampler).
Warn().Str("source", source).Str("key", labelKey).
Msg("Failed to parse a timestamp field")
return time.Time{}, false
}
return time.Unix(0, valueInt), true
}
var (
noLabelSampler = log.NewRatelimitingSampler()
badTimestampSampler = log.NewRatelimitingSampler()
)