diff --git a/.chloggen/ottl-handle-maps.yaml b/.chloggen/ottl-handle-maps.yaml new file mode 100755 index 000000000000..ed1e8a3d304f --- /dev/null +++ b/.chloggen/ottl-handle-maps.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: pkg/ottl + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add support for setting Maps in Values. This enables Contexts to set map values for attributes. + +# One or more tracking issues related to the change +issues: [16352] + +# (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: diff --git a/pkg/ottl/contexts/internal/ottlcommon/value.go b/pkg/ottl/contexts/internal/ottlcommon/value.go index 50544bad567d..a2b85decd2e8 100644 --- a/pkg/ottl/contexts/internal/ottlcommon/value.go +++ b/pkg/ottl/contexts/internal/ottlcommon/value.go @@ -85,7 +85,12 @@ func SetValue(value pcommon.Value, val interface{}) { for _, b := range v { value.Slice().AppendEmpty().SetEmptyBytes().FromRaw(b) } - default: - // TODO(anuraaga): Support set of map type. + case pcommon.Map: + v.CopyTo(value.SetEmptyMap()) + case map[string]interface{}: + value.SetEmptyMap() + for mk, mv := range v { + SetMapValue(value.Map(), mk, mv) + } } } diff --git a/pkg/ottl/contexts/ottldatapoint/datapoint_test.go b/pkg/ottl/contexts/ottldatapoint/datapoint_test.go index 17e828d3371f..7b64bb78c354 100644 --- a/pkg/ottl/contexts/ottldatapoint/datapoint_test.go +++ b/pkg/ottl/contexts/ottldatapoint/datapoint_test.go @@ -32,6 +32,15 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { newExemplars, newAttrs := createNewTelemetry() + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -287,6 +296,44 @@ func Test_newPathGetSetter_NumberDataPoint(t *testing.T) { datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refNumberDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.NumberDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refNumberDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.NumberDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -335,6 +382,15 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { newExemplars, newAttrs := createNewTelemetry() + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -614,6 +670,44 @@ func Test_newPathGetSetter_HistogramDataPoint(t *testing.T) { datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refHistogramDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.HistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refHistogramDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.HistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -668,6 +762,15 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { newNegative.SetOffset(10) newNegative.BucketCounts().FromRaw([]uint64{4, 5}) + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -1037,6 +1140,44 @@ func Test_newPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refExpoHistogramDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refExpoHistogramDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1085,13 +1226,22 @@ func createExpoHistogramDataPointTelemetry() pmetric.ExponentialHistogramDataPoi } func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { - refExpoHistogramDataPoint := createSummaryDataPointTelemetry() + refSummaryDataPoint := createSummaryDataPointTelemetry() _, newAttrs := createNewTelemetry() newQuartileValues := pmetric.NewSummaryDataPointValueAtQuantileSlice() newQuartileValues.AppendEmpty().SetValue(100) + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -1171,7 +1321,7 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { Name: "quantile_values", }, }, - orig: refExpoHistogramDataPoint.QuantileValues(), + orig: refSummaryDataPoint.QuantileValues(), newVal: newQuartileValues, modified: func(datapoint pmetric.SummaryDataPoint) { newQuartileValues.CopyTo(datapoint.QuantileValues()) @@ -1184,7 +1334,7 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { Name: "attributes", }, }, - orig: refExpoHistogramDataPoint.Attributes(), + orig: refSummaryDataPoint.Attributes(), newVal: newAttrs, modified: func(datapoint pmetric.SummaryDataPoint) { newAttrs.CopyTo(datapoint.Attributes()) @@ -1269,7 +1419,7 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { }, }, orig: func() pcommon.Slice { - val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_str") + val, _ := refSummaryDataPoint.Attributes().Get("arr_str") return val.Slice() }(), newVal: []string{"new"}, @@ -1286,7 +1436,7 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { }, }, orig: func() pcommon.Slice { - val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_bool") + val, _ := refSummaryDataPoint.Attributes().Get("arr_bool") return val.Slice() }(), newVal: []bool{false}, @@ -1303,7 +1453,7 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { }, }, orig: func() pcommon.Slice { - val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_int") + val, _ := refSummaryDataPoint.Attributes().Get("arr_int") return val.Slice() }(), newVal: []int64{20}, @@ -1320,7 +1470,7 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { }, }, orig: func() pcommon.Slice { - val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_float") + val, _ := refSummaryDataPoint.Attributes().Get("arr_float") return val.Slice() }(), newVal: []float64{2.0}, @@ -1337,7 +1487,7 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { }, }, orig: func() pcommon.Slice { - val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_bytes") + val, _ := refSummaryDataPoint.Attributes().Get("arr_bytes") return val.Slice() }(), newVal: [][]byte{{9, 6, 4}}, @@ -1345,6 +1495,44 @@ func Test_newPathGetSetter_SummaryDataPoint(t *testing.T) { datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refSummaryDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.SummaryDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refSummaryDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.SummaryDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1409,6 +1597,12 @@ func createAttributeTelemetry(attributes pcommon.Map) { arrBytes := attributes.PutEmptySlice("arr_bytes") arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{1, 2, 3}) arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{2, 3, 4}) + + pMap := attributes.PutEmptyMap("pMap") + pMap.PutStr("original", "map") + + m := attributes.PutEmptyMap("map") + m.PutStr("original", "map") } func Test_newPathGetSetter_Metric(t *testing.T) { diff --git a/pkg/ottl/contexts/ottllog/log_test.go b/pkg/ottl/contexts/ottllog/log_test.go index 386e75d2ef98..326344102eb8 100644 --- a/pkg/ottl/contexts/ottllog/log_test.go +++ b/pkg/ottl/contexts/ottllog/log_test.go @@ -41,6 +41,15 @@ func Test_newPathGetSetter(t *testing.T) { newAttrs := pcommon.NewMap() newAttrs.PutStr("hello", "world") + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -352,6 +361,44 @@ func Test_newPathGetSetter(t *testing.T) { log.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refLog.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(log plog.LogRecord, il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := log.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refLog.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(log plog.LogRecord, il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := log.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, { name: "dropped_attributes_count", path: []ottl.Field{ @@ -449,6 +496,12 @@ func createTelemetry() (plog.LogRecord, pcommon.InstrumentationScope, pcommon.Re arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{1, 2, 3}) arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{2, 3, 4}) + pMap := log.Attributes().PutEmptyMap("pMap") + pMap.PutStr("original", "map") + + m := log.Attributes().PutEmptyMap("map") + m.PutStr("original", "map") + log.SetDroppedAttributesCount(10) log.SetFlags(plog.LogRecordFlags(4)) diff --git a/pkg/ottl/contexts/ottlresource/resource_test.go b/pkg/ottl/contexts/ottlresource/resource_test.go index d2446339f8a4..633a28b961ca 100644 --- a/pkg/ottl/contexts/ottlresource/resource_test.go +++ b/pkg/ottl/contexts/ottlresource/resource_test.go @@ -31,6 +31,15 @@ func Test_newPathGetSetter(t *testing.T) { newAttrs := pcommon.NewMap() newAttrs.PutStr("hello", "world") + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -206,6 +215,44 @@ func Test_newPathGetSetter(t *testing.T) { resource.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refResource.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(resource pcommon.Resource) { + m := resource.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes mpa[string]interface", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refResource.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(resource pcommon.Resource) { + m := resource.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, { name: "dropped_attributes_count", path: []ottl.Field{ @@ -271,6 +318,12 @@ func createTelemetry() pcommon.Resource { arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{1, 2, 3}) arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{2, 3, 4}) + pMap := resource.Attributes().PutEmptyMap("pMap") + pMap.PutStr("original", "map") + + m := resource.Attributes().PutEmptyMap("map") + m.PutStr("original", "map") + resource.SetDroppedAttributesCount(10) return resource diff --git a/pkg/ottl/contexts/ottlscope/scope_test.go b/pkg/ottl/contexts/ottlscope/scope_test.go index ceb57327503a..2062af9260ab 100644 --- a/pkg/ottl/contexts/ottlscope/scope_test.go +++ b/pkg/ottl/contexts/ottlscope/scope_test.go @@ -31,6 +31,15 @@ func Test_newPathGetSetter(t *testing.T) { newAttrs := pcommon.NewMap() newAttrs.PutStr("hello", "world") + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -206,6 +215,44 @@ func Test_newPathGetSetter(t *testing.T) { is.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refIS.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := il.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refIS.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := il.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, { name: "dropped_attributes_count", path: []ottl.Field{ @@ -314,6 +361,12 @@ func createTelemetry() (pcommon.InstrumentationScope, pcommon.Resource) { arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{1, 2, 3}) arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{2, 3, 4}) + pMap := is.Attributes().PutEmptyMap("pMap") + pMap.PutStr("original", "map") + + m := is.Attributes().PutEmptyMap("map") + m.PutStr("original", "map") + resource := pcommon.NewResource() is.Attributes().CopyTo(resource.Attributes()) diff --git a/pkg/ottl/contexts/ottlspan/span_test.go b/pkg/ottl/contexts/ottlspan/span_test.go index 62b5fe4319e0..63af0690e00f 100644 --- a/pkg/ottl/contexts/ottlspan/span_test.go +++ b/pkg/ottl/contexts/ottlspan/span_test.go @@ -50,6 +50,15 @@ func Test_newPathGetSetter(t *testing.T) { newStatus := ptrace.NewStatus() newStatus.SetMessage("new status") + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -375,6 +384,44 @@ func Test_newPathGetSetter(t *testing.T) { span.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refSpan.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(span ptrace.Span, il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := span.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refSpan.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(span ptrace.Span, il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := span.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, { name: "dropped_attributes_count", path: []ottl.Field{ @@ -578,6 +625,12 @@ func createTelemetry() (ptrace.Span, pcommon.InstrumentationScope, pcommon.Resou arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{1, 2, 3}) arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{2, 3, 4}) + pMap := span.Attributes().PutEmptyMap("pMap") + pMap.PutStr("original", "map") + + m := span.Attributes().PutEmptyMap("map") + m.PutStr("original", "map") + span.SetDroppedAttributesCount(10) span.Events().AppendEmpty().SetName("event") diff --git a/pkg/ottl/contexts/ottlspanevent/span_events_test.go b/pkg/ottl/contexts/ottlspanevent/span_events_test.go index b5fc264082ef..9676cfa8f4c7 100644 --- a/pkg/ottl/contexts/ottlspanevent/span_events_test.go +++ b/pkg/ottl/contexts/ottlspanevent/span_events_test.go @@ -49,6 +49,15 @@ func Test_newPathGetSetter(t *testing.T) { newStatus := ptrace.NewStatus() newStatus.SetMessage("new status") + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]interface{}) + newMap2 := make(map[string]interface{}) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + tests := []struct { name string path []ottl.Field @@ -250,6 +259,44 @@ func Test_newPathGetSetter(t *testing.T) { spanEvent.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) }, }, + { + name: "attributes pcommon.Map", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("pMap"), + }, + }, + orig: func() pcommon.Map { + val, _ := refSpanEvent.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(spanEvent ptrace.SpanEvent, span ptrace.Span, il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := spanEvent.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]interface{}", + path: []ottl.Field{ + { + Name: "attributes", + MapKey: ottltest.Strp("map"), + }, + }, + orig: func() pcommon.Map { + val, _ := refSpanEvent.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(spanEvent ptrace.SpanEvent, span ptrace.Span, il pcommon.InstrumentationScope, resource pcommon.Resource) { + m := spanEvent.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, { name: "dropped_attributes_count", path: []ottl.Field{ @@ -360,6 +407,12 @@ func createTelemetry() (ptrace.SpanEvent, ptrace.Span, pcommon.InstrumentationSc arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{1, 2, 3}) arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{2, 3, 4}) + pMap := spanEvent.Attributes().PutEmptyMap("pMap") + pMap.PutStr("original", "map") + + m := spanEvent.Attributes().PutEmptyMap("map") + m.PutStr("original", "map") + span := ptrace.NewSpan() span.SetName("test")