Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/splunkhec] Include trace and span id if set in log record #3850

Merged
merged 1 commit into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## 💡 Enhancements 💡

- `tailsampling` processor: Add new policy `latency` (#3750)
- `splunkhec` exporter: Include `trace_id` and `span_id` if set (#3850)

## v0.28.0

Expand Down
14 changes: 14 additions & 0 deletions exporter/splunkhecexporter/logdata_to_splunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)

const (
// Keys are taken from https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/overview.md#trace-context-in-legacy-formats.
// spanIDFieldKey is the key used in log event for the span id (if any).
jrcamp marked this conversation as resolved.
Show resolved Hide resolved
spanIDFieldKey = "span_id"
// traceIDFieldKey is the key used in the log event for the trace id (if any).
traceIDFieldKey = "trace_id"
)

// Composite index of a log record in pdata.Logs.
type logIndex struct {
// Index in orig list (i.e. root parent index).
Expand All @@ -47,6 +55,12 @@ func mapLogRecordToSplunkEvent(res pdata.Resource, lr pdata.LogRecord, config *C
if lr.Name() != "" {
fields[splunk.NameLabel] = lr.Name()
}
if spanID := lr.SpanID().HexString(); spanID != "" {
fields[spanIDFieldKey] = spanID
}
if traceID := lr.TraceID().HexString(); traceID != "" {
fields[traceIDFieldKey] = traceID
}
res.Attributes().Range(func(k string, v pdata.AttributeValue) bool {
switch k {
case conventions.AttributeHostName:
Expand Down
22 changes: 22 additions & 0 deletions exporter/splunkhecexporter/logdata_to_splunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ func Test_mapLogRecordToSplunkEvent(t *testing.T) {
commonLogSplunkEvent(nil, 0, map[string]interface{}{}, "unknown", "source", "sourcetype"),
},
},
{
name: "with span and trace id",
logRecordFn: func() pdata.LogRecord {
logRecord := pdata.NewLogRecord()
logRecord.SetSpanID(pdata.NewSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 50}))
logRecord.SetTraceID(pdata.NewTraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100}))
return logRecord
},
logResourceFn: pdata.NewResource,
configDataFn: func() *Config {
return &Config{
Source: "source",
SourceType: "sourcetype",
}
},
wantSplunkEvents: func() []*splunk.Event {
event := commonLogSplunkEvent(nil, 0, map[string]interface{}{}, "unknown", "source", "sourcetype")
event.Fields["span_id"] = "0000000000000032"
event.Fields["trace_id"] = "00000000000000000000000000000064"
return []*splunk.Event{event}
}(),
},
{
name: "with double body",
logRecordFn: func() pdata.LogRecord {
Expand Down