forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
113 lines (91 loc) · 2.92 KB
/
filter.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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"
"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"
)
const operatorType = "filter"
var (
upperBound = big.NewInt(1000)
randInt = rand.Int // allow override for testing
)
func init() {
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}
// NewConfig creates a filter operator config with default values
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}
// NewConfigWithID creates a filter operator config with default values
func NewConfigWithID(operatorID string) *Config {
return &Config{
TransformerConfig: helper.NewTransformerConfig(operatorID, operatorType),
DropRatio: 1,
}
}
// Config is the configuration of a filter operator
type Config struct {
helper.TransformerConfig `mapstructure:",squash"`
Expression string `mapstructure:"expr"`
DropRatio float64 `mapstructure:"drop_ratio"`
}
// Build will build a filter operator from the supplied configuration
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
transformer, err := c.TransformerConfig.Build(logger)
if err != nil {
return nil, err
}
compiledExpression, err := helper.ExprCompileBool(c.Expression)
if err != nil {
return nil, fmt.Errorf("failed to compile expression '%s': %w", c.Expression, err)
}
if c.DropRatio < 0.0 || c.DropRatio > 1.0 {
return nil, fmt.Errorf("drop_ratio must be a number between 0 and 1")
}
return &Transformer{
TransformerOperator: transformer,
expression: compiledExpression,
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
}