-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][pkg/stanza] Cleanup add operator files (#32059)
- Loading branch information
1 parent
b4d32cf
commit 54d4b6a
Showing
3 changed files
with
51 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ")") | ||
} |
File renamed without changes.