forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalfxv2_event_to_logdata.go
84 lines (73 loc) · 2.91 KB
/
signalfxv2_event_to_logdata.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
// Copyright OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package signalfxreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/signalfxreceiver"
import (
sfxpb "github.com/signalfx/com_signalfx_metrics_protobuf/model"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)
// signalFxV2ToMetricsData converts SignalFx event proto data points to
// plog.LogRecordSlice. Returning the converted data and the number of dropped log
// records.
func signalFxV2EventsToLogRecords(events []*sfxpb.Event, lrs plog.LogRecordSlice) {
lrs.EnsureCapacity(len(events))
for _, event := range events {
lr := lrs.AppendEmpty()
attrs := lr.Attributes()
attrs.EnsureCapacity(2 + len(event.Dimensions) + len(event.Properties))
for _, dim := range event.Dimensions {
attrs.PutStr(dim.Key, dim.Value)
}
// The EventType field is stored as an attribute.
eventType := event.EventType
if eventType == "" {
eventType = "unknown"
}
attrs.PutStr(splunk.SFxEventType, eventType)
// SignalFx timestamps are in millis so convert to nanos by multiplying
// by 1 million.
lr.SetTimestamp(pcommon.Timestamp(event.Timestamp * 1e6))
if event.Category != nil {
attrs.PutInt(splunk.SFxEventCategoryKey, int64(*event.Category))
} else {
// This gives us an unambiguous way of determining that a log record
// represents a SignalFx event, even if category is missing from the
// event.
attrs.PutEmpty(splunk.SFxEventCategoryKey)
}
if len(event.Properties) > 0 {
propMap := attrs.PutEmptyMap(splunk.SFxEventPropertiesKey)
propMap.EnsureCapacity(len(event.Properties))
for _, prop := range event.Properties {
// No way to tell what value type is without testing each
// individually.
switch {
case prop.Value.StrValue != nil:
propMap.PutStr(prop.Key, prop.Value.GetStrValue())
case prop.Value.IntValue != nil:
propMap.PutInt(prop.Key, prop.Value.GetIntValue())
case prop.Value.DoubleValue != nil:
propMap.PutDouble(prop.Key, prop.Value.GetDoubleValue())
case prop.Value.BoolValue != nil:
propMap.PutBool(prop.Key, prop.Value.GetBoolValue())
default:
// If there is no property value, just insert a null to
// record that the key was present.
propMap.PutEmpty(prop.Key)
}
}
}
}
}