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

[pkg/ottl] Add support for setting maps in Values #16352

Merged
merged 3 commits into from
Nov 18, 2022
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/ottl-handle-maps.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, 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:
9 changes: 7 additions & 2 deletions pkg/ottl/contexts/internal/ottlcommon/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
210 changes: 202 additions & 8 deletions pkg/ottl/contexts/ottldatapoint/datapoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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"},
Expand All @@ -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},
Expand All @@ -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},
Expand All @@ -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},
Expand All @@ -1337,14 +1487,52 @@ 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}},
modified: func(datapoint pmetric.SummaryDataPoint) {
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) {
Expand Down Expand Up @@ -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) {
Expand Down
Loading