Skip to content

Commit

Permalink
[exporter/splunkhec] Retain all otel attributes (#2712)
Browse files Browse the repository at this point in the history
Previously we were stripping service.name and host.name since they have
dedicated entries in HEC. However we now want to keep these so that
service.name and host.name will match across logs, metrics, and traces.
  • Loading branch information
jrcamp authored and pmatyjasek-sumo committed Apr 28, 2021
1 parent 4a20e50 commit 0efaa64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions exporter/splunkhecexporter/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ func TestReceiveTraces(t *testing.T) {
func TestReceiveLogs(t *testing.T) {
actual, err := runLogExport(true, 3, t)
assert.NoError(t, err)
expected := `{"host":"myhost","source":"myapp","sourcetype":"myapp-type","index":"myindex","event":"mylog","fields":{"custom":"custom"}}`
expected := `{"host":"myhost","source":"myapp","sourcetype":"myapp-type","index":"myindex","event":"mylog","fields":{"custom":"custom","host.name":"myhost","service.name":"myapp"}}`
expected += "\n\r\n\r\n"
expected += `{"time":0.001,"host":"myhost","source":"myapp","sourcetype":"myapp-type","index":"myindex","event":"mylog","fields":{"custom":"custom"}}`
expected += `{"time":0.001,"host":"myhost","source":"myapp","sourcetype":"myapp-type","index":"myindex","event":"mylog","fields":{"custom":"custom","host.name":"myhost","service.name":"myapp"}}`
expected += "\n\r\n\r\n"
expected += `{"time":0.002,"host":"myhost","source":"myapp","sourcetype":"myapp-type","index":"myindex","event":"mylog","fields":{"custom":"custom"}}`
expected += `{"time":0.002,"host":"myhost","source":"myapp","sourcetype":"myapp-type","index":"myindex","event":"mylog","fields":{"custom":"custom","host.name":"myhost","service.name":"myapp"}}`
expected += "\n\r\n\r\n"
assert.Equal(t, expected, actual)
}
Expand Down
13 changes: 8 additions & 5 deletions exporter/splunkhecexporter/logdata_to_splunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ func mapLogRecordToSplunkEvent(lr pdata.LogRecord, config *Config, logger *zap.L
index := config.Index
fields := map[string]interface{}{}
lr.Attributes().ForEach(func(k string, v pdata.AttributeValue) {
if k == conventions.AttributeHostName {
switch k {
case conventions.AttributeHostName:
host = v.StringVal()
} else if k == conventions.AttributeServiceName {
fields[k] = v.StringVal()
case conventions.AttributeServiceName:
source = v.StringVal()
} else if k == splunk.SourcetypeLabel {
fields[k] = v.StringVal()
case splunk.SourcetypeLabel:
sourcetype = v.StringVal()
} else if k == splunk.IndexLabel {
case splunk.IndexLabel:
index = v.StringVal()
} else {
default:
fields[k] = convertAttributeValue(v, logger)
}
})
Expand Down
21 changes: 13 additions & 8 deletions exporter/splunkhecexporter/logdata_to_splunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent("mylog", ts, map[string]interface{}{"custom": "custom"}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent("mylog", ts, map[string]interface{}{"custom": "custom", "service.name": "myapp", "host.name": "myhost"},
"myhost", "myapp", "myapp-type"),
},
},
{
Expand All @@ -77,7 +78,7 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent("mylog", ts, map[string]interface{}{"foo": float64(123)}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent("mylog", ts, map[string]interface{}{"foo": float64(123), "service.name": "myapp", "host.name": "myhost"}, "myhost", "myapp", "myapp-type"),
},
},
{
Expand Down Expand Up @@ -134,7 +135,7 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent(float64(42), ts, map[string]interface{}{"custom": "custom"}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent(float64(42), ts, map[string]interface{}{"custom": "custom", "service.name": "myapp", "host.name": "myhost"}, "myhost", "myapp", "myapp-type"),
},
},
{
Expand All @@ -156,7 +157,7 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent(int64(42), ts, map[string]interface{}{"custom": "custom"}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent(int64(42), ts, map[string]interface{}{"custom": "custom", "service.name": "myapp", "host.name": "myhost"}, "myhost", "myapp", "myapp-type"),
},
},
{
Expand All @@ -178,7 +179,7 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent(true, ts, map[string]interface{}{"custom": "custom"}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent(true, ts, map[string]interface{}{"custom": "custom", "service.name": "myapp", "host.name": "myhost"}, "myhost", "myapp", "myapp-type"),
},
},
{
Expand All @@ -204,7 +205,9 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent(map[string]interface{}{"23": float64(45), "foo": "bar"}, ts, map[string]interface{}{"custom": "custom"}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent(map[string]interface{}{"23": float64(45), "foo": "bar"}, ts,
map[string]interface{}{"custom": "custom", "service.name": "myapp", "host.name": "myhost"},
"myhost", "myapp", "myapp-type"),
},
},
{
Expand All @@ -225,7 +228,8 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent(nil, ts, map[string]interface{}{"custom": "custom"}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent(nil, ts, map[string]interface{}{"custom": "custom", "service.name": "myapp", "host.name": "myhost"},
"myhost", "myapp", "myapp-type"),
},
},
{
Expand All @@ -250,7 +254,8 @@ func Test_logDataToSplunk(t *testing.T) {
}
},
wantSplunkEvents: []*splunk.Event{
commonLogSplunkEvent([]interface{}{"foo"}, ts, map[string]interface{}{"custom": "custom"}, "myhost", "myapp", "myapp-type"),
commonLogSplunkEvent([]interface{}{"foo"}, ts, map[string]interface{}{"custom": "custom", "service.name": "myapp", "host.name": "myhost"},
"myhost", "myapp", "myapp-type"),
},
},
}
Expand Down

0 comments on commit 0efaa64

Please sign in to comment.