Skip to content

Commit 54dfc96

Browse files
committed
pr review
Signed-off-by: odubajDT <[email protected]>
1 parent b4cbc17 commit 54dfc96

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

.chloggen/feat_16224_aggregate_label_value.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ change_type: enhancement
77
component: transformprocessor
88

99
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10-
note: "Support aggregating metrics based on their attribute values and substituing the values with a new value."
10+
note: "Support aggregating metrics based on their attribute values and substituting the values with a new value."
1111

1212
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
1313
issues: [16224]

processor/transformprocessor/README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ Examples:
375375

376376
`aggregate_on_attributes(function, Optional[attributes])`
377377

378-
The `aggregate_on_attributes` function aggregates all datapoints in the metric based on the supplied attributes. `function` is a case-sensitive string that represents the aggregation function and `attributes` is an optional list of attribute keys to aggregate upon.
378+
The `aggregate_on_attributes` function aggregates all datapoints in the metric based on the supplied attributes. `function` is a case-sensitive string that represents the aggregation function and `attributes` is an optional list of attribute keys of type string to aggregate upon.
379379

380380
`aggregate_on_attributes` function removes all attributes that are present in datapoints except the ones that are specified in the `attributes` parameter. If `attributes` parameter is not set, all attributes are removed from datapoints. Afterwards all datapoints are aggregated depending on the attributes left (none or the ones present in the list).
381381

@@ -420,9 +420,7 @@ To aggregate only using a specified set of attributes, you can use `keep_matchin
420420

421421
`aggregate_on_attribute_value(function, attribute, values, newValue)`
422422

423-
The `aggregate_on_attribute_value` function aggregates all datapoints in the metric containing the attribute `attribute` with one of the values present in the `values` parameter. `function` is a case-sensitive string that represents the aggregation function.
424-
425-
Firstly, `attribute` values with one of the values present in `values` are substituted by `newValue` for all datapoints. Afterwards all datapoints are aggregated depending on the attributes.
423+
The `aggregate_on_attribute_value` function aggregates all datapoints in the metric containing the attribute `attribute` (type string) with one of the values present in the `values` parameter (list of strings) into a single datapoint where the attribute has the value `newValue` (type string). `function` is a case-sensitive string that represents the aggregation function.
426424

427425
The following metric types can be aggregated:
428426

@@ -444,7 +442,7 @@ Supported aggregation functions are:
444442

445443
Examples:
446444

447-
- `aggregate_on_attribute_value(sum, attr1, [val1, val2], new_val) where name == "system.memory.usage`
445+
- `aggregate_on_attribute_value("sum", "attr1", ["val1", "val2"], "new_val") where name == "system.memory.usage"`
448446

449447
The `aggregate_on_attribute_value` function can also be used in conjunction with
450448
[keep_matching_keys](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/ottl/ottlfuncs#keep_matching_keys) or
@@ -455,7 +453,7 @@ For example, to remove attribute keys matching a regex and aggregate the metrics
455453
```yaml
456454
statements:
457455
- delete_matching_keys(attributes, "(?i).*myRegex.*") where name == "system.memory.usage"
458-
- aggregate_on_attribute_value(sum, attr1, [val1, val2], new_val) where name == "system.memory.usage"
456+
- aggregate_on_attribute_value("sum", "attr1", ["val1", "val2"], "new_val") where name == "system.memory.usage"
459457
```
460458

461459
To aggregate only using a specified set of attributes, you can use `keep_matching_keys`.

processor/transformprocessor/internal/metrics/func_agregate_on_attribute_value_metrics_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"go.opentelemetry.io/collector/pdata/pmetric"
1313

1414
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/aggregateutil"
15+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
1516
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
1617
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
1718
)
@@ -49,6 +50,27 @@ func Test_aggregateOnAttributeValues(t *testing.T) {
4950
input2.Attributes().PutStr("test", "test2")
5051
},
5152
},
53+
{
54+
name: "empty values",
55+
input: getTestSumMetricMultipleAggregateOnAttributeValue(),
56+
t: aggregateutil.Sum,
57+
values: []string{},
58+
attribute: "test",
59+
newValue: "test_new",
60+
want: func(metrics pmetric.MetricSlice) {
61+
sumMetric := metrics.AppendEmpty()
62+
sumMetric.SetEmptySum()
63+
sumMetric.SetName("sum_metric")
64+
65+
input := sumMetric.Sum().DataPoints().AppendEmpty()
66+
input.SetDoubleValue(100)
67+
input.Attributes().PutStr("test", "test1")
68+
69+
input2 := sumMetric.Sum().DataPoints().AppendEmpty()
70+
input2.SetDoubleValue(50)
71+
input2.Attributes().PutStr("test", "test2")
72+
},
73+
},
5274
{
5375
name: "non-existing attribute",
5476
input: getTestSumMetricMultipleAggregateOnAttributeValue(),
@@ -96,6 +118,49 @@ func Test_aggregateOnAttributeValues(t *testing.T) {
96118
input2.Attributes().PutStr("test3", "test3")
97119
},
98120
},
121+
{
122+
name: "duplicated values",
123+
input: getTestSumMetricMultipleAggregateOnAttributeValue(),
124+
t: aggregateutil.Sum,
125+
values: []string{
126+
"test1",
127+
"test1",
128+
"test2",
129+
},
130+
attribute: "test",
131+
newValue: "test_new",
132+
want: func(metrics pmetric.MetricSlice) {
133+
sumMetric := metrics.AppendEmpty()
134+
sumMetric.SetEmptySum()
135+
sumMetric.SetName("sum_metric")
136+
input := sumMetric.Sum().DataPoints().AppendEmpty()
137+
input.SetDoubleValue(150)
138+
input.Attributes().PutStr("test", "test_new")
139+
},
140+
},
141+
{
142+
name: "2 datapoints aggregated, one left unaggregated",
143+
input: getTestSumMetricMultipleAggregateOnAttributeValueOdd(),
144+
t: aggregateutil.Sum,
145+
values: []string{
146+
"test1",
147+
},
148+
attribute: "test",
149+
newValue: "test_new",
150+
want: func(metrics pmetric.MetricSlice) {
151+
sumMetric := metrics.AppendEmpty()
152+
sumMetric.SetEmptySum()
153+
sumMetric.SetName("sum_metric")
154+
155+
input := sumMetric.Sum().DataPoints().AppendEmpty()
156+
input.SetDoubleValue(150)
157+
input.Attributes().PutStr("test", "test_new")
158+
159+
input3 := sumMetric.Sum().DataPoints().AppendEmpty()
160+
input3.SetDoubleValue(30)
161+
input3.Attributes().PutStr("test", "test2")
162+
},
163+
},
99164
{
100165
name: "sum sum",
101166
input: getTestSumMetricMultipleAggregateOnAttributeValue(),
@@ -447,6 +512,21 @@ func Test_aggregateOnAttributeValues(t *testing.T) {
447512
}
448513
}
449514

515+
func Test_createAggregateOnAttributeValueFunction(t *testing.T) {
516+
// invalid input arguments
517+
_, e := createAggregateOnAttributeValueFunction(ottl.FunctionContext{}, nil)
518+
require.Contains(t, e.Error(), "AggregateOnAttributeValueFactory args must be of type *AggregateOnAttributeValueArguments")
519+
520+
// invalid aggregation function
521+
_, e = createAggregateOnAttributeValueFunction(ottl.FunctionContext{}, &aggregateOnAttributeValueArguments{
522+
AggregationFunction: "invalid",
523+
Attribute: "attr",
524+
Values: []string{"val"},
525+
NewValue: "newVal",
526+
})
527+
require.Contains(t, e.Error(), "invalid aggregation function")
528+
}
529+
450530
func getTestSumMetricMultipleAggregateOnAttributeValue() pmetric.Metric {
451531
metricInput := pmetric.NewMetric()
452532
metricInput.SetEmptySum()

0 commit comments

Comments
 (0)