-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
func_log.go
47 lines (37 loc) · 1.17 KB
/
func_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
import (
"context"
"fmt"
"math"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)
type LogArguments[K any] struct {
Target ottl.FloatLikeGetter[K]
}
func NewLogFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("Log", &LogArguments[K]{}, createLogFunction[K])
}
func createLogFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*LogArguments[K])
if !ok {
return nil, fmt.Errorf("LogFactory args must be of type *LogArguments[K]")
}
return logFunc(args.Target), nil
}
func logFunc[K any](target ottl.FloatLikeGetter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
value, err := target.Get(ctx, tCtx)
if err != nil {
return nil, err
}
if value == nil {
return nil, fmt.Errorf("invalid input: %v", value)
}
if *value <= 0 {
return nil, fmt.Errorf("invalid input: expected number greater than zero but got %v", *value)
}
return math.Log(*value), nil
}
}