2020-02-23 11:35:47 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2021-09-21 20:00:15 +00:00
|
|
|
"strconv"
|
2020-06-18 23:20:29 +00:00
|
|
|
"strings"
|
2020-02-23 11:35:47 +00:00
|
|
|
|
2022-09-30 07:43:31 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-08-13 13:43:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate"
|
2024-01-21 19:58:26 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs/fscore"
|
2022-08-26 12:23:41 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2022-08-24 14:54:26 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/regexutil"
|
2020-02-23 11:35:47 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RelabelConfig represents relabel config.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
|
|
|
|
type RelabelConfig struct {
|
2022-12-10 10:09:21 +00:00
|
|
|
If *IfExpression `yaml:"if,omitempty"`
|
|
|
|
Action string `yaml:"action,omitempty"`
|
2021-09-09 13:18:19 +00: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"`
|
2022-06-16 17:24:19 +00: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 13:18:19 +00: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 21:02:54 +00:00
|
|
|
S string
|
2021-09-09 13:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML unmarshals mlr from YAML passed to f.
|
2024-07-09 22:14:15 +00:00
|
|
|
func (mlr *MultiLineRegex) UnmarshalYAML(f func(any) error) error {
|
|
|
|
var v any
|
2021-09-09 13:18:19 +00:00
|
|
|
if err := f(&v); err != nil {
|
2022-02-24 00:26:15 +00:00
|
|
|
return fmt.Errorf("cannot parse multiline regex: %w", err)
|
2021-09-09 13:18:19 +00:00
|
|
|
}
|
2021-09-21 20:00:15 +00:00
|
|
|
s, err := stringValue(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-06 21:02:54 +00:00
|
|
|
mlr.S = s
|
2021-09-21 20:00:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-09 22:14:15 +00:00
|
|
|
func stringValue(v any) (string, error) {
|
2021-09-21 20:00:15 +00:00
|
|
|
if v == nil {
|
|
|
|
return "null", nil
|
|
|
|
}
|
2021-09-09 13:18:19 +00:00
|
|
|
switch x := v.(type) {
|
2024-07-09 22:14:15 +00:00
|
|
|
case []any:
|
2021-09-21 20:00:15 +00:00
|
|
|
a := make([]string, len(x))
|
2021-09-09 13:18:19 +00:00
|
|
|
for i, xx := range x {
|
2021-09-21 20:00:15 +00:00
|
|
|
s, err := stringValue(xx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-09-09 13:18:19 +00:00
|
|
|
}
|
|
|
|
a[i] = s
|
|
|
|
}
|
2021-09-21 20:00:15 +00: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 13:18:19 +00:00
|
|
|
default:
|
2021-09-21 20:00:15 +00:00
|
|
|
return "", fmt.Errorf("unexpected type for `regex`: %T; want string or []string", v)
|
2021-09-09 13:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML marshals mlr to YAML.
|
2024-07-09 22:14:15 +00:00
|
|
|
func (mlr *MultiLineRegex) MarshalYAML() (any, error) {
|
2022-08-08 00:15:23 +00: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 21:02:54 +00:00
|
|
|
a := strings.Split(mlr.S, "|")
|
2021-09-21 20:00:15 +00:00
|
|
|
if len(a) == 1 {
|
|
|
|
return a[0], nil
|
|
|
|
}
|
2021-09-09 13:18:19 +00:00
|
|
|
return a, nil
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
// ParsedConfigs represents parsed relabel configs.
|
|
|
|
type ParsedConfigs struct {
|
2022-12-10 10:09:21 +00:00
|
|
|
prcs []*parsedRelabelConfig
|
2021-02-22 14:33:55 +00: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 17:24:19 +00:00
|
|
|
var a []string
|
2021-02-22 14:33:55 +00:00
|
|
|
for _, prc := range pcs.prcs {
|
2022-12-10 10:09:21 +00:00
|
|
|
s := prc.String()
|
|
|
|
lines := strings.Split(s, "\n")
|
|
|
|
lines[0] = "- " + lines[0]
|
|
|
|
for i := range lines[1:] {
|
|
|
|
line := &lines[1+i]
|
|
|
|
if len(*line) > 0 {
|
|
|
|
*line = " " + *line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = strings.Join(lines, "\n")
|
2022-06-16 17:24:19 +00:00
|
|
|
a = append(a, s)
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
return strings.Join(a, "")
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
// LoadRelabelConfigs loads relabel configs from the given path.
|
2022-12-10 10:09:21 +00:00
|
|
|
func LoadRelabelConfigs(path string) (*ParsedConfigs, error) {
|
2024-01-21 19:58:26 +00:00
|
|
|
data, err := fscore.ReadFileOrHTTP(path)
|
2020-02-23 11:35:47 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot read `relabel_configs` from %q: %w", path, err)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-10-26 11:49:20 +00:00
|
|
|
data, err = envtemplate.ReplaceBytes(data)
|
2022-10-18 07:28:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot expand environment vars at %q: %w", path, err)
|
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
pcs, err := ParseRelabelConfigsData(data)
|
2021-02-22 14:33:55 +00: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.
|
2022-12-10 10:09:21 +00:00
|
|
|
func ParseRelabelConfigsData(data []byte) (*ParsedConfigs, error) {
|
2020-02-23 11:35:47 +00:00
|
|
|
var rcs []RelabelConfig
|
2020-03-06 18:18:28 +00:00
|
|
|
if err := yaml.UnmarshalStrict(data, &rcs); err != nil {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, err
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
return ParseRelabelConfigs(rcs)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRelabelConfigs parses rcs to dst.
|
2022-12-10 10:09:21 +00:00
|
|
|
func ParseRelabelConfigs(rcs []RelabelConfig) (*ParsedConfigs, error) {
|
2020-02-23 11:35:47 +00:00
|
|
|
if len(rcs) == 0 {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, nil
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2021-02-22 14:33:55 +00:00
|
|
|
prcs := make([]*parsedRelabelConfig, len(rcs))
|
2020-02-23 11:35:47 +00:00
|
|
|
for i := range rcs {
|
2021-02-22 14:33:55 +00:00
|
|
|
prc, err := parseRelabelConfig(&rcs[i])
|
2020-02-23 11:35:47 +00:00
|
|
|
if err != nil {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("error when parsing `relabel_config` #%d: %w", i+1, err)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2021-02-22 14:33:55 +00:00
|
|
|
prcs[i] = prc
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2021-02-22 14:33:55 +00:00
|
|
|
return &ParsedConfigs{
|
2022-12-10 10:09:21 +00:00
|
|
|
prcs: prcs,
|
2021-02-22 14:33:55 +00:00
|
|
|
}, nil
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
var (
|
|
|
|
defaultOriginalRegexForRelabelConfig = regexp.MustCompile(".*")
|
|
|
|
defaultRegexForRelabelConfig = regexp.MustCompile("^(.*)$")
|
2022-08-26 12:23:41 +00:00
|
|
|
defaultPromRegex = func() *regexutil.PromRegex {
|
|
|
|
pr, err := regexutil.NewPromRegex(".*")
|
|
|
|
if err != nil {
|
2023-10-25 19:24:01 +00:00
|
|
|
panic(fmt.Errorf("BUG: unexpected error: %w", err))
|
2022-08-26 12:23:41 +00:00
|
|
|
}
|
|
|
|
return pr
|
|
|
|
}()
|
2021-02-22 14:33:55 +00:00
|
|
|
)
|
2020-02-23 11:35:47 +00:00
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
func parseRelabelConfig(rc *RelabelConfig) (*parsedRelabelConfig, error) {
|
2020-02-23 11:35:47 +00:00
|
|
|
sourceLabels := rc.SourceLabels
|
|
|
|
separator := ";"
|
|
|
|
if rc.Separator != nil {
|
|
|
|
separator = *rc.Separator
|
|
|
|
}
|
2022-08-26 08:57:12 +00:00
|
|
|
action := strings.ToLower(rc.Action)
|
|
|
|
if action == "" {
|
|
|
|
action = "replace"
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
targetLabel := rc.TargetLabel
|
2022-08-26 12:23:41 +00:00
|
|
|
regexAnchored := defaultRegexForRelabelConfig
|
2021-02-22 14:33:55 +00:00
|
|
|
regexOriginalCompiled := defaultOriginalRegexForRelabelConfig
|
2022-08-26 12:23:41 +00:00
|
|
|
promRegex := defaultPromRegex
|
2022-08-26 08:57:12 +00:00
|
|
|
if rc.Regex != nil && !isDefaultRegex(rc.Regex.S) {
|
|
|
|
regex := rc.Regex.S
|
2021-09-09 13:18:19 +00:00
|
|
|
regexOrig := regex
|
2020-02-23 11:35:47 +00:00
|
|
|
if rc.Action != "replace_all" && rc.Action != "labelmap_all" {
|
2022-08-26 08:57:12 +00:00
|
|
|
regex = regexutil.RemoveStartEndAnchors(regex)
|
2022-08-26 12:35:16 +00:00
|
|
|
regexOrig = regex
|
2021-09-09 13:18:19 +00:00
|
|
|
regex = "^(?:" + regex + ")$"
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
re, err := regexp.Compile(regex)
|
|
|
|
if err != nil {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse `regex` %q: %w", regex, err)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-08-26 12:23:41 +00:00
|
|
|
regexAnchored = re
|
2021-09-09 13:18:19 +00:00
|
|
|
reOriginal, err := regexp.Compile(regexOrig)
|
2021-02-22 14:33:55 +00:00
|
|
|
if err != nil {
|
2021-09-09 13:18:19 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse `regex` %q: %w", regexOrig, err)
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
|
|
|
regexOriginalCompiled = reOriginal
|
2022-08-26 12:23:41 +00:00
|
|
|
promRegex, err = regexutil.NewPromRegex(regexOrig)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: cannot parse already parsed regex %q: %s", regexOrig, err)
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
modulus := rc.Modulus
|
|
|
|
replacement := "$1"
|
|
|
|
if rc.Replacement != nil {
|
|
|
|
replacement = *rc.Replacement
|
|
|
|
}
|
2022-06-16 17:24:19 +00: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 11:35:47 +00:00
|
|
|
switch action {
|
2022-06-16 17:24:19 +00:00
|
|
|
case "graphite":
|
|
|
|
if graphiteMatchTemplate == nil {
|
2024-04-17 23:31:37 +00:00
|
|
|
return nil, fmt.Errorf("missing `match` for `action=graphite`; see https://docs.victoriametrics.com/vmagent/#graphite-relabeling")
|
2022-06-16 17:24:19 +00:00
|
|
|
}
|
|
|
|
if len(graphiteLabelRules) == 0 {
|
2024-04-17 23:31:37 +00:00
|
|
|
return nil, fmt.Errorf("missing `labels` for `action=graphite`; see https://docs.victoriametrics.com/vmagent/#graphite-relabeling")
|
2022-06-16 17:24:19 +00:00
|
|
|
}
|
|
|
|
if len(rc.SourceLabels) > 0 {
|
2024-04-17 23:31:37 +00:00
|
|
|
return nil, fmt.Errorf("`source_labels` cannot be used with `action=graphite`; see https://docs.victoriametrics.com/vmagent/#graphite-relabeling")
|
2022-06-16 17:24:19 +00:00
|
|
|
}
|
|
|
|
if rc.TargetLabel != "" {
|
2024-04-17 23:31:37 +00:00
|
|
|
return nil, fmt.Errorf("`target_label` cannot be used with `action=graphite`; see https://docs.victoriametrics.com/vmagent/#graphite-relabeling")
|
2022-06-16 17:24:19 +00:00
|
|
|
}
|
|
|
|
if rc.Replacement != nil {
|
2024-04-17 23:31:37 +00:00
|
|
|
return nil, fmt.Errorf("`replacement` cannot be used with `action=graphite`; see https://docs.victoriametrics.com/vmagent/#graphite-relabeling")
|
2022-06-16 17:24:19 +00:00
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
2024-04-17 23:31:37 +00:00
|
|
|
return nil, fmt.Errorf("`regex` cannot be used for `action=graphite`; see https://docs.victoriametrics.com/vmagent/#graphite-relabeling")
|
2022-06-16 17:24:19 +00:00
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
case "replace":
|
|
|
|
if targetLabel == "" {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=replace`")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
case "replace_all":
|
|
|
|
if len(sourceLabels) == 0 {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=replace_all`")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
if targetLabel == "" {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=replace_all`")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2023-11-29 09:00:48 +00:00
|
|
|
case "keep_if_contains":
|
|
|
|
if targetLabel == "" {
|
|
|
|
return nil, fmt.Errorf("`target_label` must be set for `action=keep_if_containes`")
|
|
|
|
}
|
|
|
|
if len(sourceLabels) == 0 {
|
|
|
|
return nil, fmt.Errorf("`source_labels` must contain at least a single entry for `action=keep_if_contains`")
|
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
|
|
|
return nil, fmt.Errorf("`regex` cannot be used for `action=keep_if_contains`")
|
|
|
|
}
|
|
|
|
case "drop_if_contains":
|
|
|
|
if targetLabel == "" {
|
|
|
|
return nil, fmt.Errorf("`target_label` must be set for `action=drop_if_containes`")
|
|
|
|
}
|
|
|
|
if len(sourceLabels) == 0 {
|
|
|
|
return nil, fmt.Errorf("`source_labels` must contain at least a single entry for `action=drop_if_contains`")
|
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
|
|
|
return nil, fmt.Errorf("`regex` cannot be used for `action=drop_if_contains`")
|
|
|
|
}
|
2020-06-23 14:17:58 +00:00
|
|
|
case "keep_if_equal":
|
|
|
|
if len(sourceLabels) < 2 {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("`source_labels` must contain at least two entries for `action=keep_if_equal`; got %q", sourceLabels)
|
2020-06-23 14:17:58 +00:00
|
|
|
}
|
2022-12-22 03:55:57 +00:00
|
|
|
if targetLabel != "" {
|
|
|
|
return nil, fmt.Errorf("`target_label` cannot be used for `action=keep_if_equal`")
|
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
|
|
|
return nil, fmt.Errorf("`regex` cannot be used for `action=keep_if_equal`")
|
|
|
|
}
|
2020-06-23 14:17:58 +00:00
|
|
|
case "drop_if_equal":
|
|
|
|
if len(sourceLabels) < 2 {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("`source_labels` must contain at least two entries for `action=drop_if_equal`; got %q", sourceLabels)
|
2020-06-23 14:17:58 +00:00
|
|
|
}
|
2022-12-22 03:55:57 +00:00
|
|
|
if targetLabel != "" {
|
|
|
|
return nil, fmt.Errorf("`target_label` cannot be used for `action=drop_if_equal`")
|
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
|
|
|
return nil, fmt.Errorf("`regex` cannot be used for `action=drop_if_equal`")
|
|
|
|
}
|
|
|
|
case "keepequal":
|
|
|
|
if targetLabel == "" {
|
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=keepequal`")
|
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
|
|
|
return nil, fmt.Errorf("`regex` cannot be used for `action=keepequal`")
|
|
|
|
}
|
|
|
|
case "dropequal":
|
|
|
|
if targetLabel == "" {
|
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=dropequal`")
|
|
|
|
}
|
|
|
|
if rc.Regex != nil {
|
|
|
|
return nil, fmt.Errorf("`regex` cannot be used for `action=dropequal`")
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
case "keep":
|
2022-02-24 00:26:15 +00:00
|
|
|
if len(sourceLabels) == 0 && rc.If == nil {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=keep`")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
case "drop":
|
2022-02-24 00:26:15 +00:00
|
|
|
if len(sourceLabels) == 0 && rc.If == nil {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=drop`")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
case "hashmod":
|
|
|
|
if len(sourceLabels) == 0 {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("missing `source_labels` for `action=hashmod`")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
if targetLabel == "" {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("missing `target_label` for `action=hashmod`")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
if modulus < 1 {
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("unexpected `modulus` for `action=hashmod`: %d; must be greater than 0", modulus)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2021-09-09 13:18:19 +00:00
|
|
|
case "keep_metrics":
|
2022-05-06 21:02:54 +00:00
|
|
|
if (rc.Regex == nil || rc.Regex.S == "") && rc.If == nil {
|
2021-09-09 13:18:19 +00: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 21:02:54 +00:00
|
|
|
if (rc.Regex == nil || rc.Regex.S == "") && rc.If == nil {
|
2021-09-09 13:18:19 +00: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 08:02:37 +00: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 11:35:47 +00:00
|
|
|
case "labelmap":
|
|
|
|
case "labelmap_all":
|
|
|
|
case "labeldrop":
|
|
|
|
case "labelkeep":
|
|
|
|
default:
|
2021-02-22 14:33:55 +00:00
|
|
|
return nil, fmt.Errorf("unknown `action` %q", action)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-06-16 17:24:19 +00: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)
|
|
|
|
}
|
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
ruleOriginal, err := yaml.Marshal(rc)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: cannot marshal RelabelConfig: %s", err)
|
|
|
|
}
|
2022-09-30 07:43:31 +00:00
|
|
|
prc := &parsedRelabelConfig{
|
2022-12-10 10:09:21 +00:00
|
|
|
ruleOriginal: string(ruleOriginal),
|
|
|
|
|
2022-08-26 12:23:41 +00:00
|
|
|
SourceLabels: sourceLabels,
|
|
|
|
Separator: separator,
|
|
|
|
TargetLabel: targetLabel,
|
|
|
|
RegexAnchored: regexAnchored,
|
|
|
|
Modulus: modulus,
|
|
|
|
Replacement: replacement,
|
|
|
|
Action: action,
|
|
|
|
If: rc.If,
|
2020-06-18 23:20:29 +00:00
|
|
|
|
2022-06-16 17:24:19 +00:00
|
|
|
graphiteMatchTemplate: graphiteMatchTemplate,
|
|
|
|
graphiteLabelRules: graphiteLabelRules,
|
|
|
|
|
2022-08-26 12:23:41 +00:00
|
|
|
regex: promRegex,
|
2022-08-21 19:46:54 +00:00
|
|
|
regexOriginal: regexOriginalCompiled,
|
|
|
|
|
|
|
|
hasCaptureGroupInTargetLabel: strings.Contains(targetLabel, "$"),
|
|
|
|
hasCaptureGroupInReplacement: strings.Contains(replacement, "$"),
|
|
|
|
hasLabelReferenceInReplacement: strings.Contains(replacement, "{{"),
|
2022-09-30 07:43:31 +00:00
|
|
|
}
|
|
|
|
prc.stringReplacer = bytesutil.NewFastStringTransformer(prc.replaceFullStringSlow)
|
2022-09-30 09:25:02 +00:00
|
|
|
prc.submatchReplacer = bytesutil.NewFastStringTransformer(prc.replaceStringSubmatchesSlow)
|
2022-09-30 07:43:31 +00:00
|
|
|
return prc, nil
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-08-26 08:57:12 +00:00
|
|
|
|
|
|
|
func isDefaultRegex(expr string) bool {
|
2024-05-24 01:06:55 +00:00
|
|
|
prefix, suffix := regexutil.SimplifyPromRegex(expr)
|
2022-08-26 08:57:12 +00:00
|
|
|
if prefix != "" {
|
|
|
|
return false
|
|
|
|
}
|
2024-05-24 01:06:55 +00:00
|
|
|
return suffix == "(?s:.*)"
|
2022-08-26 08:57:12 +00:00
|
|
|
}
|