2020-02-23 12:35:47 +01:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
2020-05-03 11:41:13 +02:00
|
|
|
"fmt"
|
2020-02-23 12:35:47 +01:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
xxhash "github.com/cespare/xxhash/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParsedRelabelConfig contains parsed `relabel_config`.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
|
|
|
|
type ParsedRelabelConfig struct {
|
|
|
|
SourceLabels []string
|
|
|
|
Separator string
|
|
|
|
TargetLabel string
|
|
|
|
Regex *regexp.Regexp
|
|
|
|
Modulus uint64
|
|
|
|
Replacement string
|
|
|
|
Action string
|
2020-06-19 01:20:29 +02:00
|
|
|
|
|
|
|
hasCaptureGroupInTargetLabel bool
|
|
|
|
hasCaptureGroupInReplacement bool
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
|
2020-05-03 11:41:13 +02:00
|
|
|
// String returns human-readable representation for prc.
|
|
|
|
func (prc *ParsedRelabelConfig) String() string {
|
|
|
|
return fmt.Sprintf("SourceLabels=%s, Separator=%s, TargetLabel=%s, Regex=%s, Modulus=%d, Replacement=%s, Action=%s",
|
|
|
|
prc.SourceLabels, prc.Separator, prc.TargetLabel, prc.Regex.String(), prc.Modulus, prc.Replacement, prc.Action)
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:35:47 +01:00
|
|
|
// ApplyRelabelConfigs applies prcs to labels starting from the labelsOffset.
|
|
|
|
//
|
|
|
|
// If isFinalize is set, then FinalizeLabels is called on the labels[labelsOffset:].
|
|
|
|
//
|
|
|
|
// The returned labels at labels[labelsOffset:] are sorted.
|
|
|
|
func ApplyRelabelConfigs(labels []prompbmarshal.Label, labelsOffset int, prcs []ParsedRelabelConfig, isFinalize bool) []prompbmarshal.Label {
|
|
|
|
for i := range prcs {
|
|
|
|
tmp := applyRelabelConfig(labels, labelsOffset, &prcs[i])
|
|
|
|
if len(tmp) == labelsOffset {
|
|
|
|
// All the labels have been removed.
|
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
labels = tmp
|
|
|
|
}
|
2020-05-03 15:41:33 +02:00
|
|
|
labels = removeEmptyLabels(labels, labelsOffset)
|
2020-02-23 12:35:47 +01:00
|
|
|
if isFinalize {
|
|
|
|
labels = FinalizeLabels(labels[:labelsOffset], labels[labelsOffset:])
|
|
|
|
}
|
|
|
|
SortLabels(labels[labelsOffset:])
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeEmptyLabels(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label {
|
|
|
|
src := labels[labelsOffset:]
|
|
|
|
needsRemoval := false
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
|
|
|
if label.Name == "" || label.Value == "" {
|
|
|
|
needsRemoval = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !needsRemoval {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
dst := labels[:labelsOffset]
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
|
|
|
if label.Name != "" && label.Value != "" {
|
|
|
|
dst = append(dst, *label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2020-04-14 11:21:10 +02:00
|
|
|
// RemoveMetaLabels removes all the `__meta_` labels from src and puts the rest of labels to dst.
|
|
|
|
//
|
|
|
|
// See https://www.robustperception.io/life-of-a-label fo details.
|
|
|
|
func RemoveMetaLabels(dst, src []prompbmarshal.Label) []prompbmarshal.Label {
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
|
|
|
if strings.HasPrefix(label.Name, "__meta_") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dst = append(dst, *label)
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:41:33 +02:00
|
|
|
// FinalizeLabels removes labels with "__" in the beginning (except of "__name__").
|
2020-02-23 12:35:47 +01:00
|
|
|
func FinalizeLabels(dst, src []prompbmarshal.Label) []prompbmarshal.Label {
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
|
|
|
name := label.Name
|
2020-05-03 15:41:33 +02:00
|
|
|
if strings.HasPrefix(name, "__") && name != "__name__" {
|
2020-02-23 12:35:47 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-05-03 15:41:33 +02:00
|
|
|
dst = append(dst, *label)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:20:29 +02:00
|
|
|
// applyRelabelConfig applies relabeling according to prc.
|
2020-02-23 12:35:47 +01:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
|
2020-06-19 01:20:29 +02:00
|
|
|
func applyRelabelConfig(labels []prompbmarshal.Label, labelsOffset int, prc *ParsedRelabelConfig) []prompbmarshal.Label {
|
2020-02-23 12:35:47 +01:00
|
|
|
src := labels[labelsOffset:]
|
2020-06-19 01:20:29 +02:00
|
|
|
switch prc.Action {
|
2020-02-23 12:35:47 +01:00
|
|
|
case "replace":
|
|
|
|
bb := relabelBufPool.Get()
|
2020-06-19 01:20:29 +02:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
if len(bb.B) == 0 && prc.Regex == defaultRegexForRelabelConfig && !prc.hasCaptureGroupInReplacement && !prc.hasCaptureGroupInTargetLabel {
|
2020-02-23 12:35:47 +01:00
|
|
|
// Fast path for the following rule that just sets label value:
|
|
|
|
// - target_label: foobar
|
|
|
|
// replacement: something-here
|
|
|
|
relabelBufPool.Put(bb)
|
2020-06-19 01:20:29 +02:00
|
|
|
return setLabelValue(labels, labelsOffset, prc.TargetLabel, prc.Replacement)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
2020-06-19 01:20:29 +02:00
|
|
|
match := prc.Regex.FindSubmatchIndex(bb.B)
|
2020-02-23 12:35:47 +01:00
|
|
|
if match == nil {
|
|
|
|
// Fast path - nothing to replace.
|
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
sourceStr := bytesutil.ToUnsafeString(bb.B)
|
2020-06-19 01:20:29 +02:00
|
|
|
nameStr := prc.TargetLabel
|
|
|
|
if prc.hasCaptureGroupInTargetLabel {
|
|
|
|
nameStr = prc.expandCaptureGroups(nameStr, sourceStr, match)
|
|
|
|
}
|
|
|
|
valueStr := prc.expandCaptureGroups(prc.Replacement, sourceStr, match)
|
2020-02-23 12:35:47 +01:00
|
|
|
relabelBufPool.Put(bb)
|
2020-06-19 01:20:29 +02:00
|
|
|
return setLabelValue(labels, labelsOffset, nameStr, valueStr)
|
2020-02-23 12:35:47 +01:00
|
|
|
case "replace_all":
|
|
|
|
bb := relabelBufPool.Get()
|
2020-06-19 01:20:29 +02:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
if !prc.Regex.Match(bb.B) {
|
2020-02-23 12:35:47 +01:00
|
|
|
// Fast path - nothing to replace.
|
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
sourceStr := string(bb.B) // Make a copy of bb, since it can be returned from ReplaceAllString
|
|
|
|
relabelBufPool.Put(bb)
|
2020-06-19 01:20:29 +02:00
|
|
|
valueStr := prc.Regex.ReplaceAllString(sourceStr, prc.Replacement)
|
|
|
|
return setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
|
2020-02-23 12:35:47 +01:00
|
|
|
case "keep":
|
|
|
|
bb := relabelBufPool.Get()
|
2020-06-19 01:20:29 +02:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
keep := prc.Regex.Match(bb.B)
|
2020-02-23 12:35:47 +01:00
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
if !keep {
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "drop":
|
|
|
|
bb := relabelBufPool.Get()
|
2020-06-19 01:20:29 +02:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
drop := prc.Regex.Match(bb.B)
|
2020-02-23 12:35:47 +01:00
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
if drop {
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "hashmod":
|
|
|
|
bb := relabelBufPool.Get()
|
2020-06-19 01:20:29 +02:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
h := xxhash.Sum64(bb.B) % prc.Modulus
|
2020-02-23 12:35:47 +01:00
|
|
|
value := strconv.Itoa(int(h))
|
|
|
|
relabelBufPool.Put(bb)
|
2020-06-19 01:20:29 +02:00
|
|
|
return setLabelValue(labels, labelsOffset, prc.TargetLabel, value)
|
2020-02-23 12:35:47 +01:00
|
|
|
case "labelmap":
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
2020-06-19 01:20:29 +02:00
|
|
|
match := prc.Regex.FindStringSubmatchIndex(label.Name)
|
2020-02-23 12:35:47 +01:00
|
|
|
if match == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
value := relabelBufPool.Get()
|
2020-06-19 01:20:29 +02:00
|
|
|
value.B = prc.Regex.ExpandString(value.B[:0], prc.Replacement, label.Name, match)
|
2020-02-23 12:35:47 +01:00
|
|
|
label.Name = string(value.B)
|
|
|
|
relabelBufPool.Put(value)
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "labelmap_all":
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
2020-06-19 01:20:29 +02:00
|
|
|
if !prc.Regex.MatchString(label.Name) {
|
2020-02-23 12:35:47 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-06-19 01:20:29 +02:00
|
|
|
label.Name = prc.Regex.ReplaceAllString(label.Name, prc.Replacement)
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "labeldrop":
|
|
|
|
keepSrc := true
|
|
|
|
for i := range src {
|
2020-06-19 01:20:29 +02:00
|
|
|
if prc.Regex.MatchString(src[i].Name) {
|
2020-02-23 12:35:47 +01:00
|
|
|
keepSrc = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if keepSrc {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
dst := labels[:labelsOffset]
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
2020-06-19 01:20:29 +02:00
|
|
|
if !prc.Regex.MatchString(label.Name) {
|
2020-02-23 12:35:47 +01:00
|
|
|
dst = append(dst, *label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
case "labelkeep":
|
|
|
|
keepSrc := true
|
|
|
|
for i := range src {
|
2020-06-19 01:20:29 +02:00
|
|
|
if !prc.Regex.MatchString(src[i].Name) {
|
2020-02-23 12:35:47 +01:00
|
|
|
keepSrc = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if keepSrc {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
dst := labels[:labelsOffset]
|
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
2020-06-19 01:20:29 +02:00
|
|
|
if prc.Regex.MatchString(label.Name) {
|
2020-02-23 12:35:47 +01:00
|
|
|
dst = append(dst, *label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
default:
|
2020-06-19 01:20:29 +02:00
|
|
|
logger.Panicf("BUG: unknown `action`: %q", prc.Action)
|
2020-02-23 12:35:47 +01:00
|
|
|
return labels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 01:20:29 +02:00
|
|
|
func (prc *ParsedRelabelConfig) expandCaptureGroups(template, source string, match []int) string {
|
|
|
|
bb := relabelBufPool.Get()
|
|
|
|
bb.B = prc.Regex.ExpandString(bb.B[:0], template, source, match)
|
|
|
|
s := string(bb.B)
|
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:35:47 +01:00
|
|
|
var relabelBufPool bytesutil.ByteBufferPool
|
|
|
|
|
|
|
|
func concatLabelValues(dst []byte, labels []prompbmarshal.Label, labelNames []string, separator string) []byte {
|
|
|
|
if len(labelNames) == 0 {
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
for _, labelName := range labelNames {
|
|
|
|
label := GetLabelByName(labels, labelName)
|
|
|
|
if label != nil {
|
|
|
|
dst = append(dst, label.Value...)
|
|
|
|
}
|
|
|
|
dst = append(dst, separator...)
|
|
|
|
}
|
|
|
|
return dst[:len(dst)-len(separator)]
|
|
|
|
}
|
|
|
|
|
|
|
|
func setLabelValue(labels []prompbmarshal.Label, labelsOffset int, name, value string) []prompbmarshal.Label {
|
|
|
|
if label := GetLabelByName(labels[labelsOffset:], name); label != nil {
|
|
|
|
label.Value = value
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: name,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabelByName returns label with the given name from labels.
|
|
|
|
func GetLabelByName(labels []prompbmarshal.Label, name string) *prompbmarshal.Label {
|
|
|
|
for i := range labels {
|
|
|
|
label := &labels[i]
|
|
|
|
if label.Name == name {
|
|
|
|
return label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-14 13:11:54 +02:00
|
|
|
|
|
|
|
// GetLabelValueByName returns value for label with the given name from labels.
|
|
|
|
//
|
|
|
|
// It returns empty string for non-existing label.
|
|
|
|
func GetLabelValueByName(labels []prompbmarshal.Label, name string) string {
|
|
|
|
label := GetLabelByName(labels, name)
|
|
|
|
if label == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return label.Value
|
|
|
|
}
|