Skip to content

Commit

Permalink
[internal/filter] Move filtermetric config to filterconfig (open-tele…
Browse files Browse the repository at this point in the history
…metry#23142)

**Description:** 
Moves filtermetric's config to filterconfig to facilitate a better
bridge to OTTL. Rename to `Metric*` to not conflict with existing config
structs.

This PR attempts to be as small as possible - only the config and its
functions are moved/changed. How components use those configs does not
change. This PR does not attempt to combine the metric configuration
with the general configuration.

**Link to tracking Issue:** 
Check
open-telemetry#23141
to see how this helps the filterottl bridge.
  • Loading branch information
TylerHelmuth authored and fchikwekwe committed Jun 23, 2023
1 parent 211dc0d commit f78882f
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 135 deletions.
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

0 comments on commit f78882f

Please sign in to comment.