Skip to content

Commit

Permalink
OTEL-919: Add support for nested log attributes
Browse files Browse the repository at this point in the history
This PR adds support for nested log attributes.

Fixes: open-telemetry/opentelemetry-collector-contrib#26382
  • Loading branch information
mackjmr committed Dec 1, 2023
1 parent 443feb4 commit 2de59fe
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/otlp/logs/logs_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func Transform(lr plog.LogRecord, res pcommon.Resource, logger *zap.Logger) data
tagStr := strings.Join(tags, ",")
l.Ddtags = datadog.PtrString(tagStr)
default:
l.AdditionalProperties[k] = v.AsString()
m := flattenAttribute(k, v, 1)
for k, v := range m {
l.AdditionalProperties[k] = v
}
}
return true
})
Expand Down Expand Up @@ -172,6 +175,26 @@ func Transform(lr plog.LogRecord, res pcommon.Resource, logger *zap.Logger) data
return l
}

func flattenAttribute(key string, val pcommon.Value, depth int) map[string]string {
result := make(map[string]string)

if val.Type() != pcommon.ValueTypeMap || depth == 10 {
result[key] = val.AsString()
return result
}

val.Map().Range(func(k string, v pcommon.Value) bool {
newKey := key + "." + k
nestedResult := flattenAttribute(newKey, v, depth+1)
for nk, nv := range nestedResult {
result[nk] = nv
}
return true
})

return result
}

func extractHostNameAndServiceName(resourceAttrs pcommon.Map, logAttrs pcommon.Map) (host string, service string) {
if src, ok := attributes.SourceFromAttrs(resourceAttrs); ok && src.Kind == source.HostnameKind {
host = src.Identifier
Expand Down
78 changes: 78 additions & 0 deletions pkg/otlp/logs/logs_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,84 @@ func TestTransform(t *testing.T) {
},
},
},
{
name: "Nestings",
args: args{
lr: func() plog.LogRecord {
l := plog.NewLogRecord()
sl := l.Attributes().PutEmptyMap("root")
sl.FromRaw(map[string]any{
"nest1": map[string]any{
"nest2": "val",
},
"nest12": map[string]any{
"nest22": map[string]any{
"nest3": "val2",
},
},
})
return l
}(),
res: func() pcommon.Resource {
r := pcommon.NewResource()
return r
}(),
},
want: datadogV2.HTTPLogItem{
Ddtags: datadog.PtrString(""),
Message: *datadog.PtrString(""),
AdditionalProperties: map[string]string{
"root.nest1.nest2": "val",
"root.nest12.nest22.nest3": "val2",
"status": "",
},
},
},
{
name: "Too many nestings",
args: args{
lr: func() plog.LogRecord {
l := plog.NewLogRecord()
sl := l.Attributes().PutEmptyMap("nest1")
sl.FromRaw(map[string]any{
"nest2": map[string]any{
"nest3": map[string]any{
"nest4": map[string]any{
"nest5": map[string]any{
"nest6": map[string]any{
"nest7": map[string]any{
"nest8": map[string]any{
"nest9": map[string]any{
"nest10": map[string]any{
"nest11": map[string]any{
"nest12": "ok",
},
},
},
},
},
},
},
},
},
},
})
return l
}(),
res: func() pcommon.Resource {
r := pcommon.NewResource()
return r
}(),
},
want: datadogV2.HTTPLogItem{
Ddtags: datadog.PtrString(""),
Message: *datadog.PtrString(""),
AdditionalProperties: map[string]string{
"nest1.nest2.nest3.nest4.nest5.nest6.nest7.nest8.nest9.nest10": "{\"nest11\":{\"nest12\":\"ok\"}}",
"status": "",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 2de59fe

Please sign in to comment.