Skip to content

Commit 63bbc75

Browse files
authored
[OTel] Serialize Maps and nil Better (#45042)
This simplifies the serialization of arrays / slices of `map[string]any`.
1 parent 2019007 commit 63bbc75

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

libbeat/otelbeat/otelmap/otelmap.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,11 @@ func ConvertNonPrimitive[T mapstrOrMap](m T) {
100100
if ref.Kind() == reflect.Slice || ref.Kind() == reflect.Array {
101101
s := make([]any, ref.Len())
102102
for i := 0; i < ref.Len(); i++ {
103-
elem := ref.Index(i)
104-
if elem.Kind() == reflect.Map && elem.Type().Key().Kind() == reflect.String && elem.Type().Elem().Kind() == reflect.Interface {
105-
if m, ok := elem.Interface().(map[string]any); ok {
106-
ConvertNonPrimitive(m)
107-
}
103+
elem := ref.Index(i).Interface()
104+
if m, ok := elem.(map[string]any); ok {
105+
ConvertNonPrimitive(m)
108106
}
109-
s[i] = elem.Interface()
107+
s[i] = elem
110108
}
111109
m[key] = s
112110
break // we figured out the type, so we don't need the unknown type case

libbeat/otelbeat/otelmap/otelmap_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,18 @@ func TestFromMapstrWithNestedData(t *testing.T) {
281281
"bool_slice": []bool{false, true},
282282
"inner": []mapstr.M{
283283
{
284-
"inner_int": 42,
284+
"inner_int": 42,
285+
"inner_map_slice": [1]any{nil},
285286
"inner_slice": []map[string]any{ // slice -> slice
286287
{"string": "string"},
287288
{"number": 12.3},
288289
},
289290
},
290291
{
291292
"inner_int": 43,
293+
"inner_map_slice": []any{
294+
map[string]any{"string": "string3"},
295+
},
292296
"inner_slice": [2]map[string]any{ // array -> slice
293297
{"string": "string2"},
294298
{"number": 12.4},
@@ -303,14 +307,18 @@ func TestFromMapstrWithNestedData(t *testing.T) {
303307
"bool_slice": []any{false, true},
304308
"inner": []any{
305309
map[string]any{
306-
"inner_int": 42,
310+
"inner_int": 42,
311+
"inner_map_slice": []any{nil},
307312
"inner_slice": []any{
308313
map[string]any{"string": "string"},
309314
map[string]any{"number": 12.3},
310315
},
311316
},
312317
map[string]any{
313318
"inner_int": 43,
319+
"inner_map_slice": []any{
320+
map[string]any{"string": "string3"},
321+
},
314322
"inner_slice": []any{
315323
map[string]any{"string": "string2"},
316324
map[string]any{"number": 12.4},

0 commit comments

Comments
 (0)