Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup add operator files (#32059)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Apr 2, 2024
1 parent b4d32cf commit 54d4b6a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
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"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
Expand Down Expand Up @@ -68,39 +66,3 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
addOperator.program = compiled
return addOperator, nil
}

// 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 (p *Transformer) Process(ctx context.Context, entry *entry.Entry) error {
return p.ProcessWith(ctx, entry, p.Transform)
}

// Transform will apply the add operations to an entry
func (p *Transformer) Transform(e *entry.Entry) error {
if p.Value != nil {
return e.Set(p.Field, p.Value)
}
if p.program != nil {
env := helper.GetExprEnv(e)
defer helper.PutExprEnv(env)

result, err := vm.Run(p.program, env)
if err != nil {
return fmt.Errorf("evaluate value_expr: %w", err)
}
return e.Set(p.Field, result)
}
return fmt.Errorf("add: missing required field 'value'")
}

func isExpr(str string) bool {
return strings.HasPrefix(str, "EXPR(") && strings.HasSuffix(str, ")")
}
51 changes: 51 additions & 0 deletions pkg/stanza/operator/transformer/add/transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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, ")")
}

0 comments on commit 54d4b6a

Please sign in to comment.