forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalfxv2_event_to_logdata_test.go
130 lines (114 loc) · 3.06 KB
/
signalfxv2_event_to_logdata_test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package signalfxreceiver
import (
"fmt"
"sort"
"testing"
"time"
sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
)
func TestSignalFxV2EventsToLogData(t *testing.T) {
now := time.Now()
msec := now.UnixNano() / 1e6
// Put it on stack or heap so we can take a ref to it.
userDefinedCat := sfxpb.EventCategory_USER_DEFINED
buildDefaultSFxEvent := func() *sfxpb.Event {
return &sfxpb.Event{
EventType: "shutdown",
Timestamp: msec,
Category: &userDefinedCat,
Dimensions: buildNDimensions(3),
Properties: mapToEventProps(map[string]interface{}{
"env": "prod",
"isActive": true,
"rack": 5,
"temp": 40.5,
"nullProp": nil,
}),
}
}
buildDefaultLogs := func() plog.ScopeLogs {
sl := plog.NewScopeLogs()
l := sl.LogRecords().AppendEmpty()
l.SetTimestamp(pcommon.NewTimestampFromTime(now.Truncate(time.Millisecond)))
attrs := l.Attributes()
attrs.PutStr("com.splunk.signalfx.event_type", "shutdown")
attrs.PutStr("k0", "v0")
attrs.PutStr("k1", "v1")
attrs.PutStr("k2", "v2")
attrs.PutInt("com.splunk.signalfx.event_category", int64(sfxpb.EventCategory_USER_DEFINED))
propMap := attrs.PutEmptyMap("com.splunk.signalfx.event_properties")
propMap.PutStr("env", "prod")
propMap.PutBool("isActive", true)
propMap.PutInt("rack", 5)
propMap.PutDouble("temp", 40.5)
propMap.PutEmpty("nullProp")
return sl
}
tests := []struct {
name string
sfxEvents []*sfxpb.Event
expected plog.ScopeLogs
}{
{
name: "default",
sfxEvents: []*sfxpb.Event{buildDefaultSFxEvent()},
expected: buildDefaultLogs(),
},
{
name: "missing category",
sfxEvents: func() []*sfxpb.Event {
e := buildDefaultSFxEvent()
e.Category = nil
return []*sfxpb.Event{e}
}(),
expected: func() plog.ScopeLogs {
lrs := buildDefaultLogs()
lrs.LogRecords().At(0).Attributes().PutEmpty("com.splunk.signalfx.event_category")
return lrs
}(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sl := plog.NewScopeLogs()
signalFxV2EventsToLogRecords(tt.sfxEvents, sl.LogRecords())
assert.NoError(t, plogtest.CompareScopeLogs(tt.expected, sl))
})
}
}
func mapToEventProps(m map[string]interface{}) []*sfxpb.Property {
var out []*sfxpb.Property
for k, v := range m {
var pval sfxpb.PropertyValue
switch t := v.(type) {
case nil:
case string:
pval.StrValue = &t
case int:
asInt := int64(t)
pval.IntValue = &asInt
case int64:
pval.IntValue = &t
case bool:
pval.BoolValue = &t
case float64:
pval.DoubleValue = &t
default:
panic(fmt.Sprintf("invalid type: %v", v))
}
out = append(out, &sfxpb.Property{
Key: k,
Value: &pval,
})
}
sort.Slice(out, func(i, j int) bool {
return out[i].Key < out[j].Key
})
return out
}