Skip to content

Commit

Permalink
Add custom log source support (#28745)
Browse files Browse the repository at this point in the history
  • Loading branch information
mackjmr committed Aug 30, 2024
1 parent 9ce02da commit 09b07eb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func (e *Exporter) ConsumeLogs(ctx context.Context, ld plog.Logs) (err error) {
origin := message.NewOrigin(e.logSource)
origin.SetTags(tags)
origin.SetService(service)
origin.SetSource(e.logSource.Name)
if src, ok := ddLog.AdditionalProperties["datadog.log.source"]; ok {
origin.SetSource(src)
} else {
origin.SetSource(e.logSource.Name)
}

content, err := ddLog.MarshalJSON()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestLogsExporter(t *testing.T) {
lrr := testutil.GenerateLogsOneLogRecord()
ldd := lrr.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0)
ldd.Attributes().PutStr("message", "hello")
ldd.Attributes().PutStr("datadog.log.source", "custom_source")
return lrr
}(),
otelSource: otelSource,
Expand All @@ -83,6 +84,41 @@ func TestLogsExporter(t *testing.T) {
"message": "hello",
"app": "server",
"instance_num": "1",
"datadog.log.source": "custom_source",
"@timestamp": testutil.TestLogTime.Format("2006-01-02T15:04:05.000Z07:00"),
"status": "Info",
"dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())),
"dd.trace_id": fmt.Sprintf("%d", traceIDToUint64(ld.TraceID())),
"otel.severity_text": "Info",
"otel.severity_number": "9",
"otel.span_id": spanIDToHexOrEmptyString(ld.SpanID()),
"otel.trace_id": traceIDToHexOrEmptyString(ld.TraceID()),
"otel.timestamp": fmt.Sprintf("%d", testutil.TestLogTime.UnixNano()),
"resource-attr": "resource-attr-val-1",
},
},
expectedTags: [][]string{{"otel_source:datadog_agent"}},
},
{
name: "resource-attribute-source",
args: args{
ld: func() plog.Logs {
l := testutil.GenerateLogsOneLogRecord()
rl := l.ResourceLogs().At(0)
resourceAttrs := rl.Resource().Attributes()
resourceAttrs.PutStr("datadog.log.source", "custom_source_rattr")
return l
}(),
otelSource: otelSource,
logSourceName: LogSourceName,
},

want: testutil.JSONLogs{
{
"message": "This is a log message",
"app": "server",
"instance_num": "1",
"datadog.log.source": "custom_source_rattr",
"@timestamp": testutil.TestLogTime.Format("2006-01-02T15:04:05.000Z07:00"),
"status": "Info",
"dd.span_id": fmt.Sprintf("%d", spanIDToUint64(ld.SpanID())),
Expand Down Expand Up @@ -186,7 +222,7 @@ func TestLogsExporter(t *testing.T) {
return lrr
}(),
otelSource: "datadog_exporter",
logSourceName: "custom_source",
logSourceName: "",
},

want: testutil.JSONLogs{
Expand Down Expand Up @@ -241,7 +277,11 @@ func TestLogsExporter(t *testing.T) {
output := <-testChannel
outputJSON := make(map[string]interface{})
json.Unmarshal(output.GetContent(), &outputJSON)
assert.Equal(t, tt.args.logSourceName, output.Origin.Source())
if src, ok := outputJSON["datadog.log.source"]; ok {
assert.Equal(t, src, output.Origin.Source())
} else {
assert.Equal(t, tt.args.logSourceName, output.Origin.Source())
}
assert.Equal(t, tt.expectedTags[i], output.Origin.Tags(nil))
ans = append(ans, outputJSON)
}
Expand Down
11 changes: 11 additions & 0 deletions releasenotes/notes/mackjmr-otlp-log-source-f9bc51dcd803a238.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
enhancements:
- |
Add support for setting a custom log source from resource attribute or log attribute `datadog.log.source`.

0 comments on commit 09b07eb

Please sign in to comment.