| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package selector |
| 4 | |
| 5 | import ( |
| 6 | "github.com/netdata/netdata/go/plugins/pkg/metrix" |
| 7 | "github.com/netdata/netdata/go/plugins/pkg/selectorcore" |
| 8 | ) |
| 9 | |
| 10 | // Selector matches one metrix series. |
| 11 | type Selector interface { |
| 12 | Matches(metricName string, labels metrix.LabelView) bool |
| 13 | } |
| 14 | |
| 15 | const ( |
| 16 | OpEqual = selectorcore.OpEqual |
| 17 | OpNegEqual = selectorcore.OpNegEqual |
| 18 | OpRegexp = selectorcore.OpRegexp |
| 19 | OpNegRegexp = selectorcore.OpNegRegexp |
| 20 | OpSimplePatterns = selectorcore.OpSimplePatterns |
| 21 | OpNegSimplePatterns = selectorcore.OpNegSimplePatterns |
| 22 | ) |
| 23 | |
| 24 | // Func is an adapter for ad-hoc selector functions. |
| 25 | type Func func(metricName string, labels metrix.LabelView) bool |
| 26 | |
| 27 | func (fn Func) Matches(metricName string, labels metrix.LabelView) bool { |
| 28 | return fn(metricName, labels) |
| 29 | } |
| 30 | |
| 31 | type coreMetrixSelector struct { |
| 32 | core selectorcore.Selector |
| 33 | } |
| 34 | |
| 35 | func (s coreMetrixSelector) Matches(metricName string, labels metrix.LabelView) bool { |
| 36 | return s.core.Matches(metricName, metrixLabelsView{labels: labels}) |
| 37 | } |
| 38 | |
| 39 | func wrapCoreSelector(sel selectorcore.Selector) Selector { |
| 40 | if sel == nil { |
| 41 | return nil |
| 42 | } |
| 43 | return coreMetrixSelector{core: sel} |
| 44 | } |
| 45 | |
| 46 | type metrixLabelsView struct { |
| 47 | labels metrix.LabelView |
| 48 | } |
| 49 | |
| 50 | func (v metrixLabelsView) Get(key string) (string, bool) { |
| 51 | if v.labels == nil { |
| 52 | return "", false |
| 53 | } |
| 54 | return v.labels.Get(key) |
| 55 | } |