Skip to content

Commit 393eccf

Browse files
atoulmef7o
authored andcommitted
[exporter/splunkhec] drop empty log events (open-telemetry#34871)
**Description:** Drop empty log events Log records with no body are dropped by Splunk on reception as they contain no log message, albeit they may have attributes. This PR removes those logs from consideration to be exported. This is in tune with the behavior of splunkhecreceiver, which refuses HEC events with no event (open-telemetry#19769)
1 parent d935fc8 commit 393eccf

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

.chloggen/drop_empty_events.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: splunkhecexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Drop empty log events
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [34871]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
Log records with no body are dropped by Splunk on reception
20+
as they contain no log message, albeit they may have attributes.
21+
22+
This PR removes those logs from consideration to be exported.
23+
24+
This is in tune with the behavior of splunkhecreceiver, which refuses HEC events with no event (#19769)
25+
26+
27+
# If your change doesn't affect end users or the exported elements of any package,
28+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
29+
# Optional: The change log or logs in which this entry should be included.
30+
# e.g. '[user]' or '[user, api]'
31+
# Include 'user' if the change is relevant to end users.
32+
# Include 'api' if there is a change to a library API.
33+
# Default: '[user]'
34+
change_logs: []

exporter/splunkhecexporter/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ func (c *client) fillLogsBuffer(logs plog.Logs, buf buffer, is iterState) (iterS
205205
} else {
206206
// Parsing log record to Splunk event.
207207
event := mapLogRecordToSplunkEvent(rl.Resource(), logRecord, c.config)
208+
if event == nil {
209+
// TODO record this drop as a metric
210+
continue
211+
}
208212

209213
// JSON encoding event and writing to buffer.
210214
var err error

exporter/splunkhecexporter/logdata_to_splunk.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const (
2424
)
2525

2626
func mapLogRecordToSplunkEvent(res pcommon.Resource, lr plog.LogRecord, config *Config) *splunk.Event {
27+
body := lr.Body().AsRaw()
28+
if body == nil || body == "" {
29+
// events with no body are rejected by Splunk.
30+
return nil
31+
}
32+
2733
host := unknownHostName
2834
source := config.Source
2935
sourcetype := config.SourceType
@@ -83,11 +89,6 @@ func mapLogRecordToSplunkEvent(res pcommon.Resource, lr plog.LogRecord, config *
8389
return true
8490
})
8591

86-
body := lr.Body().AsRaw()
87-
if body == nil {
88-
body = ""
89-
}
90-
9192
return &splunk.Event{
9293
Time: nanoTimestampToEpochMilliseconds(lr.Timestamp()),
9394
Host: host,

exporter/splunkhecexporter/logdata_to_splunk_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,13 @@ func Test_mapLogRecordToSplunkEvent(t *testing.T) {
187187
config.SourceType = "sourcetype"
188188
return config
189189
},
190-
wantSplunkEvents: []*splunk.Event{
191-
commonLogSplunkEvent("", 0, map[string]any{}, "unknown", "source", "sourcetype"),
192-
},
190+
wantSplunkEvents: []*splunk.Event{},
193191
},
194192
{
195193
name: "with span and trace id",
196194
logRecordFn: func() plog.LogRecord {
197195
logRecord := plog.NewLogRecord()
196+
logRecord.Body().SetStr("foo")
198197
logRecord.SetSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 50})
199198
logRecord.SetTraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100})
200199
return logRecord
@@ -207,7 +206,7 @@ func Test_mapLogRecordToSplunkEvent(t *testing.T) {
207206
return config
208207
},
209208
wantSplunkEvents: func() []*splunk.Event {
210-
event := commonLogSplunkEvent("", 0, map[string]any{}, "unknown", "source", "sourcetype")
209+
event := commonLogSplunkEvent("foo", 0, map[string]any{}, "unknown", "source", "sourcetype")
211210
event.Fields["span_id"] = "0000000000000032"
212211
event.Fields["trace_id"] = "00000000000000000000000000000064"
213212
return []*splunk.Event{event}
@@ -329,10 +328,7 @@ func Test_mapLogRecordToSplunkEvent(t *testing.T) {
329328
config.SourceType = "sourcetype"
330329
return config
331330
},
332-
wantSplunkEvents: []*splunk.Event{
333-
commonLogSplunkEvent("", ts, map[string]any{"custom": "custom"},
334-
"myhost", "myapp", "myapp-type"),
335-
},
331+
wantSplunkEvents: []*splunk.Event{},
336332
},
337333
{
338334
name: "with array body",
@@ -449,13 +445,7 @@ func commonLogSplunkEvent(
449445

450446
func Test_emptyLogRecord(t *testing.T) {
451447
event := mapLogRecordToSplunkEvent(pcommon.NewResource(), plog.NewLogRecord(), &Config{})
452-
assert.Zero(t, event.Time)
453-
assert.Equal(t, event.Host, "unknown")
454-
assert.Zero(t, event.Source)
455-
assert.Zero(t, event.SourceType)
456-
assert.Zero(t, event.Index)
457-
assert.Equal(t, "", event.Event)
458-
assert.Empty(t, event.Fields)
448+
assert.Nil(t, event)
459449
}
460450

461451
func Test_nanoTimestampToEpochMilliseconds(t *testing.T) {

0 commit comments

Comments
 (0)