-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtransformer.go
51 lines (41 loc) · 1.35 KB
/
transformer.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
48
49
50
51
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package add // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/add"
import (
"context"
"fmt"
"strings"
"github.com/expr-lang/expr/vm"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
// Transformer is an operator that adds a string value or an expression value
type Transformer struct {
helper.TransformerOperator
Field entry.Field
Value any
program *vm.Program
}
// Process will process an entry with a add transformation.
func (t *Transformer) Process(ctx context.Context, entry *entry.Entry) error {
return t.ProcessWith(ctx, entry, t.Transform)
}
// Transform will apply the add operations to an entry
func (t *Transformer) Transform(e *entry.Entry) error {
if t.Value != nil {
return e.Set(t.Field, t.Value)
}
if t.program != nil {
env := helper.GetExprEnv(e)
defer helper.PutExprEnv(env)
result, err := vm.Run(t.program, env)
if err != nil {
return fmt.Errorf("evaluate value_expr: %w", err)
}
return e.Set(t.Field, result)
}
return fmt.Errorf("add: missing required field 'value'")
}
func isExpr(str string) bool {
return strings.HasPrefix(str, "EXPR(") && strings.HasSuffix(str, ")")
}