diff --git a/internal/coreinternal/processor/filterexpr/matcher.go b/internal/coreinternal/processor/filterexpr/metric_matcher.go similarity index 73% rename from internal/coreinternal/processor/filterexpr/matcher.go rename to internal/coreinternal/processor/filterexpr/metric_matcher.go index fe03324d5a7e..9f9679845a7d 100644 --- a/internal/coreinternal/processor/filterexpr/matcher.go +++ b/internal/coreinternal/processor/filterexpr/metric_matcher.go @@ -20,27 +20,27 @@ import ( "go.opentelemetry.io/collector/model/pdata" ) -type Matcher struct { +type MetricMatcher struct { program *vm.Program v vm.VM } -type env struct { +type metricEnv struct { MetricName string // TODO: replace this with GetLabel func(key string) (string,bool) HasLabel func(key string) bool Label func(key string) string } -func NewMatcher(expression string) (*Matcher, error) { +func NewMetricMatcher(expression string) (*MetricMatcher, error) { program, err := expr.Compile(expression) if err != nil { return nil, err } - return &Matcher{program: program, v: vm.VM{}}, nil + return &MetricMatcher{program: program, v: vm.VM{}}, nil } -func (m *Matcher) MatchMetric(metric pdata.Metric) (bool, error) { +func (m *MetricMatcher) MatchMetric(metric pdata.Metric) (bool, error) { metricName := metric.Name() switch metric.DataType() { case pdata.MetricDataTypeGauge: @@ -54,7 +54,7 @@ func (m *Matcher) MatchMetric(metric pdata.Metric) (bool, error) { } } -func (m *Matcher) matchGauge(metricName string, gauge pdata.Gauge) (bool, error) { +func (m *MetricMatcher) matchGauge(metricName string, gauge pdata.Gauge) (bool, error) { pts := gauge.DataPoints() for i := 0; i < pts.Len(); i++ { matched, err := m.matchEnv(metricName, pts.At(i).Attributes()) @@ -68,7 +68,7 @@ func (m *Matcher) matchGauge(metricName string, gauge pdata.Gauge) (bool, error) return false, nil } -func (m *Matcher) matchSum(metricName string, sum pdata.Sum) (bool, error) { +func (m *MetricMatcher) matchSum(metricName string, sum pdata.Sum) (bool, error) { pts := sum.DataPoints() for i := 0; i < pts.Len(); i++ { matched, err := m.matchEnv(metricName, pts.At(i).Attributes()) @@ -82,7 +82,7 @@ func (m *Matcher) matchSum(metricName string, sum pdata.Sum) (bool, error) { return false, nil } -func (m *Matcher) matchDoubleHistogram(metricName string, histogram pdata.Histogram) (bool, error) { +func (m *MetricMatcher) matchDoubleHistogram(metricName string, histogram pdata.Histogram) (bool, error) { pts := histogram.DataPoints() for i := 0; i < pts.Len(); i++ { matched, err := m.matchEnv(metricName, pts.At(i).Attributes()) @@ -96,12 +96,12 @@ func (m *Matcher) matchDoubleHistogram(metricName string, histogram pdata.Histog return false, nil } -func (m *Matcher) matchEnv(metricName string, attributes pdata.AttributeMap) (bool, error) { - return m.match(createEnv(metricName, attributes)) +func (m *MetricMatcher) matchEnv(metricName string, attributes pdata.AttributeMap) (bool, error) { + return m.match(createMetricEnv(metricName, attributes)) } -func createEnv(metricName string, attributes pdata.AttributeMap) env { - return env{ +func createMetricEnv(metricName string, attributes pdata.AttributeMap) metricEnv { + return metricEnv{ MetricName: metricName, HasLabel: func(key string) bool { _, ok := attributes.Get(key) @@ -114,7 +114,7 @@ func createEnv(metricName string, attributes pdata.AttributeMap) env { } } -func (m *Matcher) match(env env) (bool, error) { +func (m *MetricMatcher) match(env metricEnv) (bool, error) { result, err := m.v.Run(m.program, env) if err != nil { return false, err diff --git a/internal/coreinternal/processor/filterexpr/matcher_test.go b/internal/coreinternal/processor/filterexpr/metric_matcher_test.go similarity index 58% rename from internal/coreinternal/processor/filterexpr/matcher_test.go rename to internal/coreinternal/processor/filterexpr/metric_matcher_test.go index cfa73b9c6f78..9cfa635db7d4 100644 --- a/internal/coreinternal/processor/filterexpr/matcher_test.go +++ b/internal/coreinternal/processor/filterexpr/metric_matcher_test.go @@ -22,20 +22,20 @@ import ( "go.opentelemetry.io/collector/model/pdata" ) -func TestCompileExprError(t *testing.T) { - _, err := NewMatcher("") +func TestMetricCompileExprError(t *testing.T) { + _, err := NewMetricMatcher("") require.Error(t, err) } -func TestRunExprError(t *testing.T) { - matcher, err := NewMatcher("foo") +func TestMetricRunExprError(t *testing.T) { + matcher, err := NewMetricMatcher("foo") require.NoError(t, err) - matched, _ := matcher.match(env{}) + matched, _ := matcher.match(metricEnv{}) require.False(t, matched) } -func TestUnknownDataType(t *testing.T) { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) +func TestMetricUnknownDataType(t *testing.T) { + matcher, err := NewMetricMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName("my.metric") @@ -45,20 +45,20 @@ func TestUnknownDataType(t *testing.T) { assert.False(t, matched) } -func TestEmptyGauge(t *testing.T) { - testEmptyValue(t, pdata.MetricDataTypeGauge) +func TestMetricEmptyGauge(t *testing.T) { + testMetricEmptyValue(t, pdata.MetricDataTypeGauge) } -func TestEmptySum(t *testing.T) { - testEmptyValue(t, pdata.MetricDataTypeSum) +func TestMetricEmptySum(t *testing.T) { + testMetricEmptyValue(t, pdata.MetricDataTypeSum) } -func TestEmptyHistogram(t *testing.T) { - testEmptyValue(t, pdata.MetricDataTypeHistogram) +func TestMetricEmptyHistogram(t *testing.T) { + testMetricEmptyValue(t, pdata.MetricDataTypeHistogram) } -func testEmptyValue(t *testing.T, dataType pdata.MetricDataType) { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) +func testMetricEmptyValue(t *testing.T, dataType pdata.MetricDataType) { + matcher, err := NewMetricMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName("my.metric") @@ -68,8 +68,8 @@ func testEmptyValue(t *testing.T, dataType pdata.MetricDataType) { assert.False(t, matched) } -func TestGaugeEmptyDataPoint(t *testing.T) { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) +func TestMetricGaugeEmptyDataPoint(t *testing.T) { + matcher, err := NewMetricMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName("my.metric") @@ -80,8 +80,8 @@ func TestGaugeEmptyDataPoint(t *testing.T) { assert.True(t, matched) } -func TestSumEmptyDataPoint(t *testing.T) { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) +func TestMetricSumEmptyDataPoint(t *testing.T) { + matcher, err := NewMetricMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName("my.metric") @@ -92,8 +92,8 @@ func TestSumEmptyDataPoint(t *testing.T) { assert.True(t, matched) } -func TestHistogramEmptyDataPoint(t *testing.T) { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) +func TestMetricHistogramEmptyDataPoint(t *testing.T) { + matcher, err := NewMetricMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName("my.metric") @@ -104,8 +104,8 @@ func TestHistogramEmptyDataPoint(t *testing.T) { assert.True(t, matched) } -func TestMatchIntGaugeDataPointByMetricAndSecondPointLabelValue(t *testing.T) { - matcher, err := NewMatcher( +func TestMetricMatchIntGaugeDataPointByMetricAndSecondPointLabelValue(t *testing.T) { + matcher, err := NewMetricMatcher( `MetricName == 'my.metric' && Label("baz") == "glarch"`, ) require.NoError(t, err) @@ -122,38 +122,38 @@ func TestMatchIntGaugeDataPointByMetricAndSecondPointLabelValue(t *testing.T) { assert.True(t, matched) } -func TestMatchGaugeByMetricName(t *testing.T) { +func TestMetricMatchGaugeByMetricName(t *testing.T) { expression := `MetricName == 'my.metric'` - assert.True(t, testMatchGauge(t, "my.metric", expression, nil)) + assert.True(t, testMetricMatchGauge(t, "my.metric", expression, nil)) } -func TestNonMatchGaugeByMetricName(t *testing.T) { +func TestMetricNonMatchGaugeByMetricName(t *testing.T) { expression := `MetricName == 'my.metric'` - assert.False(t, testMatchGauge(t, "foo.metric", expression, nil)) + assert.False(t, testMetricMatchGauge(t, "foo.metric", expression, nil)) } -func TestNonMatchGaugeDataPointByMetricAndHasLabel(t *testing.T) { +func TestMetricNonMatchGaugeDataPointByMetricAndHasLabel(t *testing.T) { expression := `MetricName == 'my.metric' && HasLabel("foo")` - assert.False(t, testMatchGauge(t, "foo.metric", expression, nil)) + assert.False(t, testMetricMatchGauge(t, "foo.metric", expression, nil)) } -func TestMatchGaugeDataPointByMetricAndHasLabel(t *testing.T) { +func TestMetricMatchGaugeDataPointByMetricAndHasLabel(t *testing.T) { expression := `MetricName == 'my.metric' && HasLabel("foo")` - assert.True(t, testMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) + assert.True(t, testMetricMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) } -func TestMatchGaugeDataPointByMetricAndLabelValue(t *testing.T) { +func TestMetricMatchGaugeDataPointByMetricAndLabelValue(t *testing.T) { expression := `MetricName == 'my.metric' && Label("foo") == "bar"` - assert.False(t, testMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) + assert.False(t, testMetricMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) } -func TestNonMatchGaugeDataPointByMetricAndLabelValue(t *testing.T) { +func TestMetricNonMatchGaugeDataPointByMetricAndLabelValue(t *testing.T) { expression := `MetricName == 'my.metric' && Label("foo") == "bar"` - assert.False(t, testMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) + assert.False(t, testMetricMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) } -func testMatchGauge(t *testing.T, metricName, expression string, lbls map[string]pdata.AttributeValue) bool { - matcher, err := NewMatcher(expression) +func testMetricMatchGauge(t *testing.T, metricName, expression string, lbls map[string]pdata.AttributeValue) bool { + matcher, err := NewMetricMatcher(expression) require.NoError(t, err) m := pdata.NewMetric() m.SetName(metricName) @@ -168,16 +168,16 @@ func testMatchGauge(t *testing.T, metricName, expression string, lbls map[string return match } -func TestMatchSumByMetricName(t *testing.T) { +func TestMetricMatchSumByMetricName(t *testing.T) { assert.True(t, matchSum(t, "my.metric")) } -func TestNonMatchSumByMetricName(t *testing.T) { +func TestMetricNonMatchSumByMetricName(t *testing.T) { assert.False(t, matchSum(t, "foo.metric")) } func matchSum(t *testing.T, metricName string) bool { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) + matcher, err := NewMetricMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName(metricName) @@ -189,16 +189,16 @@ func matchSum(t *testing.T, metricName string) bool { return matched } -func TestMatchHistogramByMetricName(t *testing.T) { +func TestMetricMatchHistogramByMetricName(t *testing.T) { assert.True(t, matchHistogram(t, "my.metric")) } -func TestNonMatchHistogramByMetricName(t *testing.T) { +func TestMetricNonMatchHistogramByMetricName(t *testing.T) { assert.False(t, matchHistogram(t, "foo.metric")) } func matchHistogram(t *testing.T, metricName string) bool { - matcher, err := NewMatcher(`MetricName == 'my.metric'`) + matcher, err := NewMetricMatcher(`MetricName == 'my.metric'`) require.NoError(t, err) m := pdata.NewMetric() m.SetName(metricName) diff --git a/internal/coreinternal/processor/filtermetric/expr_matcher.go b/internal/coreinternal/processor/filtermetric/expr_matcher.go index f07012700f4c..26ed637a5190 100644 --- a/internal/coreinternal/processor/filtermetric/expr_matcher.go +++ b/internal/coreinternal/processor/filtermetric/expr_matcher.go @@ -21,13 +21,13 @@ import ( ) type exprMatcher struct { - matchers []*filterexpr.Matcher + matchers []*filterexpr.MetricMatcher } func newExprMatcher(expressions []string) (*exprMatcher, error) { m := &exprMatcher{} for _, expression := range expressions { - matcher, err := filterexpr.NewMatcher(expression) + matcher, err := filterexpr.NewMetricMatcher(expression) if err != nil { return nil, err }