| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package selector |
| 4 | |
| 5 | import "github.com/netdata/netdata/go/plugins/pkg/selectorcore" |
| 6 | |
| 7 | // Meta contains selector metadata needed by template compilers/engines. |
| 8 | type Meta struct { |
| 9 | MetricNames []string |
| 10 | ConstrainedLabelKeys []string |
| 11 | } |
| 12 | |
| 13 | // Compiled is a selector with stable metadata. |
| 14 | type Compiled interface { |
| 15 | Selector |
| 16 | Meta() Meta |
| 17 | } |
| 18 | |
| 19 | type compiledSelector struct { |
| 20 | Selector |
| 21 | meta Meta |
| 22 | } |
| 23 | |
| 24 | func (c compiledSelector) Meta() Meta { |
| 25 | return Meta{ |
| 26 | MetricNames: append([]string(nil), c.meta.MetricNames...), |
| 27 | ConstrainedLabelKeys: append([]string(nil), c.meta.ConstrainedLabelKeys...), |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // ParseCompiled parses a selector expression and returns matcher + metadata. |
| 32 | func ParseCompiled(expr string) (Compiled, error) { |
| 33 | compiled, err := selectorcore.ParseCompiled(expr) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | meta := compiled.Meta() |
| 38 | out := compiledSelector{Selector: wrapCoreSelector(compiled)} |
| 39 | out.meta = Meta{ |
| 40 | MetricNames: append([]string(nil), meta.MetricNames...), |
| 41 | ConstrainedLabelKeys: append([]string(nil), meta.ConstrainedLabelKeys...), |
| 42 | } |
| 43 | return out, nil |
| 44 | } |