2022-02-24 01:26:15 +01:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
2022-05-06 23:02:54 +02:00
|
|
|
"encoding/json"
|
2022-02-24 01:26:15 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2022-08-26 13:53:02 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/regexutil"
|
2022-02-24 01:26:15 +01:00
|
|
|
"github.com/VictoriaMetrics/metricsql"
|
|
|
|
)
|
|
|
|
|
2023-07-25 01:10:47 +02:00
|
|
|
// IfExpression represents PromQL-like label filters such as `metric_name{filters...}`.
|
2022-02-24 01:26:15 +01:00
|
|
|
//
|
2023-07-25 01:10:47 +02:00
|
|
|
// It may contain either a single filter or multiple filters, which are executed with `or` operator.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// if: 'foo{bar="baz"}'
|
|
|
|
//
|
|
|
|
// if:
|
|
|
|
// - 'foo{bar="baz"}'
|
|
|
|
// - '{x=~"y"}'
|
2022-02-24 01:26:15 +01:00
|
|
|
type IfExpression struct {
|
2023-07-25 01:10:47 +02:00
|
|
|
ies []*ifExpression
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if labels match at least a single label filter inside ie.
|
|
|
|
//
|
|
|
|
// Match returns true for empty ie.
|
|
|
|
func (ie *IfExpression) Match(labels []prompbmarshal.Label) bool {
|
|
|
|
if ie == nil || len(ie.ies) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, ie := range ie.ies {
|
|
|
|
if ie.Match(labels) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse parses ie from s.
|
|
|
|
func (ie *IfExpression) Parse(s string) error {
|
|
|
|
ieLocal, err := newIfExpression(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ie.ies = []*ifExpression{ieLocal}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals ie from JSON data.
|
|
|
|
func (ie *IfExpression) UnmarshalJSON(data []byte) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ie.unmarshalFromInterface(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals ie to JSON.
|
|
|
|
func (ie *IfExpression) MarshalJSON() ([]byte, error) {
|
|
|
|
if ie == nil || len(ie.ies) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if len(ie.ies) == 1 {
|
|
|
|
return json.Marshal(ie.ies[0])
|
|
|
|
}
|
|
|
|
return json.Marshal(ie.ies)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals ie from YAML passed to f.
|
|
|
|
func (ie *IfExpression) UnmarshalYAML(f func(interface{}) error) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := f(&v); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal `match` option: %w", err)
|
|
|
|
}
|
|
|
|
return ie.unmarshalFromInterface(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ie *IfExpression) unmarshalFromInterface(v interface{}) error {
|
|
|
|
ies := ie.ies[:0]
|
|
|
|
switch t := v.(type) {
|
|
|
|
case string:
|
|
|
|
ieLocal, err := newIfExpression(t)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unexpected `match` option: %w", err)
|
|
|
|
}
|
|
|
|
ies = append(ies, ieLocal)
|
|
|
|
case []interface{}:
|
|
|
|
for _, x := range t {
|
|
|
|
s, ok := x.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unexpected `match` item type; got %#v; want string", x)
|
|
|
|
}
|
|
|
|
ieLocal, err := newIfExpression(s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unexpected `match` item: %w", err)
|
|
|
|
}
|
|
|
|
ies = append(ies, ieLocal)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unexpected `match` type; got %#v; want string or an array of strings", t)
|
|
|
|
}
|
|
|
|
ie.ies = ies
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML marshals ie to YAML
|
|
|
|
func (ie *IfExpression) MarshalYAML() (interface{}, error) {
|
|
|
|
if ie == nil || len(ie.ies) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if len(ie.ies) == 1 {
|
|
|
|
return ie.ies[0].MarshalYAML()
|
|
|
|
}
|
|
|
|
a := make([]string, 0, len(ie.ies))
|
|
|
|
for _, ieLocal := range ie.ies {
|
|
|
|
v, err := ieLocal.MarshalYAML()
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
s := v.(string)
|
|
|
|
a = append(a, s)
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIfExpression(s string) (*ifExpression, error) {
|
|
|
|
var ie ifExpression
|
|
|
|
if err := ie.Parse(s); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ie, nil
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 19:24:19 +02:00
|
|
|
// String returns string representation of ie.
|
|
|
|
func (ie *IfExpression) String() string {
|
2023-07-25 01:10:47 +02:00
|
|
|
if ie == nil {
|
2023-07-25 04:30:47 +02:00
|
|
|
return "{}"
|
|
|
|
}
|
|
|
|
if len(ie.ies) == 1 {
|
|
|
|
return ie.ies[0].String()
|
2023-07-25 01:10:47 +02:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s", ie.ies)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ifExpression struct {
|
|
|
|
s string
|
|
|
|
lfss [][]*labelFilter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ie *ifExpression) String() string {
|
2022-06-16 19:24:19 +02:00
|
|
|
if ie == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ie.s
|
|
|
|
}
|
|
|
|
|
2023-07-25 01:10:47 +02:00
|
|
|
func (ie *ifExpression) Parse(s string) error {
|
2022-02-24 01:26:15 +01:00
|
|
|
expr, err := metricsql.Parse(s)
|
|
|
|
if err != nil {
|
2022-04-19 17:26:21 +02:00
|
|
|
return err
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
|
|
|
me, ok := expr.(*metricsql.MetricExpr)
|
|
|
|
if !ok {
|
2022-04-19 17:26:21 +02:00
|
|
|
return fmt.Errorf("expecting series selector; got %q", expr.AppendString(nil))
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
2023-07-16 08:48:21 +02:00
|
|
|
lfss, err := metricExprToLabelFilterss(me)
|
2022-02-24 01:26:15 +01:00
|
|
|
if err != nil {
|
2022-04-19 17:26:21 +02:00
|
|
|
return fmt.Errorf("cannot parse series selector: %w", err)
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
|
|
|
ie.s = s
|
2023-07-16 08:48:21 +02:00
|
|
|
ie.lfss = lfss
|
2022-02-24 01:26:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:02:54 +02:00
|
|
|
// UnmarshalJSON unmarshals ie from JSON data.
|
2023-07-25 01:10:47 +02:00
|
|
|
func (ie *ifExpression) UnmarshalJSON(data []byte) error {
|
2022-05-06 23:02:54 +02:00
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ie.Parse(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals ie to JSON.
|
2023-07-25 01:10:47 +02:00
|
|
|
func (ie *ifExpression) MarshalJSON() ([]byte, error) {
|
2022-05-06 23:02:54 +02:00
|
|
|
return json.Marshal(ie.s)
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:26:21 +02:00
|
|
|
// UnmarshalYAML unmarshals ie from YAML passed to f.
|
2023-07-25 01:10:47 +02:00
|
|
|
func (ie *ifExpression) UnmarshalYAML(f func(interface{}) error) error {
|
2022-04-19 17:26:21 +02:00
|
|
|
var s string
|
|
|
|
if err := f(&s); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal `if` option: %w", err)
|
|
|
|
}
|
|
|
|
if err := ie.Parse(s); err != nil {
|
|
|
|
return fmt.Errorf("cannot parse `if` series selector: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-24 01:26:15 +01:00
|
|
|
// MarshalYAML marshals ie to YAML.
|
2023-07-25 01:10:47 +02:00
|
|
|
func (ie *ifExpression) MarshalYAML() (interface{}, error) {
|
2022-02-24 01:26:15 +01:00
|
|
|
return ie.s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if ie matches the given labels.
|
2023-07-25 01:10:47 +02:00
|
|
|
func (ie *ifExpression) Match(labels []prompbmarshal.Label) bool {
|
2022-12-31 01:47:17 +01:00
|
|
|
if ie == nil {
|
|
|
|
return true
|
|
|
|
}
|
2023-07-16 08:48:21 +02:00
|
|
|
for _, lfs := range ie.lfss {
|
|
|
|
if matchLabelFilters(lfs, labels) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchLabelFilters(lfs []*labelFilter, labels []prompbmarshal.Label) bool {
|
|
|
|
for _, lf := range lfs {
|
2022-02-24 01:26:15 +01:00
|
|
|
if !lf.match(labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-07-16 08:48:21 +02:00
|
|
|
func metricExprToLabelFilterss(me *metricsql.MetricExpr) ([][]*labelFilter, error) {
|
|
|
|
lfssNew := make([][]*labelFilter, len(me.LabelFilterss))
|
|
|
|
for i, lfs := range me.LabelFilterss {
|
|
|
|
lfsNew := make([]*labelFilter, len(lfs))
|
|
|
|
for j := range lfs {
|
|
|
|
lf, err := newLabelFilter(&lfs[j])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse %s: %w", me.AppendString(nil), err)
|
|
|
|
}
|
|
|
|
lfsNew[j] = lf
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
2023-07-16 08:48:21 +02:00
|
|
|
lfssNew[i] = lfsNew
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
2023-07-16 08:48:21 +02:00
|
|
|
return lfssNew, nil
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// labelFilter contains PromQL filter for `{label op "value"}`
|
|
|
|
type labelFilter struct {
|
|
|
|
label string
|
|
|
|
op string
|
|
|
|
value string
|
|
|
|
|
|
|
|
// re contains compiled regexp for `=~` and `!~` op.
|
2022-08-26 13:53:02 +02:00
|
|
|
re *regexutil.PromRegex
|
2022-02-24 01:26:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLabelFilter(mlf *metricsql.LabelFilter) (*labelFilter, error) {
|
|
|
|
lf := &labelFilter{
|
|
|
|
label: toCanonicalLabelName(mlf.Label),
|
|
|
|
op: getFilterOp(mlf),
|
|
|
|
value: mlf.Value,
|
|
|
|
}
|
|
|
|
if lf.op == "=~" || lf.op == "!~" {
|
2022-08-26 13:53:02 +02:00
|
|
|
re, err := regexutil.NewPromRegex(lf.value)
|
2022-02-24 01:26:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse regexp for %s: %w", mlf.AppendString(nil), err)
|
|
|
|
}
|
|
|
|
lf.re = re
|
|
|
|
}
|
|
|
|
return lf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lf *labelFilter) match(labels []prompbmarshal.Label) bool {
|
|
|
|
switch lf.op {
|
|
|
|
case "=":
|
|
|
|
return lf.equalValue(labels)
|
|
|
|
case "!=":
|
|
|
|
return !lf.equalValue(labels)
|
|
|
|
case "=~":
|
2022-08-26 13:53:02 +02:00
|
|
|
return lf.matchRegexp(labels)
|
2022-02-24 01:26:15 +01:00
|
|
|
case "!~":
|
2022-08-26 13:53:02 +02:00
|
|
|
return !lf.matchRegexp(labels)
|
2022-02-24 01:26:15 +01:00
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unexpected operation for label filter: %s", lf.op)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lf *labelFilter) equalValue(labels []prompbmarshal.Label) bool {
|
|
|
|
labelNameMatches := 0
|
|
|
|
for _, label := range labels {
|
|
|
|
if toCanonicalLabelName(label.Name) != lf.label {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
labelNameMatches++
|
|
|
|
if label.Value == lf.value {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if labelNameMatches == 0 {
|
|
|
|
// Special case for {non_existing_label=""}, which matches anything except of non-empty non_existing_label
|
|
|
|
return lf.value == ""
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-08-26 13:53:02 +02:00
|
|
|
func (lf *labelFilter) matchRegexp(labels []prompbmarshal.Label) bool {
|
2022-02-24 01:26:15 +01:00
|
|
|
labelNameMatches := 0
|
|
|
|
for _, label := range labels {
|
|
|
|
if toCanonicalLabelName(label.Name) != lf.label {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
labelNameMatches++
|
|
|
|
if lf.re.MatchString(label.Value) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if labelNameMatches == 0 {
|
|
|
|
// Special case for {non_existing_label=~"something|"}, which matches empty non_existing_label
|
|
|
|
return lf.re.MatchString("")
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCanonicalLabelName(labelName string) string {
|
|
|
|
if labelName == "__name__" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return labelName
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFilterOp(mlf *metricsql.LabelFilter) string {
|
|
|
|
if mlf.IsNegative {
|
|
|
|
if mlf.IsRegexp {
|
|
|
|
return "!~"
|
|
|
|
}
|
|
|
|
return "!="
|
|
|
|
}
|
|
|
|
if mlf.IsRegexp {
|
|
|
|
return "=~"
|
|
|
|
}
|
|
|
|
return "="
|
|
|
|
}
|