2020-02-23 12:35:47 +01:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2021-09-21 22:00:15 +02:00
|
|
|
"strconv"
|
2020-06-19 01:20:29 +02:00
|
|
|
"strings"
|
2020-02-23 12:35:47 +01:00
|
|
|
|
2020-08-13 15:43:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate"
|
2021-12-02 23:08:42 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
2020-02-23 12:35:47 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RelabelConfig represents relabel config.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
|
|
|
|
type RelabelConfig struct {
|
2021-09-09 15:18:19 +02:00
|
|
|
SourceLabels []string `yaml:"source_labels,flow,omitempty"`
|
|
|
|
Separator *string `yaml:"separator,omitempty"`
|
|
|
|
TargetLabel string `yaml:"target_label,omitempty"`
|
|
|
|
Regex *MultiLineRegex `yaml:"regex,omitempty"`
|
|
|
|
Modulus uint64 `yaml:"modulus,omitempty"`
|
|
|
|
Replacement *string `yaml:"replacement,omitempty"`
|
|
|
|
Action string `yaml:"action,omitempty"`
|
2022-02-24 01:26:15 +01:00
|
|
|
If *IfExpression `yaml:"if,omitempty"`
|
2022-06-16 19:24:19 +02:00
|
|
|
|
|
|
|
// Match is used together with Labels for `action: graphite`. For example:
|
|
|
|
// - action: graphite
|
|
|
|
// match: 'foo.*.*.bar'
|
|
|
|
// labels:
|
|
|
|
// job: '$1'
|
|
|
|
// instance: '${2}:8080'
|
|
|
|
Match string `yaml:"match,omitempty"`
|
|
|
|
|
|
|
|
// Labels is used together with Match for `action: graphite`. For example:
|
|
|
|
// - action: graphite
|
|
|
|
// match: 'foo.*.*.bar'
|
|
|
|
// labels:
|
|
|
|
// job: '$1'
|
|
|
|
// instance: '${2}:8080'
|
|
|
|
Labels map[string]string `yaml:"labels,omitempty"`
|
2021-09-09 15:18:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MultiLineRegex contains a regex, which can be split into multiple lines.
|
|
|
|
//
|
|
|
|
// These lines are joined with "|" then.
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// regex:
|
|
|
|
// - foo
|
|
|
|
// - bar
|
|
|
|
//
|
|
|
|
// is equivalent to:
|
|
|
|
//
|
|
|
|
// regex: "foo|bar"
|
|
|
|
type MultiLineRegex struct {
|
2022-05-06 23:02:54 +02:00
|
|
|
S string
|
2021-09-09 15:18:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals mlr from YAML passed to f.
|
|
|
|
func (mlr *MultiLineRegex) UnmarshalYAML(f func(interface{}) error) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := f(&v); err != nil {
|
2022-02-24 01:26:15 +01:00
|
|
|
return fmt.Errorf("cannot parse multiline regex: %w", err)
|
2021-09-09 15:18:19 +02:00
|
|
|
}
|
2021-09-21 22:00:15 +02:00
|
|
|
s, err := stringValue(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-06 23:02:54 +02:00
|
|
|
mlr.S = s
|
2021-09-21 22:00:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringValue(v interface{}) (string, error) {
|
|
|
|
if v == nil {
|
|
|
|
return "null", nil
|
|
|
|
}
|
2021-09-09 15:18:19 +02:00
|
|
|
switch x := v.(type) {
|
|
|
|
case []interface{}:
|
2021-09-21 22:00:15 +02:00
|
|
|
a := make([]string, len(x))
|
2021-09-09 15:18:19 +02:00
|
|
|
for i, xx := range x {
|
2021-09-21 22:00:15 +02:00
|
|
|
s, err := stringValue(xx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-09-09 15:18:19 +02:00
|
|
|
}
|
|
|
|
a[i] = s
|
|
|
|
}
|
2021-09-21 22:00:15 +02:00
|
|
|
return strings.Join(a, "|"), nil
|
|
|
|
case string:
|
|
|
|
return x, nil
|
|
|
|
case float64:
|
|
|
|
return strconv.FormatFloat(x, 'f', -1, 64), nil
|
|
|
|
case int:
|
|
|
|
return strconv.Itoa(x), nil
|
|
|
|
case bool:
|
|
|
|
if x {
|
|
|
|
return "true", nil
|
|
|
|
}
|
|
|
|
return "false", nil
|
2021-09-09 15:18:19 +02:00
|
|
|
default:
|
2021-09-21 22:00:15 +02:00
|
|
|
return "", fmt.Errorf("unexpected type for `regex`: %T; want string or []string", v)
|
2021-09-09 15:18:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML marshals mlr to YAML.
|
|
|
|
func (mlr *MultiLineRegex) MarshalYAML() (interface{}, error) {
|
2022-08-08 02:15:23 +02:00
|
|
|
if strings.ContainsAny(mlr.S, "([") {
|
|
|
|
// The mlr.S contains groups. Fall back to returning the regexp as is without splitting it into parts.
|
|
|
|
// This fixes https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2928 .
|
|
|
|
return mlr.S, nil
|
|
|
|
}
|
2022-05-06 23:02:54 +02:00
|
|
|
a := strings.Split(mlr.S, "|")
|
2021-09-21 22:00:15 +02:00
|
|
|
if len(a) == 1 {
|
|
|
|
return a[0], nil
|
|
|
|
}
|
2021-09-09 15:18:19 +02:00
|
|
|
return a, nil
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:33:55 +01:00
|
|
|
// ParsedConfigs represents parsed relabel configs.
|
|
|
|
type ParsedConfigs struct {
|
2021-06-04 19:27:55 +02:00
|
|
|
prcs []*parsedRelabelConfig
|
|
|
|
relabelDebug bool
|
2021-02-22 15:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of relabel configs in pcs.
|
|
|
|
func (pcs *ParsedConfigs) Len() int {
|
|
|
|
if pcs == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(pcs.prcs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns human-readabale representation for pcs.
|
|
|
|
func (pcs *ParsedConfigs) String() string {
|
|
|
|
if pcs == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2022-06-16 19:24:19 +02:00
|
|
|
var a []string
|
2021-02-22 15:33:55 +01:00
|
|
|
for _, prc := range pcs.prcs {
|
2022-06-16 19:24:19 +02:00
|
|
|
s := "[" + prc.String() + "]"
|
|
|
|
a = append(a, s)
|
2021-02-22 15:33:55 +01:00
|
|
|
}
|
2022-06-16 19:24:19 +02:00
|
|
|
return fmt.Sprintf("%s, relabelDebug=%v", strings.Join(a, ","), pcs.relabelDebug)
|
2021-02-22 15:33:55 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 12:35:47 +01:00
|
|
|
// LoadRelabelConfigs loads relabel configs from the given path.
|
2021-06-04 19:27:55 +02:00
|
|
|
func LoadRelabelConfigs(path string, relabelDebug bool) (*ParsedConfigs, error) {
|
2021-12-02 23:08:42 +01:00
|
|
|
data, err := fs.ReadFileOrHTTP(path)
|
2020-02-23 12:35:47 +01:00
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot read `relabel_configs` from %q: %w", path, err)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2020-08-13 15:43:55 +02:00
|
|
|
data = envtemplate.Replace(data)
|
2021-06-04 19:27:55 +02:00
|
|
|
pcs, err := ParseRelabelConfigsData(data, relabelDebug)
|
2021-02-22 15:33:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot unmarshal `relabel_configs` from %q: %w", path, err)
|
|
|
|
}
|
|
|
|
return pcs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRelabelConfigsData parses relabel configs from the given data.
|
2021-06-04 19:27:55 +02:00
|
|
|
func ParseRelabelConfigsData(data []byte, relabelDebug bool) (*ParsedConfigs, error) {
|
2020-02-23 12:35:47 +01:00
|
|
|
var rcs []RelabelConfig
|
2020-03-06 19:18:28 +01:00
|
|
|
if err := yaml.UnmarshalStrict(data, &rcs); err != nil {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, err
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2021-06-04 19:27:55 +02:00
|
|
|
return ParseRelabelConfigs(rcs, relabelDebug)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRelabelConfigs parses rcs to dst.
|
2021-06-04 19:27:55 +02:00
|
|
|
func ParseRelabelConfigs(rcs []RelabelConfig, relabelDebug bool) (*ParsedConfigs, error) {
|
2020-02-23 12:35:47 +01:00
|
|
|
if len(rcs) == 0 {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, nil
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2021-02-22 15:33:55 +01:00
|
|
|
prcs := make([]*parsedRelabelConfig, len(rcs))
|
2020-02-23 12:35:47 +01:00
|
|
|
for i := range rcs {
|
2021-02-22 15:33:55 +01:00
|
|
|
prc, err := parseRelabelConfig(&rcs[i])
|
2020-02-23 12:35:47 +01:00
|
|
|
if err != nil {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("error when parsing `relabel_config` #%d: %w", i+1, err)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2021-02-22 15:33:55 +01:00
|
|
|
prcs[i] = prc
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2021-02-22 15:33:55 +01:00
|
|
|
return &ParsedConfigs{
|
2021-06-04 19:27:55 +02:00
|
|
|
prcs: prcs,
|
|
|
|
relabelDebug: relabelDebug,
|
2021-02-22 15:33:55 +01:00
|
|
|
}, nil
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:33:55 +01:00
|
|
|
var (
|
|
|
|
defaultOriginalRegexForRelabelConfig = regexp.MustCompile(".*")
|
|
|
|
defaultRegexForRelabelConfig = regexp.MustCompile("^(.*)$")
|
|
|
|
)
|
2020-02-23 12:35:47 +01:00
|
|
|
|
2021-02-22 15:33:55 +01:00
|
|
|
func parseRelabelConfig(rc *RelabelConfig) (*parsedRelabelConfig, error) {
|
2020-02-23 12:35:47 +01:00
|
|
|
sourceLabels := rc.SourceLabels
|
|
|
|
separator := ";"
|
|
|
|
if rc.Separator != nil {
|
|
|
|
separator = *rc.Separator
|
|
|
|
}
|
|
|
|
targetLabel := rc.TargetLabel
|
|
|
|
regexCompiled := defaultRegexForRelabelConfig
|
2021-02-22 15:33:55 +01:00
|
|
|
regexOriginalCompiled := defaultOriginalRegexForRelabelConfig
|
2020-02-23 12:35:47 +01:00
|
|
|
if rc.Regex != nil {
|
2022-05-06 23:02:54 +02:00
|
|
|
regex := rc.Regex.S
|
2021-09-09 15:18:19 +02:00
|
|
|
regexOrig := regex
|
2020-02-23 12:35:47 +01:00
|
|
|
if rc.Action != "replace_all" && rc.Action != "labelmap_all" {
|
2021-09-09 15:18:19 +02:00
|
|
|
regex = "^(?:" + regex + ")$"
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
re, err := regexp.Compile(regex)
|
|
|
|
if err != nil {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("cannot parse `regex` %q: %w", regex, err)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
regexCompiled = re
|
2021-09-09 15:18:19 +02:00
|
|
|
reOriginal, err := regexp.Compile(regexOrig)
|
2021-02-22 15:33:55 +01:00
|
|
|
if err != nil {
|
2021-09-09 15:18:19 +02:00
|
|
|
return nil, fmt.Errorf("cannot parse `regex` %q: %w", regexOrig, err)
|
2021-02-22 15:33:55 +01:00
|
|
|
}
|
|
|
|
regexOriginalCompiled = reOriginal
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
modulus := rc.Modulus
|
|
|
|
replacement := "$1"
|
|
|
|
if rc.Replacement != nil {
|
|
|
|
replacement = *rc.Replacement
|
|
|
|
}
|
2022-06-16 19:24:19 +02:00
|
|
|
var graphiteMatchTemplate *graphiteMatchTemplate
|
|
|
|
if rc.Match != "" {
|
|
|
|
graphiteMatchTemplate = newGraphiteMatchTemplate(rc.Match)
|
|
|
|
}
|
|
|
|
var graphiteLabelRules []graphiteLabelRule
|
|
|
|
if rc.Labels != nil {
|
|
|
|
graphiteLabelRules = newGraphiteLabelRules(rc.Labels)
|
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
action := rc.Action
|
|
|
|
if action == "" {
|
|
|
|
action = "replace"
|
|
|
|
}
|
|
|
|
switch action {
|
2022-06-16 19:24:19 +02:00
|
|
|
case "graphite":
|
|
|
|
if graphiteMatchTemplate == nil {
|
|
|
|
return nil, fmt.Errorf("missing `match` for `action=graphite`; see https://docs.victoriametrics.com/vmagent.html#graphite-relabeling")
|
|
|
|
}
|
|
|
|
if len(graphiteLabelRules) == 0 {
|
|
|
|
return nil, fmt.Errorf("missing `labels` for `action=graphite`; see https://docs.victoriametrics.com/vmagent.html#graphite-relabeling")
|
|
|
|
}
|
|
|
|
if len(rc.SourceLabels) > 0 {
|
|
|
|
return nil, fmt.Errorf("`source_labels` cannot be used with `action=graphite`; see https://docs.victoriametrics.com/vmagent.html#graphite-relabeling")
|
|
|
|
}
|
|
|
|
if rc.TargetLabel != "" {
|
|
|
|
return nil, fmt.Errorf("`target_label` cannot be used with `action=graphite`; see https://docs.victoriametrics.com/vmagent.html#graphite-relabeling")
|
|
|
|
}
|
|
|
|
if rc.Replacement != nil {
|
|
|
|
return nil, fmt.Errorf("`replacement` cannot be used with `action=graphite`; see https://docs.victoriametrics.com/vmagent.html#graphite-relabeling")
|
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
|
|
|
return nil, fmt.Errorf("`regex` cannot be used with `action=graphite`; see https://docs.victoriametrics.com/vmagent.html#graphite-relabeling")
|
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
case "replace":
|
|
|
|
if targetLabel == "" {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=replace`")
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
case "replace_all":
|
|
|
|
if len(sourceLabels) == 0 {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=replace_all`")
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
if targetLabel == "" {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=replace_all`")
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2020-06-23 16:17:58 +02:00
|
|
|
case "keep_if_equal":
|
|
|
|
if len(sourceLabels) < 2 {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("`source_labels` must contain at least two entries for `action=keep_if_equal`; got %q", sourceLabels)
|
2020-06-23 16:17:58 +02:00
|
|
|
}
|
|
|
|
case "drop_if_equal":
|
|
|
|
if len(sourceLabels) < 2 {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("`source_labels` must contain at least two entries for `action=drop_if_equal`; got %q", sourceLabels)
|
2020-06-23 16:17:58 +02:00
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
case "keep":
|
2022-02-24 01:26:15 +01:00
|
|
|
if len(sourceLabels) == 0 && rc.If == nil {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=keep`")
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
case "drop":
|
2022-02-24 01:26:15 +01:00
|
|
|
if len(sourceLabels) == 0 && rc.If == nil {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=drop`")
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
case "hashmod":
|
|
|
|
if len(sourceLabels) == 0 {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=hashmod`")
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
if targetLabel == "" {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=hashmod`")
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
if modulus < 1 {
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("unexpected `modulus` for `action=hashmod`: %d; must be greater than 0", modulus)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2021-09-09 15:18:19 +02:00
|
|
|
case "keep_metrics":
|
2022-05-06 23:02:54 +02:00
|
|
|
if (rc.Regex == nil || rc.Regex.S == "") && rc.If == nil {
|
2021-09-09 15:18:19 +02:00
|
|
|
return nil, fmt.Errorf("`regex` must be non-empty for `action=keep_metrics`")
|
|
|
|
}
|
|
|
|
if len(sourceLabels) > 0 {
|
|
|
|
return nil, fmt.Errorf("`source_labels` must be empty for `action=keep_metrics`; got %q", sourceLabels)
|
|
|
|
}
|
|
|
|
sourceLabels = []string{"__name__"}
|
|
|
|
action = "keep"
|
|
|
|
case "drop_metrics":
|
2022-05-06 23:02:54 +02:00
|
|
|
if (rc.Regex == nil || rc.Regex.S == "") && rc.If == nil {
|
2021-09-09 15:18:19 +02:00
|
|
|
return nil, fmt.Errorf("`regex` must be non-empty for `action=drop_metrics`")
|
|
|
|
}
|
|
|
|
if len(sourceLabels) > 0 {
|
|
|
|
return nil, fmt.Errorf("`source_labels` must be empty for `action=drop_metrics`; got %q", sourceLabels)
|
|
|
|
}
|
|
|
|
sourceLabels = []string{"__name__"}
|
|
|
|
action = "drop"
|
2022-06-01 10:02:37 +02:00
|
|
|
case "uppercase", "lowercase":
|
|
|
|
if len(sourceLabels) == 0 {
|
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=%s`", action)
|
|
|
|
}
|
|
|
|
if targetLabel == "" {
|
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=%s`", action)
|
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
case "labelmap":
|
|
|
|
case "labelmap_all":
|
|
|
|
case "labeldrop":
|
|
|
|
case "labelkeep":
|
|
|
|
default:
|
2021-02-22 15:33:55 +01:00
|
|
|
return nil, fmt.Errorf("unknown `action` %q", action)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2022-06-16 19:24:19 +02:00
|
|
|
if action != "graphite" {
|
|
|
|
if graphiteMatchTemplate != nil {
|
|
|
|
return nil, fmt.Errorf("`match` config cannot be applied to `action=%s`; it is applied only to `action=graphite`", action)
|
|
|
|
}
|
|
|
|
if len(graphiteLabelRules) > 0 {
|
|
|
|
return nil, fmt.Errorf("`labels` config cannot be applied to `action=%s`; it is applied only to `action=graphite`", action)
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 15:33:55 +01:00
|
|
|
return &parsedRelabelConfig{
|
2020-02-23 12:35:47 +01:00
|
|
|
SourceLabels: sourceLabels,
|
|
|
|
Separator: separator,
|
|
|
|
TargetLabel: targetLabel,
|
|
|
|
Regex: regexCompiled,
|
|
|
|
Modulus: modulus,
|
|
|
|
Replacement: replacement,
|
|
|
|
Action: action,
|
2022-02-24 01:26:15 +01:00
|
|
|
If: rc.If,
|
2020-06-19 01:20:29 +02:00
|
|
|
|
2022-06-16 19:24:19 +02:00
|
|
|
graphiteMatchTemplate: graphiteMatchTemplate,
|
|
|
|
graphiteLabelRules: graphiteLabelRules,
|
|
|
|
|
2022-08-21 21:46:54 +02:00
|
|
|
regexOriginal: regexOriginalCompiled,
|
|
|
|
|
|
|
|
hasCaptureGroupInTargetLabel: strings.Contains(targetLabel, "$"),
|
|
|
|
hasCaptureGroupInReplacement: strings.Contains(replacement, "$"),
|
|
|
|
hasLabelReferenceInReplacement: strings.Contains(replacement, "{{"),
|
2021-02-22 15:33:55 +01:00
|
|
|
}, nil
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|