Skip to content

Commit

Permalink
Update SA event type to fix processlist (open-telemetry#1385)
Browse files Browse the repository at this point in the history
Signed-off-by: Dani Louca <dlouca@splunk.com>
  • Loading branch information
dloucasfx authored Mar 22, 2022
1 parent 07fcab6 commit c2db908
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
10 changes: 7 additions & 3 deletions internal/receiver/smartagentreceiver/converter/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ const (
SFxEventCategoryKey = "com.splunk.signalfx.event_category"
// SFxEventPropertiesKey key for splunk event properties.
SFxEventPropertiesKey = "com.splunk.signalfx.event_properties"
// SFxEventType key for splunk event type
SFxEventType = "com.splunk.signalfx.event_type"
)

// eventToLog converts a SFx event to a pdata.Logs entry suitable for consumption by LogConsumer.
// based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/5de076e9773bdb7617b544a57fa0a4b848cec92c/receiver/signalfxreceiver/signalfxv2_event_to_logdata.go#L27
func sfxEventToPDataLogs(event *event.Event, logger *zap.Logger) pdata.Logs {
logs, lr := newLogs()

lr.SetName(event.EventType)

var unixNano int64
if !event.Timestamp.IsZero() {
unixNano = event.Timestamp.UnixNano()
}
lr.SetTimestamp(pdata.Timestamp(unixNano))

// size for event category and dimension attributes
attrsCapacity := 1 + len(event.Dimensions)
attrsCapacity := 2 + len(event.Dimensions)
if len(event.Properties) > 0 {
attrsCapacity++
}
Expand All @@ -58,6 +58,10 @@ func sfxEventToPDataLogs(event *event.Event, logger *zap.Logger) pdata.Logs {
attrs.InsertInt(SFxEventCategoryKey, int64(event.Category))
}

if event.EventType != "" {
attrs.InsertString(SFxEventType, event.EventType)
}

for k, v := range event.Dimensions {
attrs.InsertString(k, v)
}
Expand Down
93 changes: 42 additions & 51 deletions internal/receiver/smartagentreceiver/converter/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ func TestEventToPDataLogs(tt *testing.T) {
{
name: "event zero value",
event: event.Event{},
expectedLog: newExpectedLog(
"",
map[string]pdata.AttributeValue{
"com.splunk.signalfx.event_category": pdata.NewAttributeValueEmpty(),
}, 0,
),
expectedLog: newExpectedLog(map[string]pdata.AttributeValue{
"com.splunk.signalfx.event_category": pdata.NewAttributeValueEmpty(),
}, 0),
},
{
name: "complete event",
Expand All @@ -63,31 +60,29 @@ func TestEventToPDataLogs(tt *testing.T) {
},
Timestamp: time.Unix(1, 1),
},
expectedLog: newExpectedLog(
"some_event_type",
func() map[string]pdata.AttributeValue {
attrs := map[string]pdata.AttributeValue{
"com.splunk.signalfx.event_category": pdata.NewAttributeValueInt(1),
"dimension_name": pdata.NewAttributeValueString("dimension_value"),
}
properties := pdata.NewAttributeValueMap()
pdata.NewAttributeMapFromMap(
map[string]pdata.AttributeValue{
"bool_property_name": pdata.NewAttributeValueBool(true),
"string_property_name": pdata.NewAttributeValueString("some value"),
"int_property_name": pdata.NewAttributeValueInt(12345),
"int8_property_name": pdata.NewAttributeValueInt(127),
"int16_property_name": pdata.NewAttributeValueInt(23456),
"int32_property_name": pdata.NewAttributeValueInt(34567),
"int64_property_name": pdata.NewAttributeValueInt(45678),
"float32_property_name": pdata.NewAttributeValueDouble(12345.678),
"float64_property_name": pdata.NewAttributeValueDouble(23456.789),
},
).CopyTo(properties.MapVal())
attrs["com.splunk.signalfx.event_properties"] = properties
return attrs
}(), 1000000001,
),
expectedLog: newExpectedLog(func() map[string]pdata.AttributeValue {
attrs := map[string]pdata.AttributeValue{
"com.splunk.signalfx.event_category": pdata.NewAttributeValueInt(1),
"com.splunk.signalfx.event_type": pdata.NewAttributeValueString("some_event_type"),
"dimension_name": pdata.NewAttributeValueString("dimension_value"),
}
properties := pdata.NewAttributeValueMap()
pdata.NewAttributeMapFromMap(
map[string]pdata.AttributeValue{
"bool_property_name": pdata.NewAttributeValueBool(true),
"string_property_name": pdata.NewAttributeValueString("some value"),
"int_property_name": pdata.NewAttributeValueInt(12345),
"int8_property_name": pdata.NewAttributeValueInt(127),
"int16_property_name": pdata.NewAttributeValueInt(23456),
"int32_property_name": pdata.NewAttributeValueInt(34567),
"int64_property_name": pdata.NewAttributeValueInt(45678),
"float32_property_name": pdata.NewAttributeValueDouble(12345.678),
"float64_property_name": pdata.NewAttributeValueDouble(23456.789),
},
).CopyTo(properties.MapVal())
attrs["com.splunk.signalfx.event_properties"] = properties
return attrs
}(), 1000000001),
},
{
name: "unsupported properties",
Expand All @@ -100,23 +95,21 @@ func TestEventToPDataLogs(tt *testing.T) {
"uint_property": uint(12345),
},
},
expectedLog: newExpectedLog(
"some_event_type",
func() map[string]pdata.AttributeValue {
attrs := map[string]pdata.AttributeValue{
"com.splunk.signalfx.event_category": pdata.NewAttributeValueInt(10000000),
}
properties := pdata.NewAttributeValueMap()
pdata.NewAttributeMapFromMap(
map[string]pdata.AttributeValue{
"struct_property": pdata.NewAttributeValueString("{something}"),
"uint_property": pdata.NewAttributeValueString("12345"),
},
).CopyTo(properties.MapVal())
attrs["com.splunk.signalfx.event_properties"] = properties
return attrs
}(), 0,
),
expectedLog: newExpectedLog(func() map[string]pdata.AttributeValue {
attrs := map[string]pdata.AttributeValue{
"com.splunk.signalfx.event_category": pdata.NewAttributeValueInt(10000000),
"com.splunk.signalfx.event_type": pdata.NewAttributeValueString("some_event_type"),
}
properties := pdata.NewAttributeValueMap()
pdata.NewAttributeMapFromMap(
map[string]pdata.AttributeValue{
"struct_property": pdata.NewAttributeValueString("{something}"),
"uint_property": pdata.NewAttributeValueString("12345"),
},
).CopyTo(properties.MapVal())
attrs["com.splunk.signalfx.event_properties"] = properties
return attrs
}(), 0),
},
} {
tt.Run(test.name, func(t *testing.T) {
Expand All @@ -126,10 +119,9 @@ func TestEventToPDataLogs(tt *testing.T) {
}
}

func newExpectedLog(eventType string, properties map[string]pdata.AttributeValue, timestamp uint64) pdata.Logs {
func newExpectedLog(properties map[string]pdata.AttributeValue, timestamp uint64) pdata.Logs {
ld := pdata.NewLogs()
lr := ld.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty().LogRecords().AppendEmpty()
lr.SetName(eventType)
lr.SetTimestamp(pdata.Timestamp(timestamp))

attrs := lr.Attributes()
Expand All @@ -144,7 +136,6 @@ func assertLogsEqual(t *testing.T, expected, received pdata.Logs) {
expectedLog := expected.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).LogRecords().At(0)
receivedLog := received.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).LogRecords().At(0)

assert.Equal(t, expectedLog.Name(), receivedLog.Name())
assert.Equal(t, expectedLog.Timestamp(), receivedLog.Timestamp())

assertAttributeMapContainsAll(t, expectedLog.Attributes(), receivedLog.Attributes())
Expand Down
4 changes: 3 additions & 1 deletion internal/receiver/smartagentreceiver/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ func TestSendEvent(t *testing.T) {
require.Equal(t, 1, len(received))
log := received[0]
logRecord := log.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).LogRecords().At(0)
assert.Equal(t, "my_event", logRecord.Name())
attributes := logRecord.Attributes()
eventType, ok := attributes.Get("com.splunk.signalfx.event_type")
require.True(t, ok)
assert.Equal(t, "my_event", eventType.StringVal())
eventProperties, ok := attributes.Get("com.splunk.signalfx.event_properties")
require.True(t, ok)
val, ok := eventProperties.MapVal().Get("property")
Expand Down

0 comments on commit c2db908

Please sign in to comment.