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

[internal/filter] Move filtermetric config to filterconfig #23142

Merged
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
50 changes: 50 additions & 0 deletions internal/filter/filterconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset/regexp"
)

// MatchConfig has two optional MatchProperties one to define what is processed
Expand Down Expand Up @@ -224,3 +225,52 @@ type LogSeverityNumberMatchProperties struct {
// If this is true, entries with undefined severity will match.
MatchUndefined bool `mapstructure:"match_undefined"`
}

// MetricMatchType specifies the strategy for matching against `pmetric.Metric`s. This
// is distinct from filterset.MatchType which matches against metric (and
// tracing) names only. To support matching against metric names and
// `pmetric.Metric`s, filtermetric.MatchType is effectively a superset of
// filterset.MatchType.
type MetricMatchType string

// These are the MetricMatchType that users can specify for filtering
// `pmetric.Metric`s.
const (
MetricRegexp = MetricMatchType(filterset.Regexp)
MetricStrict = MetricMatchType(filterset.Strict)
MetricExpr = "expr"
)

// MetricMatchProperties specifies the set of properties in a metric to match against and the
// type of string pattern matching to use.
type MetricMatchProperties struct {
// MatchType specifies the type of matching desired
MatchType MetricMatchType `mapstructure:"match_type"`
// RegexpConfig specifies options for the MetricRegexp match type
RegexpConfig *regexp.Config `mapstructure:"regexp"`

// MetricNames specifies the list of string patterns to match metric names against.
// A match occurs if the metric name matches at least one string pattern in this list.
MetricNames []string `mapstructure:"metric_names"`

// Expressions specifies the list of expr expressions to match metrics against.
// A match occurs if any datapoint in a metric matches at least one expression in this list.
Expressions []string `mapstructure:"expressions"`

// ResourceAttributes defines a list of possible resource attributes to match metrics against.
// A match occurs if any resource attribute matches all expressions in this given list.
ResourceAttributes []Attribute `mapstructure:"resource_attributes"`
}

func CreateMetricMatchPropertiesFromDefault(properties *MatchProperties) *MetricMatchProperties {
if properties == nil {
return nil
}

return &MetricMatchProperties{
MatchType: MetricMatchType(properties.Config.MatchType),
RegexpConfig: properties.Config.RegexpConfig,
MetricNames: properties.MetricNames,
ResourceAttributes: properties.Resources,
}
}
59 changes: 0 additions & 59 deletions internal/filter/filtermetric/config.go

This file was deleted.

7 changes: 4 additions & 3 deletions internal/filter/filtermetric/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset/regexp"
)
Expand All @@ -30,7 +31,7 @@ var (
}
)

func createConfigWithRegexpOptions(filters []string, rCfg *regexp.Config) *MatchProperties {
func createConfigWithRegexpOptions(filters []string, rCfg *regexp.Config) *filterconfig.MetricMatchProperties {
cfg := createConfig(filters, filterset.Regexp)
cfg.RegexpConfig = rCfg
return cfg
Expand All @@ -40,12 +41,12 @@ func TestConfig(t *testing.T) {
testFile := filepath.Join("testdata", "config.yaml")
v, err := confmaptest.LoadConf(testFile)
require.NoError(t, err)
testYamls := map[string]MatchProperties{}
testYamls := map[string]filterconfig.MetricMatchProperties{}
require.NoErrorf(t, v.Unmarshal(&testYamls, confmap.WithErrorUnused()), "unable to unmarshal yaml from file %v", testFile)

tests := []struct {
name string
expCfg *MatchProperties
expCfg *filterconfig.MetricMatchProperties
}{
{
name: "config/regexp",
Expand Down
7 changes: 4 additions & 3 deletions internal/filter/filtermetric/filtermetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ package filtermetric // import "github.com/open-telemetry/opentelemetry-collecto

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/expr"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)

// NewSkipExpr creates a BoolExpr that on evaluation returns true if a metric should NOT be processed or kept.
// The logic determining if a metric should be processed is based on include and exclude settings.
// Include properties are checked before exclude settings are checked.
func NewSkipExpr(include *MatchProperties, exclude *MatchProperties) (expr.BoolExpr[ottlmetric.TransformContext], error) {
func NewSkipExpr(include *filterconfig.MetricMatchProperties, exclude *filterconfig.MetricMatchProperties) (expr.BoolExpr[ottlmetric.TransformContext], error) {
var matchers []expr.BoolExpr[ottlmetric.TransformContext]
inclExpr, err := newExpr(include)
if err != nil {
Expand All @@ -32,12 +33,12 @@ func NewSkipExpr(include *MatchProperties, exclude *MatchProperties) (expr.BoolE

// NewMatcher constructs a metric Matcher. If an 'expr' match type is specified,
// returns an expr matcher, otherwise a name matcher.
func newExpr(mp *MatchProperties) (expr.BoolExpr[ottlmetric.TransformContext], error) {
func newExpr(mp *filterconfig.MetricMatchProperties) (expr.BoolExpr[ottlmetric.TransformContext], error) {
if mp == nil {
return nil, nil
}

if mp.MatchType == Expr {
if mp.MatchType == filterconfig.MetricExpr {
if len(mp.Expressions) == 0 {
return nil, nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/filter/filtermetric/filtermetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func createMetric(name string) pmetric.Metric {
func TestMatcherMatches(t *testing.T) {
tests := []struct {
name string
cfg *MatchProperties
cfg *filterconfig.MetricMatchProperties
metric pmetric.Metric
shouldMatch bool
}{
Expand Down
7 changes: 4 additions & 3 deletions internal/filter/filtermetric/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
package filtermetric

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
)

func createConfig(filters []string, matchType filterset.MatchType) *MatchProperties {
return &MatchProperties{
MatchType: MatchType(matchType),
func createConfig(filters []string, matchType filterset.MatchType) *filterconfig.MetricMatchProperties {
return &filterconfig.MetricMatchProperties{
MatchType: filterconfig.MetricMatchType(matchType),
MetricNames: filters,
}
}
3 changes: 2 additions & 1 deletion internal/filter/filtermetric/name_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package filtermetric // import "github.com/open-telemetry/opentelemetry-collecto
import (
"context"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)
Expand All @@ -15,7 +16,7 @@ type nameMatcher struct {
nameFilters filterset.FilterSet
}

func newNameMatcher(mp *MatchProperties) (*nameMatcher, error) {
func newNameMatcher(mp *filterconfig.MetricMatchProperties) (*nameMatcher, error) {
nameFS, err := filterset.CreateFilterSet(
mp.MetricNames,
&filterset.Config{
Expand Down
5 changes: 3 additions & 2 deletions processor/attributesprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/collector/processor/processorhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/attraction"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterlog"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filtermetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterspan"
Expand Down Expand Up @@ -99,8 +100,8 @@ func createMetricsProcessor(
}

skipExpr, err := filtermetric.NewSkipExpr(
filtermetric.CreateMatchPropertiesFromDefault(oCfg.Include),
filtermetric.CreateMatchPropertiesFromDefault(oCfg.Exclude),
filterconfig.CreateMetricMatchPropertiesFromDefault(oCfg.Include),
filterconfig.CreateMetricMatchPropertiesFromDefault(oCfg.Exclude),
)
if err != nil {
return nil, err
Expand Down
5 changes: 2 additions & 3 deletions processor/filterprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filtermetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset/regexp"
Expand Down Expand Up @@ -45,12 +44,12 @@ type MetricFilters struct {
// Include match properties describe metrics that should be included in the Collector Service pipeline,
// all other metrics should be dropped from further processing.
// If both Include and Exclude are specified, Include filtering occurs first.
Include *filtermetric.MatchProperties `mapstructure:"include"`
Include *filterconfig.MetricMatchProperties `mapstructure:"include"`

// Exclude match properties describe metrics that should be excluded from the Collector Service pipeline,
// all other metrics should be included.
// If both Include and Exclude are specified, Include filtering occurs first.
Exclude *filtermetric.MatchProperties `mapstructure:"exclude"`
Exclude *filterconfig.MetricMatchProperties `mapstructure:"exclude"`

// RegexpConfig specifies options for the Regexp match type
RegexpConfig *regexp.Config `mapstructure:"regexp"`
Expand Down
Loading