Skip to content

Commit 09b07eb

Browse files
authored
Add custom log source support (#28745)
1 parent 9ce02da commit 09b07eb

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

comp/otelcol/otlp/components/exporter/logsagentexporter/logs_exporter.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ func (e *Exporter) ConsumeLogs(ctx context.Context, ld plog.Logs) (err error) {
8080
origin := message.NewOrigin(e.logSource)
8181
origin.SetTags(tags)
8282
origin.SetService(service)
83-
origin.SetSource(e.logSource.Name)
83+
if src, ok := ddLog.AdditionalProperties["datadog.log.source"]; ok {
84+
origin.SetSource(src)
85+
} else {
86+
origin.SetSource(e.logSource.Name)
87+
}
8488

8589
content, err := ddLog.MarshalJSON()
8690
if err != nil {

comp/otelcol/otlp/components/exporter/logsagentexporter/logs_exporter_test.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestLogsExporter(t *testing.T) {
7272
lrr := testutil.GenerateLogsOneLogRecord()
7373
ldd := lrr.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0)
7474
ldd.Attributes().PutStr("message", "hello")
75+
ldd.Attributes().PutStr("datadog.log.source", "custom_source")
7576
return lrr
7677
}(),
7778
otelSource: otelSource,
@@ -83,6 +84,41 @@ func TestLogsExporter(t *testing.T) {
8384
"message": "hello",
8485
"app": "server",
8586
"instance_num": "1",
87+
"datadog.log.source": "custom_source",
88+
"@timestamp": testutil.TestLogTime.Format("2006-01-02T15:04:05.000Z07:00"),
89+
"status": "Info",
90+
"dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())),
91+
"dd.trace_id": fmt.Sprintf("%d", traceIDToUint64(ld.TraceID())),
92+
"otel.severity_text": "Info",
93+
"otel.severity_number": "9",
94+
"otel.span_id": spanIDToHexOrEmptyString(ld.SpanID()),
95+
"otel.trace_id": traceIDToHexOrEmptyString(ld.TraceID()),
96+
"otel.timestamp": fmt.Sprintf("%d", testutil.TestLogTime.UnixNano()),
97+
"resource-attr": "resource-attr-val-1",
98+
},
99+
},
100+
expectedTags: [][]string{{"otel_source:datadog_agent"}},
101+
},
102+
{
103+
name: "resource-attribute-source",
104+
args: args{
105+
ld: func() plog.Logs {
106+
l := testutil.GenerateLogsOneLogRecord()
107+
rl := l.ResourceLogs().At(0)
108+
resourceAttrs := rl.Resource().Attributes()
109+
resourceAttrs.PutStr("datadog.log.source", "custom_source_rattr")
110+
return l
111+
}(),
112+
otelSource: otelSource,
113+
logSourceName: LogSourceName,
114+
},
115+
116+
want: testutil.JSONLogs{
117+
{
118+
"message": "This is a log message",
119+
"app": "server",
120+
"instance_num": "1",
121+
"datadog.log.source": "custom_source_rattr",
86122
"@timestamp": testutil.TestLogTime.Format("2006-01-02T15:04:05.000Z07:00"),
87123
"status": "Info",
88124
"dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())),
@@ -186,7 +222,7 @@ func TestLogsExporter(t *testing.T) {
186222
return lrr
187223
}(),
188224
otelSource: "datadog_exporter",
189-
logSourceName: "custom_source",
225+
logSourceName: "",
190226
},
191227

192228
want: testutil.JSONLogs{
@@ -241,7 +277,11 @@ func TestLogsExporter(t *testing.T) {
241277
output := <-testChannel
242278
outputJSON := make(map[string]interface{})
243279
json.Unmarshal(output.GetContent(), &outputJSON)
244-
assert.Equal(t, tt.args.logSourceName, output.Origin.Source())
280+
if src, ok := outputJSON["datadog.log.source"]; ok {
281+
assert.Equal(t, src, output.Origin.Source())
282+
} else {
283+
assert.Equal(t, tt.args.logSourceName, output.Origin.Source())
284+
}
245285
assert.Equal(t, tt.expectedTags[i], output.Origin.Tags(nil))
246286
ans = append(ans, outputJSON)
247287
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Each section from every release note are combined when the
2+
# CHANGELOG.rst is rendered. So the text needs to be worded so that
3+
# it does not depend on any information only available in another
4+
# section. This may mean repeating some details, but each section
5+
# must be readable independently of the other.
6+
#
7+
# Each section note must be formatted as reStructuredText.
8+
---
9+
enhancements:
10+
- |
11+
Add support for setting a custom log source from resource attribute or log attribute `datadog.log.source`.

0 commit comments

Comments
 (0)