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

Improve performance of metric expression matcher #5864

Merged
merged 1 commit into from
Oct 21, 2021
Merged
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
29 changes: 15 additions & 14 deletions internal/coreinternal/processor/filterexpr/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ type Matcher struct {

type env struct {
MetricName string
// TODO: replace this with GetLabel func(key string) (string,bool)
HasLabel func(key string) bool
Label func(key string) string
attributes pdata.AttributeMap
}

func (e *env) HasLabel(key string) bool {
_, ok := e.attributes.Get(key)
return ok
}

func (e *env) Label(key string) string {
v, _ := e.attributes.Get(key)
return v.StringVal()
}

func NewMatcher(expression string) (*Matcher, error) {
Expand Down Expand Up @@ -100,21 +108,14 @@ func (m *Matcher) matchEnv(metricName string, attributes pdata.AttributeMap) (bo
return m.match(createEnv(metricName, attributes))
}

func createEnv(metricName string, attributes pdata.AttributeMap) env {
return env{
func createEnv(metricName string, attributes pdata.AttributeMap) *env {
return &env{
MetricName: metricName,
HasLabel: func(key string) bool {
_, ok := attributes.Get(key)
return ok
},
Label: func(key string) string {
v, _ := attributes.Get(key)
return v.StringVal()
},
attributes: attributes,
}
}

func (m *Matcher) match(env env) (bool, error) {
func (m *Matcher) match(env *env) (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 @@ -30,7 +30,7 @@ func TestCompileExprError(t *testing.T) {
func TestRunExprError(t *testing.T) {
matcher, err := NewMatcher("foo")
require.NoError(t, err)
matched, _ := matcher.match(env{})
matched, _ := matcher.match(&env{})
require.False(t, matched)
}

Expand Down