Skip to content

Commit

Permalink
feat: bring in copy of stanza operator helper and include severity in…
Browse files Browse the repository at this point in the history
… expr env for stanza entries
  • Loading branch information
raj-k-singh committed Sep 19, 2024
1 parent 7960b17 commit 1a3a4ef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Mostly brought in as-is from otel-collector-contrib with minor changes
// For example: includes severity_text and severity_number in GetExprEnv

package signozstanzahelper

import (
"os"
"sync"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
)

var envPool = sync.Pool{
New: func() any {
return map[string]any{
"os_env_func": os.Getenv,
}
},
}

// GetExprEnv returns a map of key/value pairs that can be be used to evaluate an expression
func GetExprEnv(e *entry.Entry) map[string]any {
env := envPool.Get().(map[string]any)
env["$"] = e.Body
env["body"] = e.Body
env["attributes"] = e.Attributes
env["resource"] = e.Resource
env["timestamp"] = e.Timestamp
env["severity_text"] = e.SeverityText
env["severity_number"] = int(e.Severity)

return env
}

// PutExprEnv adds a key/value pair that will can be used to evaluate an expression
func PutExprEnv(e map[string]any) {
envPool.Put(e)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

signozstanzahelper "github.com/SigNoz/signoz-otel-collector/processor/signozlogspipelineprocessor/stanza/operator/helper"
"github.com/expr-lang/expr/vm"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
Expand All @@ -33,8 +34,8 @@ func (t *Transformer) Transform(e *entry.Entry) error {
return e.Set(t.Field, t.Value)
}
if t.program != nil {
env := helper.GetExprEnv(e)
defer helper.PutExprEnv(env)
env := signozstanzahelper.GetExprEnv(e)
defer signozstanzahelper.PutExprEnv(env)

result, err := vm.Run(t.program, env)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"

signozstanzahelper "github.com/SigNoz/signoz-otel-collector/processor/signozlogspipelineprocessor/stanza/operator/helper"
"github.com/expr-lang/expr/vm"
"go.uber.org/zap"

Expand Down Expand Up @@ -35,8 +36,8 @@ func (t *Transformer) CanProcess() bool {

// Process will route incoming entries based on matching expressions
func (t *Transformer) Process(ctx context.Context, entry *entry.Entry) error {
env := helper.GetExprEnv(entry)
defer helper.PutExprEnv(env)
env := signozstanzahelper.GetExprEnv(entry)
defer signozstanzahelper.PutExprEnv(env)

for _, route := range t.routes {
matches, err := vm.Run(route.Expression, env)
Expand Down

0 comments on commit 1a3a4ef

Please sign in to comment.