Skip to content

Commit

Permalink
adapt scale function to be an editor function
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 17, 2024
1 parent 0f07a49 commit 92d33ed
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 153 deletions.
13 changes: 7 additions & 6 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Test_e2e_editors(t *testing.T) {
tCtx.GetLogRecord().Attributes().Remove("flags")
tCtx.GetLogRecord().Attributes().Remove("total.string")
tCtx.GetLogRecord().Attributes().Remove("foo")
tCtx.GetLogRecord().Attributes().Remove("double_value")
},
},
{
Expand Down Expand Up @@ -280,6 +281,12 @@ func Test_e2e_editors(t *testing.T) {
s.AppendEmpty().SetInt(6)
},
},
{
statement: `scale_metric(attributes["double_value"],0.1)`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutDouble("double_value", 1.05)
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -713,12 +720,6 @@ func Test_e2e_converters(t *testing.T) {
m.PutStr("bar", "pass")
},
},
{
statement: `set(attributes["double_value"], Scale(attributes["double_value"],0.1))`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutDouble("double_value", 1.05)
},
},
}

for _, tt := range tests {
Expand Down
44 changes: 19 additions & 25 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Available Editors:
- [replace_pattern](#replace_pattern)
- [set](#set)
- [truncate_all](#truncate_all)
- [scale_metric](#scale_metric)

### append

Expand Down Expand Up @@ -402,6 +403,24 @@ Examples:

- `truncate_all(resource.attributes, 50)`

### scale_metric

`scale_metric(value, factor)`

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.

Examples:

- `scale_metric(10.0, 0.1)`: Trivial example
- `scale_metric(data_points, 10.0)`: Modifies the metric's `data_points` by multiplying them with the factor `10.0`.

## Converters

Converters are pure functions that take OTTL values as input and output a single value for use within a statement.
Expand Down Expand Up @@ -457,7 +476,6 @@ Available Converters:
- [UnixSeconds](#unixseconds)
- [UUID](#UUID)
- [Year](#year)
- [Scale](#scale)

### Base64Decode

Expand Down Expand Up @@ -1391,30 +1409,6 @@ Examples:

- `Year(Now())`

### Scale

`Scale(value, factor)`

The `Scale` function returns the original `value`, multiplied 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.

Examples:

- `Scale(10.0, 0.1)`: Trivial example
- `Scale(data_points, 10.0)`: Returns the scaled `data_points` of a metric as a result.

If the actual metric value should be modified, this can be done in combination with the `set` function:

```
set(data_points, Scale(data_points, 10.0))
```

## Function syntax

Functions should be named and formatted according to the following standards.
Expand Down
41 changes: 17 additions & 24 deletions pkg/ottl/ottlfuncs/func_scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
)

type ScaleArguments[K any] struct {
Value ottl.Getter[K]
Value ottl.GetSetter[K]
Multiplier float64
}

func NewScaleFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("Scale", &ScaleArguments[K]{}, createScaleFunction[K])
return ottl.NewFactory("scale_metric", &ScaleArguments[K]{}, createScaleFunction[K])
}

func createScaleFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
Expand All @@ -32,40 +32,34 @@ func createScaleFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (o
return Scale(args.Value, args.Multiplier)
}

func Scale[K any](getter ottl.Getter[K], multiplier float64) (ottl.ExprFunc[K], error) {
func Scale[K any](getSetter ottl.GetSetter[K], multiplier float64) (ottl.ExprFunc[K], error) {
return func(ctx context.Context, tCtx K) (any, error) {
got, err := getter.Get(ctx, tCtx)
got, err := getSetter.Get(ctx, tCtx)
if err != nil {
return nil, err
}

switch value := got.(type) {
case float64:
return value * multiplier, nil
value *= multiplier
return nil, getSetter.Set(ctx, tCtx, value)
case int64:
return float64(value) * multiplier, nil
value = int64(float64(value) * multiplier)
return nil, getSetter.Set(ctx, tCtx, value)
case pmetric.NumberDataPointSlice:
scaledMetric := pmetric.NewNumberDataPointSlice()
value.CopyTo(scaledMetric)
scaleMetric(scaledMetric, multiplier)
return scaledMetric, nil
scaleMetric(value, multiplier)
return nil, nil
case pmetric.HistogramDataPointSlice:
scaledMetric := pmetric.NewHistogramDataPointSlice()
value.CopyTo(scaledMetric)
scaleHistogram(scaledMetric, multiplier)
return scaledMetric, nil
scaleHistogram(value, multiplier)
return nil, nil
case pmetric.SummaryDataPointValueAtQuantileSlice:
scaledMetric := pmetric.NewSummaryDataPointValueAtQuantileSlice()
value.CopyTo(scaledMetric)
scaleSummaryDataPointValueAtQuantileSlice(scaledMetric, multiplier)
return scaledMetric, nil
scaleSummaryDataPointValueAtQuantileSlice(value, multiplier)
return nil, nil
case pmetric.ExemplarSlice:
scaledSlice := pmetric.NewExemplarSlice()
value.CopyTo(scaledSlice)
scaleExemplarSlice(scaledSlice, multiplier)
return scaledSlice, nil
scaleExemplarSlice(value, multiplier)
return nil, nil
case pmetric.ExponentialHistogramDataPointSlice:
return nil, errors.New("exponential histograms are not supported by the 'Scale' function")
return nil, errors.New("exponential histograms are not supported by the 'scale_metric' function")
default:
return nil, fmt.Errorf("unsupported data type: '%T'", value)
}
Expand Down Expand Up @@ -97,7 +91,6 @@ func scaleSummaryDataPointValueAtQuantileSlice(values pmetric.SummaryDataPointVa
}

func scaleHistogram(datapoints pmetric.HistogramDataPointSlice, multiplier float64) {

for i := 0; i < datapoints.Len(); i++ {
dp := datapoints.At(i)

Expand Down
Loading

0 comments on commit 92d33ed

Please sign in to comment.