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

OTEL-919: Add support for nested log attributes #207

Merged
merged 3 commits into from
Dec 1, 2023
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
16 changes: 16 additions & 0 deletions .chloggen/mackjmr_add-support-for-nested-log-attributes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component (e.g. pkg/quantile)
component: pkg/otlp/logs

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for nested log attributes.

# The PR related to this change
issues: [207]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
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 {
mackjmr marked this conversation as resolved.
Show resolved Hide resolved
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
111 changes: 111 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,117 @@ func TestTransform(t *testing.T) {
},
},
},
{
name: "Nestings",
args: args{
lr: func() plog.LogRecord {
l := plog.NewLogRecord()
l.Attributes().FromRaw(
map[string]any{
"root": map[string]any{
"nest1": map[string]any{
"nest2": "val",
},
"nest12": map[string]any{
"nest22": map[string]any{
"nest3": "val2",
},
},
"nest13": "val3",
},
},
)
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",
"root.nest13": "val3",
"status": "",
},
},
},
{
name: "Nil Map",
args: args{
lr: func() plog.LogRecord {
l := plog.NewLogRecord()
l.Attributes().FromRaw(nil)
return l
}(),
res: func() pcommon.Resource {
r := pcommon.NewResource()
return r
}(),
},
want: datadogV2.HTTPLogItem{
Ddtags: datadog.PtrString(""),
Message: *datadog.PtrString(""),
AdditionalProperties: map[string]string{
"status": "",
},
},
},
{
name: "Too many nestings",
mackjmr marked this conversation as resolved.
Show resolved Hide resolved
args: args{
lr: func() plog.LogRecord {
l := plog.NewLogRecord()
l.Attributes().FromRaw(
map[string]any{
"nest1": 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",
},
},
},
},
},
},
"nest14": map[string]any{
"nest15": "ok2",
},
},
},
},
},
},
},
)
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\"}}",
"nest1.nest2.nest3.nest4.nest5.nest14.nest15": "ok2",
"status": "",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading