forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/transform] Convert between sum and gauge in metric context (…
…open-telemetry#29091) **Description:** Allow running OTTL `convert_sum_to_gauge` and `convert_gauge_to_sum` in metric context instead of datapoint when `processor.transform.ConvertBetweenSumAndGaugeMetricContext` is enabled closes open-telemetry#20773 <!-- **Testing:** <Describe what testing was performed and which tests were added.> **Documentation:** <Describe the documentation added.> --> This is the result of an effort at contribfest at Kubecon NA '23. --------- Signed-off-by: Alex Boten <[email protected]> Co-authored-by: Faith Chikwekwe <[email protected]> Co-authored-by: Tyler Helmuth <[email protected]> Co-authored-by: Andreas Thaler <[email protected]> Co-authored-by: Antoine Toulme <[email protected]> Co-authored-by: Etienne Pelletier <[email protected]> Co-authored-by: bryan-aguilar <[email protected]> Co-authored-by: Rajkumar Rangaraj <[email protected]> Co-authored-by: Curtis Robert <[email protected]> Co-authored-by: Alex Boten <[email protected]> Co-authored-by: Jacob Marble <[email protected]> Co-authored-by: Jon <[email protected]> Co-authored-by: Daniel Jaglowski <[email protected]> Co-authored-by: Antoine Toulme <[email protected]>
- Loading branch information
1 parent
48e7aff
commit 6b8a461
Showing
12 changed files
with
422 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# 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: processor/transform | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Convert between sum and gauge in metric context when alpha feature gate `processor.transform.ConvertBetweenSumAndGaugeMetricContext` enabled | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [20773] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
processor/transformprocessor/internal/metrics/func_convert_gauge_to_sum_datapoint.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/metrics" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint" | ||
) | ||
|
||
func newConvertDatapointGaugeToSumFactory() ottl.Factory[ottldatapoint.TransformContext] { | ||
return ottl.NewFactory("convert_gauge_to_sum", &convertGaugeToSumArguments{}, createConvertDatapointGaugeToSumFunction) | ||
} | ||
|
||
func createConvertDatapointGaugeToSumFunction(_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[ottldatapoint.TransformContext], error) { | ||
// use the same args as in metric context | ||
args, ok := oArgs.(*convertGaugeToSumArguments) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("ConvertGaugeToSumFactory args must be of type *ConvertGaugeToSumArguments") | ||
} | ||
|
||
return convertDatapointGaugeToSum(args.StringAggTemp, args.Monotonic) | ||
} | ||
|
||
func convertDatapointGaugeToSum(stringAggTemp string, monotonic bool) (ottl.ExprFunc[ottldatapoint.TransformContext], error) { | ||
var aggTemp pmetric.AggregationTemporality | ||
switch stringAggTemp { | ||
case "delta": | ||
aggTemp = pmetric.AggregationTemporalityDelta | ||
case "cumulative": | ||
aggTemp = pmetric.AggregationTemporalityCumulative | ||
default: | ||
return nil, fmt.Errorf("unknown aggregation temporality: %s", stringAggTemp) | ||
} | ||
|
||
return func(_ context.Context, tCtx ottldatapoint.TransformContext) (any, error) { | ||
metric := tCtx.GetMetric() | ||
if metric.Type() != pmetric.MetricTypeGauge { | ||
return nil, nil | ||
} | ||
|
||
dps := metric.Gauge().DataPoints() | ||
|
||
metric.SetEmptySum().SetAggregationTemporality(aggTemp) | ||
metric.Sum().SetIsMonotonic(monotonic) | ||
|
||
// Setting the data type removed all the data points, so we must copy them back to the metric. | ||
dps.CopyTo(metric.Sum().DataPoints()) | ||
|
||
return nil, nil | ||
}, nil | ||
} |
149 changes: 149 additions & 0 deletions
149
processor/transformprocessor/internal/metrics/func_convert_gauge_to_sum_datapoint_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint" | ||
) | ||
|
||
func Test_convertDatapointGaugeToSum(t *testing.T) { | ||
gaugeInput := pmetric.NewMetric() | ||
|
||
dp1 := gaugeInput.SetEmptyGauge().DataPoints().AppendEmpty() | ||
dp1.SetIntValue(10) | ||
|
||
dp2 := gaugeInput.Gauge().DataPoints().AppendEmpty() | ||
dp2.SetDoubleValue(14.5) | ||
|
||
sumInput := pmetric.NewMetric() | ||
sumInput.SetEmptySum() | ||
|
||
histogramInput := pmetric.NewMetric() | ||
histogramInput.SetEmptyHistogram() | ||
|
||
expoHistogramInput := pmetric.NewMetric() | ||
expoHistogramInput.SetEmptyHistogram() | ||
|
||
summaryInput := pmetric.NewMetric() | ||
summaryInput.SetEmptySummary() | ||
|
||
tests := []struct { | ||
name string | ||
stringAggTemp string | ||
monotonic bool | ||
input pmetric.Metric | ||
want func(pmetric.Metric) | ||
}{ | ||
{ | ||
name: "convert gauge to cumulative sum", | ||
stringAggTemp: "cumulative", | ||
monotonic: false, | ||
input: gaugeInput, | ||
want: func(metric pmetric.Metric) { | ||
gaugeInput.CopyTo(metric) | ||
|
||
dps := gaugeInput.Gauge().DataPoints() | ||
|
||
metric.SetEmptySum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) | ||
metric.Sum().SetIsMonotonic(false) | ||
|
||
dps.CopyTo(metric.Sum().DataPoints()) | ||
}, | ||
}, | ||
{ | ||
name: "convert gauge to delta sum", | ||
stringAggTemp: "delta", | ||
monotonic: true, | ||
input: gaugeInput, | ||
want: func(metric pmetric.Metric) { | ||
gaugeInput.CopyTo(metric) | ||
|
||
dps := gaugeInput.Gauge().DataPoints() | ||
|
||
metric.SetEmptySum().SetAggregationTemporality(pmetric.AggregationTemporalityDelta) | ||
metric.Sum().SetIsMonotonic(true) | ||
|
||
dps.CopyTo(metric.Sum().DataPoints()) | ||
}, | ||
}, | ||
{ | ||
name: "noop for sum", | ||
stringAggTemp: "delta", | ||
monotonic: true, | ||
input: sumInput, | ||
want: func(metric pmetric.Metric) { | ||
sumInput.CopyTo(metric) | ||
}, | ||
}, | ||
{ | ||
name: "noop for histogram", | ||
stringAggTemp: "delta", | ||
monotonic: true, | ||
input: histogramInput, | ||
want: func(metric pmetric.Metric) { | ||
histogramInput.CopyTo(metric) | ||
}, | ||
}, | ||
{ | ||
name: "noop for exponential histogram", | ||
stringAggTemp: "delta", | ||
monotonic: true, | ||
input: expoHistogramInput, | ||
want: func(metric pmetric.Metric) { | ||
expoHistogramInput.CopyTo(metric) | ||
}, | ||
}, | ||
{ | ||
name: "noop for summary", | ||
stringAggTemp: "delta", | ||
monotonic: true, | ||
input: summaryInput, | ||
want: func(metric pmetric.Metric) { | ||
summaryInput.CopyTo(metric) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
metric := pmetric.NewMetric() | ||
tt.input.CopyTo(metric) | ||
|
||
ctx := ottldatapoint.NewTransformContext(pmetric.NewNumberDataPoint(), metric, pmetric.NewMetricSlice(), pcommon.NewInstrumentationScope(), pcommon.NewResource()) | ||
|
||
exprFunc, _ := convertDatapointGaugeToSum(tt.stringAggTemp, tt.monotonic) | ||
|
||
_, err := exprFunc(nil, ctx) | ||
assert.Nil(t, err) | ||
|
||
expected := pmetric.NewMetric() | ||
tt.want(expected) | ||
|
||
assert.Equal(t, expected, metric) | ||
}) | ||
} | ||
} | ||
|
||
func Test_convertDatapointGaugeToSum_validation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
stringAggTemp string | ||
}{ | ||
{ | ||
name: "invalid aggregation temporality", | ||
stringAggTemp: "not a real aggregation temporality", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := convertDatapointGaugeToSum(tt.stringAggTemp, true) | ||
assert.Error(t, err, "unknown aggregation temporality: not a real aggregation temporality") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
processor/transformprocessor/internal/metrics/func_convert_sum_to_gauge_datapoint.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/metrics" | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint" | ||
) | ||
|
||
func newConvertDatapointSumToGaugeFactory() ottl.Factory[ottldatapoint.TransformContext] { | ||
return ottl.NewFactory("convert_sum_to_gauge", nil, createDatapointConvertSumToGaugeFunction) | ||
} | ||
|
||
func createDatapointConvertSumToGaugeFunction(_ ottl.FunctionContext, _ ottl.Arguments) (ottl.ExprFunc[ottldatapoint.TransformContext], error) { | ||
return convertDatapointSumToGauge() | ||
} | ||
|
||
func convertDatapointSumToGauge() (ottl.ExprFunc[ottldatapoint.TransformContext], error) { | ||
return func(_ context.Context, tCtx ottldatapoint.TransformContext) (any, error) { | ||
metric := tCtx.GetMetric() | ||
if metric.Type() != pmetric.MetricTypeSum { | ||
return nil, nil | ||
} | ||
|
||
dps := metric.Sum().DataPoints() | ||
|
||
// Setting the data type removed all the data points, so we must copy them back to the metric. | ||
dps.CopyTo(metric.SetEmptyGauge().DataPoints()) | ||
|
||
return nil, nil | ||
}, nil | ||
} |
Oops, something went wrong.