Skip to content

Commit

Permalink
[processor/transform] convert_sum_to_gauge in metric context
Browse files Browse the repository at this point in the history
  • Loading branch information
graphaelli committed Nov 9, 2023
1 parent b716a4d commit bc8c4fd
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (
"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"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)

func newConvertSumToGaugeFactory() ottl.Factory[ottldatapoint.TransformContext] {
func newConvertSumToGaugeFactory() ottl.Factory[ottlmetric.TransformContext] {
return ottl.NewFactory("convert_sum_to_gauge", nil, createConvertSumToGaugeFunction)
}

func createConvertSumToGaugeFunction(_ ottl.FunctionContext, _ ottl.Arguments) (ottl.ExprFunc[ottldatapoint.TransformContext], error) {
func createConvertSumToGaugeFunction(_ ottl.FunctionContext, _ ottl.Arguments) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return convertSumToGauge()
}

func convertSumToGauge() (ottl.ExprFunc[ottldatapoint.TransformContext], error) {
return func(_ context.Context, tCtx ottldatapoint.TransformContext) (any, error) {
func convertSumToGauge() (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) {
metric := tCtx.GetMetric()
if metric.Type() != pmetric.MetricTypeSum {
return nil, nil
Expand Down
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
}
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)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)

func Test_convertSumToGauge(t *testing.T) {
Expand Down Expand Up @@ -83,7 +83,7 @@ func Test_convertSumToGauge(t *testing.T) {
metric := pmetric.NewMetric()
tt.input.CopyTo(metric)

ctx := ottldatapoint.NewTransformContext(pmetric.NewNumberDataPoint(), metric, pmetric.NewMetricSlice(), pcommon.NewInstrumentationScope(), pcommon.NewResource())
ctx := ottlmetric.NewTransformContext(metric, pmetric.NewMetricSlice(), pcommon.NewInstrumentationScope(), pcommon.NewResource())

exprFunc, _ := convertSumToGauge()

Expand Down
21 changes: 19 additions & 2 deletions processor/transformprocessor/internal/metrics/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@
package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/metrics"

import (
"go.opentelemetry.io/collector/featuregate"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
)

var useConvertSumToGaugeMetricContext = featuregate.GlobalRegistry().MustRegister(
"processor.transform.ConvertSumToGaugeMetricContext",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled will use metric context for convert_sum_to_gauge"),
)

func DataPointFunctions() map[string]ottl.Factory[ottldatapoint.TransformContext] {
functions := ottlfuncs.StandardFuncs[ottldatapoint.TransformContext]()

datapointFunctions := ottl.CreateFactoryMap[ottldatapoint.TransformContext](
newConvertSumToGaugeFactory(),
newConvertGaugeToSumFactory(),
newConvertSummarySumValToSumFactory(),
newConvertSummaryCountValToSumFactory(),
)

if !useConvertSumToGaugeMetricContext.IsEnabled() {
f := newConvertDatapointSumToGaugeFactory()
datapointFunctions[f.Name()] = f
}

for k, v := range datapointFunctions {
functions[k] = v
}
Expand All @@ -35,9 +47,14 @@ func MetricFunctions() map[string]ottl.Factory[ottlmetric.TransformContext] {
newExtractCountMetricFactory(),
)

if useConvertSumToGaugeMetricContext.IsEnabled() {
f := newConvertSumToGaugeFactory()
metricFunctions[f.Name()] = f
}

for k, v := range metricFunctions {
functions[k] = v

}

return functions
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func Test_DataPointFunctions(t *testing.T) {
expected := ottlfuncs.StandardFuncs[ottldatapoint.TransformContext]()
expected["convert_sum_to_gauge"] = newConvertSumToGaugeFactory()
expected["convert_sum_to_gauge"] = newConvertDatapointSumToGaugeFactory()
expected["convert_gauge_to_sum"] = newConvertGaugeToSumFactory()
expected["convert_summary_sum_val_to_sum"] = newConvertSummarySumValToSumFactory()
expected["convert_summary_count_val_to_sum"] = newConvertSummaryCountValToSumFactory()
Expand All @@ -31,6 +31,7 @@ func Test_DataPointFunctions(t *testing.T) {

func Test_MetricFunctions(t *testing.T) {
expected := ottlfuncs.StandardFuncs[ottlmetric.TransformContext]()
expected["convert_sum_to_gauge"] = newConvertSumToGaugeFactory()
expected["extract_sum_metric"] = newExtractSumMetricFactory()
expected["extract_count_metric"] = newExtractCountMetricFactory()
actual := MetricFunctions()
Expand Down

0 comments on commit bc8c4fd

Please sign in to comment.