Skip to content

Commit

Permalink
adapt the function to only work with metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
  • Loading branch information
bacherfl committed Jun 18, 2024
1 parent 61e2a09 commit 75a6d67
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
58 changes: 58 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)

var (
Expand Down Expand Up @@ -308,6 +311,38 @@ func Test_e2e_editors(t *testing.T) {
}
}

func Test_e2e_metricEditors(t *testing.T) {
tests := []struct {
statement string
want func(tCtx ottlmetric.TransformContext)
}{
{
statement: `scale_metric(data_points,0.1)`,
want: func(tCtx ottlmetric.TransformContext) {
tCtx.GetMetric().Gauge().DataPoints().At(0).SetDoubleValue(1.0)
},
},
}

for _, tt := range tests {
t.Run(tt.statement, func(t *testing.T) {
settings := componenttest.NewNopTelemetrySettings()
metricParser, err := ottlmetric.NewParser(ottlfuncs.StandardFuncs[ottlmetric.TransformContext](), settings)
assert.NoError(t, err)
logStatements, err := metricParser.ParseStatement(tt.statement)
assert.NoError(t, err)

tCtx := constructMetricTransformContext()
_, _, _ = logStatements.Execute(context.Background(), tCtx)

exTCtx := constructMetricTransformContext()
tt.want(exTCtx)

assert.NoError(t, pmetrictest.CompareResourceMetrics(newResourceMetrics(exTCtx), newResourceMetrics(tCtx)))
})
}
}

func Test_e2e_converters(t *testing.T) {
tests := []struct {
statement string
Expand Down Expand Up @@ -827,6 +862,19 @@ func Test_e2e_ottl_features(t *testing.T) {
}
}

func constructMetricTransformContext() ottlmetric.TransformContext {
resource := pcommon.NewResource()
resource.Attributes().PutStr("host.name", "localhost")

scope := pcommon.NewInstrumentationScope()
scope.SetName("scope")

metric := pmetric.NewMetric()
metric.SetEmptyGauge().DataPoints().AppendEmpty().SetDoubleValue(10.0)

return ottlmetric.NewTransformContext(metric, pmetric.NewMetricSlice(), scope, resource)
}

func constructLogTransformContext() ottllog.TransformContext {
resource := pcommon.NewResource()
resource.Attributes().PutStr("host.name", "localhost")
Expand Down Expand Up @@ -870,3 +918,13 @@ func newResourceLogs(tCtx ottllog.TransformContext) plog.ResourceLogs {
tCtx.GetLogRecord().CopyTo(l)
return rl
}

func newResourceMetrics(tCtx ottlmetric.TransformContext) pmetric.ResourceMetrics {
rl := pmetric.NewResourceMetrics()
tCtx.GetResource().CopyTo(rl.Resource())
sl := rl.ScopeMetrics().AppendEmpty()
tCtx.GetInstrumentationScope().CopyTo(sl.Scope())
l := sl.Metrics().AppendEmpty()
tCtx.GetMetric().CopyTo(l)
return rl
}
2 changes: 0 additions & 2 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,6 @@ Examples:
The `Scale` function multiplies the original `value` by the `factor`.
The supported data types are:

- `int`
- `double`
- `data_points` - Supported metric types are `Gauge`, `Sum` and `Histogram`.
To scale a metric of these types, the `data_points` property of the respective metric needs to be passed to the function,
as indicated in the examples below.
Expand Down
6 changes: 0 additions & 6 deletions pkg/ottl/ottlfuncs/func_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ func Scale[K any](getSetter ottl.GetSetter[K], multiplier float64) (ottl.ExprFun
}

switch value := got.(type) {
case float64:
value *= multiplier
return nil, getSetter.Set(ctx, tCtx, value)
case int64:
value = int64(float64(value) * multiplier)
return nil, getSetter.Set(ctx, tCtx, value)
case pmetric.NumberDataPointSlice:
scaleMetric(value, multiplier)
return nil, nil
Expand Down
22 changes: 0 additions & 22 deletions pkg/ottl/ottlfuncs/func_scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,6 @@ func TestScale(t *testing.T) {
wantErr bool
}
tests := []testCase{
{
name: "scale float value",
valueFunc: func() any {
return 1.05
},
multiplier: 10.0,
wantFunc: func() any {
return 10.5
},
wantErr: false,
},
{
name: "scale int value",
valueFunc: func() any {
return int64(1)
},
multiplier: 10.0,
wantFunc: func() any {
return float64(10)
},
wantErr: false,
},
{
name: "unsupported data type",
valueFunc: func() any {
Expand Down

0 comments on commit 75a6d67

Please sign in to comment.