2022-02-24 00:26:15 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
2022-05-06 21:02:54 +00:00
|
|
|
"encoding/json"
|
2022-02-24 00:26:15 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2022-08-26 11:53:02 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/regexutil"
|
2022-02-24 00:26:15 +00:00
|
|
|
"github.com/VictoriaMetrics/metricsql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IfExpression represents `if` expression at RelabelConfig.
|
|
|
|
//
|
|
|
|
// The `if` expression can contain arbitrary PromQL-like label filters such as `metric_name{filters...}`
|
|
|
|
type IfExpression struct {
|
|
|
|
s string
|
|
|
|
lfs []*labelFilter
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:24:19 +00:00
|
|
|
// String returns string representation of ie.
|
|
|
|
func (ie *IfExpression) String() string {
|
|
|
|
if ie == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ie.s
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:26:21 +00:00
|
|
|
// Parse parses `if` expression from s and stores it to ie.
|
|
|
|
func (ie *IfExpression) Parse(s string) error {
|
2022-02-24 00:26:15 +00:00
|
|
|
expr, err := metricsql.Parse(s)
|
|
|
|
if err != nil {
|
2022-04-19 15:26:21 +00:00
|
|
|
return err
|
2022-02-24 00:26:15 +00:00
|
|
|
}
|
|
|
|
me, ok := expr.(*metricsql.MetricExpr)
|
|
|
|
if !ok {
|
2022-04-19 15:26:21 +00:00
|
|
|
return fmt.Errorf("expecting series selector; got %q", expr.AppendString(nil))
|
2022-02-24 00:26:15 +00:00
|
|
|
}
|
|
|
|
lfs, err := metricExprToLabelFilters(me)
|
|
|
|
if err != nil {
|
2022-04-19 15:26:21 +00:00
|
|
|
return fmt.Errorf("cannot parse series selector: %w", err)
|
2022-02-24 00:26:15 +00:00
|
|
|
}
|
|
|
|
ie.s = s
|
|
|
|
ie.lfs = lfs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:02:54 +00:00
|
|
|
// UnmarshalJSON unmarshals ie from JSON data.
|
|
|
|
func (ie *IfExpression) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ie.Parse(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals ie to JSON.
|
|
|
|
func (ie *IfExpression) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(ie.s)
|
|
|
|
}
|
|
|
|
|
2022-04-19 15:26:21 +00:00
|
|
|
// UnmarshalYAML unmarshals ie from YAML passed to f.
|
|
|
|
func (ie *IfExpression) UnmarshalYAML(f func(interface{}) error) error {
|
|
|
|
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 00:26:15 +00:00
|
|
|
// MarshalYAML marshals ie to YAML.
|
|
|
|
func (ie *IfExpression) MarshalYAML() (interface{}, error) {
|
|
|
|
return ie.s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if ie matches the given labels.
|
|
|
|
func (ie *IfExpression) Match(labels []prompbmarshal.Label) bool {
|
2022-12-31 00:47:17 +00:00
|
|
|
if ie == nil {
|
|
|
|
return true
|
|
|
|
}
|
2022-02-24 00:26:15 +00:00
|
|
|
for _, lf := range ie.lfs {
|
|
|
|
if !lf.match(labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func metricExprToLabelFilters(me *metricsql.MetricExpr) ([]*labelFilter, error) {
|
|
|
|
lfs := make([]*labelFilter, len(me.LabelFilters))
|
|
|
|
for i := range me.LabelFilters {
|
|
|
|
lf, err := newLabelFilter(&me.LabelFilters[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse %s: %w", me.AppendString(nil), err)
|
|
|
|
}
|
|
|
|
lfs[i] = lf
|
|
|
|
}
|
|
|
|
return lfs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 11:53:02 +00:00
|
|
|
re *regexutil.PromRegex
|
2022-02-24 00:26:15 +00: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 11:53:02 +00:00
|
|
|
re, err := regexutil.NewPromRegex(lf.value)
|
2022-02-24 00:26:15 +00: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 11:53:02 +00:00
|
|
|
return lf.matchRegexp(labels)
|
2022-02-24 00:26:15 +00:00
|
|
|
case "!~":
|
2022-08-26 11:53:02 +00:00
|
|
|
return !lf.matchRegexp(labels)
|
2022-02-24 00:26:15 +00: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 11:53:02 +00:00
|
|
|
func (lf *labelFilter) matchRegexp(labels []prompbmarshal.Label) bool {
|
2022-02-24 00:26:15 +00: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 "="
|
|
|
|
}
|