diff --git a/internal/coreinternal/processor/filterexpr/matcher.go b/internal/coreinternal/processor/filterexpr/matcher.go index fe03324d5a7e..5512e0e35e4f 100644 --- a/internal/coreinternal/processor/filterexpr/matcher.go +++ b/internal/coreinternal/processor/filterexpr/matcher.go @@ -27,7 +27,13 @@ type Matcher struct { type env struct { MetricName string - // TODO: replace this with GetLabel func(key string) (string,bool) + + // TODO: replace this with GetAttribute func(key string) (string,bool) + HasAttribute func(key string) bool + Attribute func(key string) string + + // HasLabel and Label are aliases for HasAttribute and Attribute, kept for + // backwards compatibility. These are deprecated and will be removed in the future. HasLabel func(key string) bool Label func(key string) string } @@ -101,17 +107,21 @@ func (m *Matcher) matchEnv(metricName string, attributes pdata.AttributeMap) (bo } func createEnv(metricName string, attributes pdata.AttributeMap) env { - return env{ + e := env{ MetricName: metricName, - HasLabel: func(key string) bool { + HasAttribute: func(key string) bool { _, ok := attributes.Get(key) return ok }, - Label: func(key string) string { + Attribute: func(key string) string { v, _ := attributes.Get(key) return v.StringVal() }, } + // HasLabel and Label are aliases for HasAttribute and Attribute. + e.HasLabel = e.HasAttribute + e.Label = e.Attribute + return e } func (m *Matcher) match(env env) (bool, error) { diff --git a/internal/coreinternal/processor/filterexpr/matcher_test.go b/internal/coreinternal/processor/filterexpr/matcher_test.go index cfa73b9c6f78..101644d2f8df 100644 --- a/internal/coreinternal/processor/filterexpr/matcher_test.go +++ b/internal/coreinternal/processor/filterexpr/matcher_test.go @@ -142,11 +142,21 @@ func TestMatchGaugeDataPointByMetricAndHasLabel(t *testing.T) { assert.True(t, testMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) } +func TestMatchGaugeDataPointByMetricAndHasAttribute(t *testing.T) { + expression := `MetricName == 'my.metric' && HasAttribute("foo")` + assert.True(t, testMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) +} + func TestMatchGaugeDataPointByMetricAndLabelValue(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("")})) } +func TestMatchGaugeDataPointByMetricAndAttributeValue(t *testing.T) { + expression := `MetricName == 'my.metric' && Attribute("foo") == "bar"` + assert.False(t, testMatchGauge(t, "my.metric", expression, map[string]pdata.AttributeValue{"foo": pdata.NewAttributeValueString("")})) +} + func TestNonMatchGaugeDataPointByMetricAndLabelValue(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("")})) diff --git a/processor/filterprocessor/README.md b/processor/filterprocessor/README.md index 0784e73db1c8..863d4a64684a 100644 --- a/processor/filterprocessor/README.md +++ b/processor/filterprocessor/README.md @@ -98,12 +98,16 @@ Made available to the expression environment are the following: * `MetricName` a variable containing the current Metric's name +* `Attribute(name)` + a function that takes an attribute name string as an argument and returns a string: the value of an attribute with that + name if one exists, or "" +* `HasAttribute(name)` + a function that takes an attribute name string as an argument and returns a boolean: true if the datapoint has an attribute + with that name, false otherwise * `Label(name)` - a function that takes a label name string as an argument and returns a string: the value of a label with that - name if one exists, or "" + Alias for `Attribute(name)`. Deprecated. * `HasLabel(name)` - a function that takes a label name string as an argument and returns a boolean: true if the datapoint has a label - with that name, false otherwise + Alias for `HasLabel(name)`. Deprecated. Example: @@ -114,11 +118,11 @@ processors: exclude: match_type: expr expressions: - - MetricName == "my.metric" && Label("my_label") == "abc123" + - MetricName == "my.metric" && Attribute("my_attr") == "abc123" ``` The above config will filter out any Metric that both has the name "my.metric" and has at least one datapoint -with a label of 'my_label="abc123"'. +with an attribute of 'my_attr="abc123"'. ### Support for multiple expressions