Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore][pkg/stanza] Cleanup filter operator files #32062

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
package filter // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/filter"

import (
"context"
"crypto/rand"
"fmt"
"math/big"

"github.com/expr-lang/expr/vm"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)
Expand Down Expand Up @@ -70,44 +67,3 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
dropCutoff: big.NewInt(int64(c.DropRatio * 1000)),
}, nil
}

// Transformer is an operator that filters entries based on matching expressions
type Transformer struct {
helper.TransformerOperator
expression *vm.Program
dropCutoff *big.Int // [0..1000)
}

// Process will drop incoming entries that match the filter expression
func (f *Transformer) Process(ctx context.Context, entry *entry.Entry) error {
env := helper.GetExprEnv(entry)
defer helper.PutExprEnv(env)

matches, err := vm.Run(f.expression, env)
if err != nil {
f.Errorf("Running expressing returned an error", zap.Error(err))
return nil
}

filtered, ok := matches.(bool)
if !ok {
f.Errorf("Expression did not compile as a boolean")
return nil
}

if !filtered {
f.Write(ctx, entry)
return nil
}

i, err := randInt(rand.Reader, upperBound)
if err != nil {
return err
}

if i.Cmp(f.dropCutoff) >= 0 {
f.Write(ctx, entry)
}

return nil
}
57 changes: 57 additions & 0 deletions pkg/stanza/operator/transformer/filter/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package filter

import (
"path/filepath"
"testing"

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

// test unmarshalling of values into config struct
func TestUnmarshal(t *testing.T) {
operatortest.ConfigUnmarshalTests{
DefaultConfig: NewConfig(),
TestsFile: filepath.Join(".", "testdata", "config.yaml"),
Tests: []operatortest.ConfigUnmarshalTest{
{
Name: "default",
Expect: NewConfig(),
},
{
Name: "drop_ratio_0",
Expect: func() *Config {
cfg := NewConfig()
cfg.DropRatio = 0
return cfg
}(),
},
{
Name: "drop_ratio_half",
Expect: func() *Config {
cfg := NewConfig()
cfg.DropRatio = 0.5
return cfg
}(),
},
{
Name: "drop_ratio_1",
Expect: func() *Config {
cfg := NewConfig()
cfg.DropRatio = 1
return cfg
}(),
},
{
Name: "expr",
Expect: func() *Config {
cfg := NewConfig()
cfg.Expression = "body == 'value'"
return cfg
}(),
},
},
}.Run(t)
}
14 changes: 14 additions & 0 deletions pkg/stanza/operator/transformer/filter/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
default:
type: filter
drop_ratio_0:
type: filter
drop_ratio: 0
drop_ratio_half:
type: filter
drop_ratio: 0.5
drop_ratio_1:
type: filter
drop_ratio: 1
expr:
type: filter
expr: body == 'value'
57 changes: 57 additions & 0 deletions pkg/stanza/operator/transformer/filter/transformer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package filter // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/transformer/filter"

import (
"context"
"crypto/rand"
"math/big"

"github.com/expr-lang/expr/vm"
"go.uber.org/zap"

"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 filters entries based on matching expressions
type Transformer struct {
helper.TransformerOperator
expression *vm.Program
dropCutoff *big.Int // [0..1000)
}

// Process will drop incoming entries that match the filter expression
func (t *Transformer) Process(ctx context.Context, entry *entry.Entry) error {
env := helper.GetExprEnv(entry)
defer helper.PutExprEnv(env)

matches, err := vm.Run(t.expression, env)
if err != nil {
t.Errorf("Running expressing returned an error", zap.Error(err))
return nil
}

filtered, ok := matches.(bool)
if !ok {
t.Errorf("Expression did not compile as a boolean")
return nil
}

if !filtered {
t.Write(ctx, entry)
return nil
}

i, err := randInt(rand.Reader, upperBound)
if err != nil {
return err
}

if i.Cmp(t.dropCutoff) >= 0 {
t.Write(ctx, entry)
}

return nil
}