| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package selectorcore |
| 4 | |
| 5 | import "github.com/netdata/netdata/go/plugins/pkg/matcher" |
| 6 | |
| 7 | // Labels is the minimal key/value label accessor used by selector matching. |
| 8 | type Labels interface { |
| 9 | Get(key string) (string, bool) |
| 10 | } |
| 11 | |
| 12 | // Selector matches one metric series represented by metric name + labels. |
| 13 | type Selector interface { |
| 14 | Matches(metricName string, labels Labels) bool |
| 15 | } |
| 16 | |
| 17 | const ( |
| 18 | MetricNameLabel = "__name__" |
| 19 | OpEqual = "=" |
| 20 | OpNegEqual = "!=" |
| 21 | OpRegexp = "=~" |
| 22 | OpNegRegexp = "!~" |
| 23 | OpSimplePatterns = "=*" |
| 24 | OpNegSimplePatterns = "!*" |
| 25 | ) |
| 26 | |
| 27 | type labelSelector struct { |
| 28 | name string |
| 29 | m matcher.Matcher |
| 30 | } |
| 31 | |
| 32 | func (s labelSelector) Matches(metricName string, labels Labels) bool { |
| 33 | if s.name == MetricNameLabel { |
| 34 | return s.m.MatchString(metricName) |
| 35 | } |
| 36 | if labels == nil { |
| 37 | return false |
| 38 | } |
| 39 | if value, ok := labels.Get(s.name); ok { |
| 40 | return s.m.MatchString(value) |
| 41 | } |
| 42 | return false |
| 43 | } |
| 44 | |
| 45 | // Func is an adapter for ad-hoc selector functions. |
| 46 | type Func func(metricName string, labels Labels) bool |
| 47 | |
| 48 | func (fn Func) Matches(metricName string, labels Labels) bool { |
| 49 | return fn(metricName, labels) |
| 50 | } |