2020-02-23 11:35:47 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
2020-06-18 23:20:29 +00:00
|
|
|
"strings"
|
2020-02-23 11:35:47 +00:00
|
|
|
|
2020-08-13 13:43:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envtemplate"
|
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 {
|
2020-11-13 14:17:03 +00:00
|
|
|
SourceLabels []string `yaml:"source_labels,flow,omitempty"`
|
|
|
|
Separator *string `yaml:"separator,omitempty"`
|
|
|
|
TargetLabel string `yaml:"target_label,omitempty"`
|
|
|
|
Regex *string `yaml:"regex,omitempty"`
|
|
|
|
Modulus uint64 `yaml:"modulus,omitempty"`
|
|
|
|
Replacement *string `yaml:"replacement,omitempty"`
|
|
|
|
Action string `yaml:"action,omitempty"`
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
// ParsedConfigs represents parsed relabel configs.
|
|
|
|
type ParsedConfigs struct {
|
2021-06-04 17:27:55 +00:00
|
|
|
prcs []*parsedRelabelConfig
|
|
|
|
relabelDebug bool
|
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 ""
|
|
|
|
}
|
|
|
|
var sb strings.Builder
|
|
|
|
for _, prc := range pcs.prcs {
|
2021-06-04 17:27:55 +00:00
|
|
|
fmt.Fprintf(&sb, "%s,", prc.String())
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
2021-06-04 17:27:55 +00:00
|
|
|
fmt.Fprintf(&sb, "relabelDebug=%v", pcs.relabelDebug)
|
2021-02-22 14:33:55 +00:00
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
// LoadRelabelConfigs loads relabel configs from the given path.
|
2021-06-04 17:27:55 +00:00
|
|
|
func LoadRelabelConfigs(path string, relabelDebug bool) (*ParsedConfigs, error) {
|
2020-02-23 11:35:47 +00:00
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
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
|
|
|
}
|
2020-08-13 13:43:55 +00:00
|
|
|
data = envtemplate.Replace(data)
|
2021-06-04 17:27:55 +00:00
|
|
|
pcs, err := ParseRelabelConfigsData(data, relabelDebug)
|
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.
|
2021-06-04 17:27:55 +00:00
|
|
|
func ParseRelabelConfigsData(data []byte, relabelDebug bool) (*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
|
|
|
}
|
2021-06-04 17:27:55 +00:00
|
|
|
return ParseRelabelConfigs(rcs, relabelDebug)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRelabelConfigs parses rcs to dst.
|
2021-06-04 17:27:55 +00:00
|
|
|
func ParseRelabelConfigs(rcs []RelabelConfig, relabelDebug bool) (*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{
|
2021-06-04 17:27:55 +00:00
|
|
|
prcs: prcs,
|
|
|
|
relabelDebug: relabelDebug,
|
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("^(.*)$")
|
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
targetLabel := rc.TargetLabel
|
|
|
|
regexCompiled := defaultRegexForRelabelConfig
|
2021-02-22 14:33:55 +00:00
|
|
|
regexOriginalCompiled := defaultOriginalRegexForRelabelConfig
|
2020-02-23 11:35:47 +00:00
|
|
|
if rc.Regex != nil {
|
|
|
|
regex := *rc.Regex
|
|
|
|
if rc.Action != "replace_all" && rc.Action != "labelmap_all" {
|
2020-06-25 14:18:55 +00:00
|
|
|
regex = "^(?:" + *rc.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
|
|
|
}
|
|
|
|
regexCompiled = re
|
2021-02-22 14:33:55 +00:00
|
|
|
reOriginal, err := regexp.Compile(*rc.Regex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse `regex` %q: %w", *rc.Regex, err)
|
|
|
|
}
|
|
|
|
regexOriginalCompiled = reOriginal
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
modulus := rc.Modulus
|
|
|
|
replacement := "$1"
|
|
|
|
if rc.Replacement != nil {
|
|
|
|
replacement = *rc.Replacement
|
|
|
|
}
|
|
|
|
action := rc.Action
|
|
|
|
if action == "" {
|
|
|
|
action = "replace"
|
|
|
|
}
|
|
|
|
switch action {
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
case "keep":
|
|
|
|
if len(sourceLabels) == 0 {
|
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":
|
|
|
|
if len(sourceLabels) == 0 {
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2021-02-22 14:33:55 +00:00
|
|
|
return &parsedRelabelConfig{
|
2020-02-23 11:35:47 +00:00
|
|
|
SourceLabels: sourceLabels,
|
|
|
|
Separator: separator,
|
|
|
|
TargetLabel: targetLabel,
|
|
|
|
Regex: regexCompiled,
|
|
|
|
Modulus: modulus,
|
|
|
|
Replacement: replacement,
|
|
|
|
Action: action,
|
2020-06-18 23:20:29 +00:00
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
regexOriginal: regexOriginalCompiled,
|
2020-06-18 23:20:29 +00:00
|
|
|
hasCaptureGroupInTargetLabel: strings.Contains(targetLabel, "$"),
|
|
|
|
hasCaptureGroupInReplacement: strings.Contains(replacement, "$"),
|
2021-02-22 14:33:55 +00:00
|
|
|
}, nil
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|