Skip to content

Commit

Permalink
refactor(filterexpr): rename matcher to metric_matcher
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
Dominik Rosiek committed Oct 8, 2021
1 parent 51af8c8 commit 6c7b474
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/coreinternal/processor/filtermetric/expr_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6c7b474

Please sign in to comment.