-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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_sum_to_gauge in metric context
- Loading branch information
1 parent
b716a4d
commit bc8c4fd
Showing
6 changed files
with
164 additions
and
10 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
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 | ||
} |
99 changes: 99 additions & 0 deletions
99
processor/transformprocessor/internal/metrics/func_convert_sum_to_gauge_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,99 @@ | ||
// 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_convertDatapointSumToGauge(t *testing.T) { | ||
sumInput := pmetric.NewMetric() | ||
|
||
dp1 := sumInput.SetEmptySum().DataPoints().AppendEmpty() | ||
dp1.SetIntValue(10) | ||
|
||
dp2 := sumInput.Sum().DataPoints().AppendEmpty() | ||
dp2.SetDoubleValue(14.5) | ||
|
||
gaugeInput := pmetric.NewMetric() | ||
gaugeInput.SetEmptyGauge() | ||
|
||
histogramInput := pmetric.NewMetric() | ||
histogramInput.SetEmptyHistogram() | ||
|
||
expoHistogramInput := pmetric.NewMetric() | ||
expoHistogramInput.SetEmptyExponentialHistogram() | ||
|
||
summaryInput := pmetric.NewMetric() | ||
summaryInput.SetEmptySummary() | ||
|
||
tests := []struct { | ||
name string | ||
input pmetric.Metric | ||
want func(pmetric.Metric) | ||
}{ | ||
{ | ||
name: "convert sum to gauge", | ||
input: sumInput, | ||
want: func(metric pmetric.Metric) { | ||
sumInput.CopyTo(metric) | ||
|
||
dps := sumInput.Sum().DataPoints() | ||
dps.CopyTo(metric.SetEmptyGauge().DataPoints()) | ||
}, | ||
}, | ||
{ | ||
name: "noop for gauge", | ||
input: gaugeInput, | ||
want: func(metric pmetric.Metric) { | ||
gaugeInput.CopyTo(metric) | ||
}, | ||
}, | ||
{ | ||
name: "noop for histogram", | ||
input: histogramInput, | ||
want: func(metric pmetric.Metric) { | ||
histogramInput.CopyTo(metric) | ||
}, | ||
}, | ||
{ | ||
name: "noop for exponential histogram", | ||
input: expoHistogramInput, | ||
want: func(metric pmetric.Metric) { | ||
expoHistogramInput.CopyTo(metric) | ||
}, | ||
}, | ||
{ | ||
name: "noop for summary", | ||
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, _ := convertDatapointSumToGauge() | ||
|
||
_, err := exprFunc(nil, ctx) | ||
assert.Nil(t, err) | ||
|
||
expected := pmetric.NewMetric() | ||
tt.want(expected) | ||
|
||
assert.Equal(t, expected, metric) | ||
}) | ||
} | ||
} |
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
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