Skip to content

Commit

Permalink
add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
graphaelli committed Nov 16, 2023
1 parent 20e1785 commit 703b612
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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"
)

type convertDatapointGaugeToSumArguments struct {
StringAggTemp string
Monotonic bool
}

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) {
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
}
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")
})
}
}

0 comments on commit 703b612

Please sign in to comment.