Skip to content

Commit

Permalink
Remove LogRecord Name field usage from signalfx exporter and receiver
Browse files Browse the repository at this point in the history
The LogRecord Name field [is removed](open-telemetry/opentelemetry-specification#2271) from the specification and is [going to be removed](open-telemetry/opentelemetry-proto#357) from the OTLP.
  • Loading branch information
tigrannajaryan committed Feb 3, 2022
1 parent 2bcba8c commit 957ebfe
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
7 changes: 4 additions & 3 deletions exporter/signalfxexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ func makeSampleResourceLogs() pdata.Logs {
out := pdata.NewLogs()
l := out.ResourceLogs().AppendEmpty().InstrumentationLibraryLogs().AppendEmpty().LogRecords().AppendEmpty()

l.SetName("shutdown")
l.SetTimestamp(pdata.Timestamp(1000))
attrs := l.Attributes()

Expand All @@ -490,6 +489,7 @@ func makeSampleResourceLogs() pdata.Logs {
propMap.Sort()
attrs.Insert("com.splunk.signalfx.event_properties", propMapVal)
attrs.Insert("com.splunk.signalfx.event_category", pdata.NewAttributeValueInt(int64(sfxpb.EventCategory_USER_DEFINED)))
attrs.Insert("com.splunk.signalfx.event_type", pdata.NewAttributeValueString("shutdown"))

l.Attributes().Sort()

Expand All @@ -515,7 +515,9 @@ func TestConsumeEventData(t *testing.T) {
name: "no_event_attribute",
resourceLogs: func() pdata.Logs {
out := makeSampleResourceLogs()
out.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).LogRecords().At(0).Attributes().Delete("com.splunk.signalfx.event_category")
attrs := out.ResourceLogs().At(0).InstrumentationLibraryLogs().At(0).LogRecords().At(0).Attributes()
attrs.Delete("com.splunk.signalfx.event_category")
attrs.Delete("com.splunk.signalfx.event_type")
return out
}(),
reqTestFunc: nil,
Expand Down Expand Up @@ -713,7 +715,6 @@ func generateLargeEventBatch() pdata.Logs {
ts := time.Now()
for i := 0; i < batchSize; i++ {
lr := logs.AppendEmpty()
lr.SetName("test_" + strconv.Itoa(i))
lr.Attributes().InsertString("k0", "k1")
lr.Attributes().InsertNull("com.splunk.signalfx.event_category")
lr.SetTimestamp(pdata.NewTimestampFromTime(ts))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,8 @@ func LogRecordSliceToSignalFxV2(
func convertLogRecord(lr pdata.LogRecord, resourceAttrs pdata.AttributeMap, logger *zap.Logger) (*sfxpb.Event, bool) {
attrs := lr.Attributes()

categoryVal, ok := attrs.Get(splunk.SFxEventCategoryKey)
if !ok {
return nil, false
}

var event sfxpb.Event

if categoryVal.Type() == pdata.AttributeValueTypeInt {
asCat := sfxpb.EventCategory(categoryVal.IntVal())
event.Category = &asCat
}

if mapVal, ok := attrs.Get(splunk.SFxEventPropertiesKey); ok && mapVal.Type() == pdata.AttributeValueTypeMap {
mapVal.MapVal().Range(func(k string, v pdata.AttributeValue) bool {
val, err := attributeValToPropertyVal(v)
Expand Down Expand Up @@ -87,9 +77,23 @@ func convertLogRecord(lr pdata.LogRecord, resourceAttrs pdata.AttributeMap, logg
return true
})

categorySet := false
addDimension := func(k string, v pdata.AttributeValue) bool {
// Skip internal attributes
if k == splunk.SFxEventCategoryKey || k == splunk.SFxEventPropertiesKey {
switch k {
case splunk.SFxEventCategoryKey:
categorySet = true
if v.Type() == pdata.AttributeValueTypeInt {
asCat := sfxpb.EventCategory(v.IntVal())
event.Category = &asCat
}
return true
case splunk.SFxEventPropertiesKey:
return true
case splunk.SFxEventType:
if v.Type() == pdata.AttributeValueTypeString {
event.EventType = v.StringVal()
}
return true
}

Expand All @@ -108,7 +112,10 @@ func convertLogRecord(lr pdata.LogRecord, resourceAttrs pdata.AttributeMap, logg
resourceAttrsForDimensions.Range(addDimension)
attrs.Range(addDimension)

event.EventType = lr.Name()
if !categorySet {
return nil, false
}

// Convert nanoseconds to nearest milliseconds, which is the unit of
// SignalFx event timestamps.
event.Timestamp = int64(lr.Timestamp()) / 1e6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func TestLogDataToSignalFxEvents(t *testing.T) {
logSlice := ilLogs.AppendEmpty().LogRecords()

l := logSlice.AppendEmpty()
l.SetName("shutdown")
l.SetTimestamp(pdata.NewTimestampFromTime(now.Truncate(time.Millisecond)))
attrs := l.Attributes()

Expand All @@ -80,6 +79,7 @@ func TestLogDataToSignalFxEvents(t *testing.T) {
propMap.Sort()
attrs.Insert("com.splunk.signalfx.event_properties", propMapVal)
attrs.Insert("com.splunk.signalfx.event_category", pdata.NewAttributeValueInt(int64(sfxpb.EventCategory_USER_DEFINED)))
attrs.Insert("com.splunk.signalfx.event_type", pdata.NewAttributeValueString("shutdown"))

l.Attributes().Sort()

Expand Down
1 change: 1 addition & 0 deletions internal/splunk/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
SFxAccessTokenLabel = "com.splunk.signalfx.access_token" // #nosec
SFxEventCategoryKey = "com.splunk.signalfx.event_category"
SFxEventPropertiesKey = "com.splunk.signalfx.event_properties"
SFxEventType = "com.splunk.signalfx.event_type"
DefaultSourceTypeLabel = "com.splunk.sourcetype"
DefaultSourceLabel = "com.splunk.source"
DefaultIndexLabel = "com.splunk.index"
Expand Down
14 changes: 8 additions & 6 deletions receiver/signalfxreceiver/signalfxv2_event_to_logdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ func signalFxV2EventsToLogRecords(events []*sfxpb.Event, lrs pdata.LogRecordSlic
for _, event := range events {
lr := lrs.AppendEmpty()

// The EventType field is the most logical "name" of the event.
lr.SetName(event.EventType)
attrs := lr.Attributes()
attrs.Clear()
attrs.EnsureCapacity(2 + len(event.Dimensions) + len(event.Properties))

// The EventType field is stored as an attribute.
if event.EventType != "" {
attrs.InsertString(splunk.SFxEventType, event.EventType)
}

// SignalFx timestamps are in millis so convert to nanos by multiplying
// by 1 million.
lr.SetTimestamp(pdata.Timestamp(event.Timestamp * 1e6))

attrs := lr.Attributes()
attrs.Clear()
attrs.EnsureCapacity(1 + len(event.Dimensions) + len(event.Properties))

if event.Category != nil {
attrs.InsertInt(splunk.SFxEventCategoryKey, int64(*event.Category))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func TestSignalFxV2EventsToLogData(t *testing.T) {
buildDefaultLogs := func() pdata.LogRecordSlice {
logSlice := pdata.NewLogRecordSlice()
l := logSlice.AppendEmpty()
l.SetName("shutdown")
l.SetTimestamp(pdata.NewTimestampFromTime(now.Truncate(time.Millisecond)))
attrs := l.Attributes()
attrs.InsertString("com.splunk.signalfx.event_type", "shutdown")
attrs.InsertString("k0", "v0")
attrs.InsertString("k1", "v1")
attrs.InsertString("k2", "v2")
Expand Down

0 comments on commit 957ebfe

Please sign in to comment.