diff --git a/exporter/splunkhecexporter/client_test.go b/exporter/splunkhecexporter/client_test.go index abfc7df85d9a..7ae9271f664b 100644 --- a/exporter/splunkhecexporter/client_test.go +++ b/exporter/splunkhecexporter/client_test.go @@ -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) } diff --git a/exporter/splunkhecexporter/logdata_to_splunk.go b/exporter/splunkhecexporter/logdata_to_splunk.go index 02bf1979ec3c..b9c4259b002f 100644 --- a/exporter/splunkhecexporter/logdata_to_splunk.go +++ b/exporter/splunkhecexporter/logdata_to_splunk.go @@ -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) } }) diff --git a/exporter/splunkhecexporter/logdata_to_splunk_test.go b/exporter/splunkhecexporter/logdata_to_splunk_test.go index 335db39476a8..7cd129716956 100644 --- a/exporter/splunkhecexporter/logdata_to_splunk_test.go +++ b/exporter/splunkhecexporter/logdata_to_splunk_test.go @@ -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"), }, }, { @@ -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"), }, }, { @@ -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"), }, }, { @@ -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"), }, }, { @@ -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"), }, }, { @@ -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"), }, }, { @@ -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"), }, }, { @@ -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"), }, }, }