Skip to content

Commit d398d64

Browse files
committed
move scale function to transformprocessor
Signed-off-by: Florian Bacher <[email protected]>
1 parent 0090fcc commit d398d64

File tree

7 files changed

+115
-143
lines changed

7 files changed

+115
-143
lines changed

pkg/ottl/ottlfuncs/README.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ Available Editors:
5959
- [replace_pattern](#replace_pattern)
6060
- [set](#set)
6161
- [truncate_all](#truncate_all)
62-
- [scale_metric](#scale_metric)
6362

6463
### append
6564

@@ -403,22 +402,6 @@ Examples:
403402

404403
- `truncate_all(resource.attributes, 50)`
405404

406-
### scale_metric
407-
408-
`scale_metric(factor)`
409-
410-
The `scale_metric` function multiplies the values in the data points in the metric by the `factor`.
411-
The supported data types are:
412-
413-
- `data_points` - Supported metric types are `Gauge`, `Sum` and `Histogram`.
414-
To scale a metric of these types, the `data_points` property of the respective metric needs to be passed to the function,
415-
as indicated in the examples below.
416-
417-
Examples:
418-
419-
- `scale_metric(10.0, 0.1)`: Trivial example
420-
- `scale_metric(data_points, 10.0)`: Modifies the metric's `data_points` by multiplying them with the factor `10.0`.
421-
422405
## Converters
423406

424407
Converters are pure functions that take OTTL values as input and output a single value for use within a statement.

pkg/ottl/ottlfuncs/functions.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func StandardFuncs[K any]() map[string]ottl.Factory[K] {
2323
NewReplacePatternFactory[K](),
2424
NewSetFactory[K](),
2525
NewTruncateAllFactory[K](),
26-
NewScaleFactory[K](),
2726
}
2827
f = append(f, converters[K]()...)
2928

processor/transformprocessor/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ In addition to OTTL functions, the processor defines its own functions to help w
212212
- [convert_summary_count_val_to_sum](#convert_summary_count_val_to_sum)
213213
- [convert_summary_sum_val_to_sum](#convert_summary_sum_val_to_sum)
214214
- [copy_metric](#copy_metric)
215+
- [scale_metric](#scale_metric)
215216

216217
### convert_sum_to_gauge
217218

@@ -347,6 +348,21 @@ Examples:
347348

348349
- `copy_metric(desc="new desc") where description == "old desc"`
349350

351+
### scale_metric
352+
353+
`scale_metric(factor,unit)`
354+
355+
The `scale_metric` function multiplies the values in the data points in the metric by the `factor`.
356+
If set to a non-empty string, the metric's unit will be set to `unit`.
357+
The supported data types are:
358+
359+
Supported metric types are `Gauge`, `Sum` and `Histogram`.
360+
361+
Examples:
362+
363+
- `scale_metric(0.1,"")`: Scale the metric by a factor of `0.1`. The unit of the metric will not be modified.
364+
- `scale_metric(10.0, "kWh")`: Scale the metric by a factor of `10.0` and sets the unit to `kWh`.
365+
350366
## Examples
351367

352368
### Perform transformation if field does not exist

pkg/ottl/ottlfuncs/func_scale.go renamed to processor/transformprocessor/internal/metrics/func_scale.go

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
4+
package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
55

66
import (
77
"context"
@@ -11,60 +11,52 @@ import (
1111
"go.opentelemetry.io/collector/pdata/pmetric"
1212

1313
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
14+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
1415
)
1516

16-
type ScaleArguments[K any] struct {
17-
Value ottl.GetSetter[K]
17+
type ScaleArguments struct {
1818
Multiplier float64
19+
Unit string
1920
}
2021

21-
func NewScaleFactory[K any]() ottl.Factory[K] {
22-
return ottl.NewFactory("scale_metric", &ScaleArguments[K]{}, createScaleFunction[K])
22+
func newScaleMetricFactory() ottl.Factory[ottlmetric.TransformContext] {
23+
return ottl.NewFactory("scale_metric", &ScaleArguments{}, createScaleFunction)
2324
}
2425

25-
func createScaleFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
26-
args, ok := oArgs.(*ScaleArguments[K])
26+
func createScaleFunction(_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
27+
args, ok := oArgs.(*ScaleArguments)
2728

2829
if !ok {
2930
return nil, fmt.Errorf("ScaleFactory args must be of type *ScaleArguments[K]")
3031
}
3132

32-
return Scale(args.Value, args.Multiplier)
33+
return Scale(*args)
3334
}
3435

35-
func Scale[K any](getSetter ottl.GetSetter[K], multiplier float64) (ottl.ExprFunc[K], error) {
36-
return func(ctx context.Context, tCtx K) (any, error) {
37-
got, err := getSetter.Get(ctx, tCtx)
38-
if err != nil {
39-
return nil, err
40-
}
41-
42-
switch value := got.(type) {
43-
case pmetric.NumberDataPointSlice:
44-
scaleMetric(value, multiplier)
45-
return nil, nil
46-
case pmetric.HistogramDataPointSlice:
47-
scaleHistogram(value, multiplier)
48-
return nil, nil
49-
case pmetric.SummaryDataPointValueAtQuantileSlice:
50-
scaleSummaryDataPointValueAtQuantileSlice(value, multiplier)
51-
return nil, nil
52-
case pmetric.ExemplarSlice:
53-
scaleExemplarSlice(value, multiplier)
54-
return nil, nil
55-
case pmetric.ExponentialHistogramDataPointSlice:
36+
func Scale(args ScaleArguments) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
37+
return func(ctx context.Context, tCtx ottlmetric.TransformContext) (any, error) {
38+
metric := tCtx.GetMetric()
39+
40+
switch metric.Type() {
41+
case pmetric.MetricTypeGauge:
42+
scaleMetric(metric.Gauge().DataPoints(), args.Multiplier)
43+
case pmetric.MetricTypeHistogram:
44+
scaleHistogram(metric.Histogram().DataPoints(), args.Multiplier)
45+
case pmetric.MetricTypeSummary:
46+
scaleSummarySlice(metric.Summary().DataPoints(), args.Multiplier)
47+
case pmetric.MetricTypeSum:
48+
scaleMetric(metric.Sum().DataPoints(), args.Multiplier)
49+
case pmetric.MetricTypeExponentialHistogram:
5650
return nil, errors.New("exponential histograms are not supported by the 'scale_metric' function")
5751
default:
58-
return nil, fmt.Errorf("unsupported data type: '%T'", value)
52+
return nil, fmt.Errorf("unsupported metric type: '%v'", metric.Type())
5953
}
60-
}, nil
61-
}
6254

63-
func scaleExemplarSlice(values pmetric.ExemplarSlice, multiplier float64) {
64-
for i := 0; i < values.Len(); i++ {
65-
ex := values.At(i)
66-
scaleExemplar(&ex, multiplier)
67-
}
55+
if args.Unit != "" {
56+
metric.SetUnit(args.Unit)
57+
}
58+
return nil, nil
59+
}, nil
6860
}
6961

7062
func scaleExemplar(ex *pmetric.Exemplar, multiplier float64) {
@@ -76,11 +68,16 @@ func scaleExemplar(ex *pmetric.Exemplar, multiplier float64) {
7668
}
7769
}
7870

79-
func scaleSummaryDataPointValueAtQuantileSlice(values pmetric.SummaryDataPointValueAtQuantileSlice, multiplier float64) {
71+
func scaleSummarySlice(values pmetric.SummaryDataPointSlice, multiplier float64) {
8072
for i := 0; i < values.Len(); i++ {
8173
dp := values.At(i)
8274

83-
dp.SetValue(dp.Value() * multiplier)
75+
dp.SetSum(dp.Sum() * multiplier)
76+
77+
for i := 0; i < dp.QuantileValues().Len(); i++ {
78+
qv := dp.QuantileValues().At(i)
79+
qv.SetValue(qv.Value() * multiplier)
80+
}
8481
}
8582
}
8683

0 commit comments

Comments
 (0)