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

Rename "label" to "attribute" in expression matcher DSL #5862

Closed
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
18 changes: 14 additions & 4 deletions internal/coreinternal/processor/filterexpr/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions internal/coreinternal/processor/filterexpr/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")}))
Expand Down
16 changes: 10 additions & 6 deletions processor/filterprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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

Expand Down