2020-02-23 11:35:47 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
2020-05-03 09:41:13 +00:00
|
|
|
"fmt"
|
2020-02-23 11:35:47 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2022-11-30 05:22:12 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2022-08-26 12:23:41 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/regexutil"
|
2022-06-21 17:23:30 +00:00
|
|
|
"github.com/cespare/xxhash/v2"
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
// parsedRelabelConfig contains parsed `relabel_config`.
|
2020-02-23 11:35:47 +00:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
|
2021-02-22 14:33:55 +00:00
|
|
|
type parsedRelabelConfig struct {
|
2022-12-10 10:09:21 +00:00
|
|
|
// ruleOriginal contains the original relabeling rule for the given prasedRelabelConfig.
|
|
|
|
ruleOriginal string
|
|
|
|
|
2022-08-26 12:23:41 +00:00
|
|
|
SourceLabels []string
|
|
|
|
Separator string
|
|
|
|
TargetLabel string
|
|
|
|
RegexAnchored *regexp.Regexp
|
|
|
|
Modulus uint64
|
|
|
|
Replacement string
|
|
|
|
Action string
|
|
|
|
If *IfExpression
|
2020-06-18 23:20:29 +00:00
|
|
|
|
2022-06-16 17:24:19 +00:00
|
|
|
graphiteMatchTemplate *graphiteMatchTemplate
|
|
|
|
graphiteLabelRules []graphiteLabelRule
|
|
|
|
|
2022-08-26 12:23:41 +00:00
|
|
|
regex *regexutil.PromRegex
|
2022-08-21 19:46:54 +00:00
|
|
|
regexOriginal *regexp.Regexp
|
|
|
|
|
|
|
|
hasCaptureGroupInTargetLabel bool
|
|
|
|
hasCaptureGroupInReplacement bool
|
|
|
|
hasLabelReferenceInReplacement bool
|
2022-09-30 07:43:31 +00:00
|
|
|
|
2022-09-30 09:28:20 +00:00
|
|
|
stringReplacer *bytesutil.FastStringTransformer
|
2022-09-30 09:25:02 +00:00
|
|
|
submatchReplacer *bytesutil.FastStringTransformer
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 10:09:21 +00:00
|
|
|
// DebugStep contains debug information about a single relabeling rule step
|
|
|
|
type DebugStep struct {
|
|
|
|
// Rule contains string representation of the rule step
|
|
|
|
Rule string
|
|
|
|
|
2023-03-21 05:07:52 +00:00
|
|
|
// In contains the input labels before the execution of the rule step
|
2022-12-10 10:09:21 +00:00
|
|
|
In string
|
|
|
|
|
|
|
|
// Out contains the output labels after the execution of the rule step
|
|
|
|
Out string
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns human-readable representation for ds
|
|
|
|
func (ds DebugStep) String() string {
|
|
|
|
return fmt.Sprintf("rule=%q, in=%s, out=%s", ds.Rule, ds.In, ds.Out)
|
|
|
|
}
|
|
|
|
|
2020-05-03 09:41:13 +00:00
|
|
|
// String returns human-readable representation for prc.
|
2021-02-22 14:33:55 +00:00
|
|
|
func (prc *parsedRelabelConfig) String() string {
|
2022-12-10 10:09:21 +00:00
|
|
|
return prc.ruleOriginal
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyDebug applies pcs to labels in debug mode.
|
|
|
|
//
|
|
|
|
// It returns DebugStep list - one entry per each applied relabeling step.
|
|
|
|
func (pcs *ParsedConfigs) ApplyDebug(labels []prompbmarshal.Label) ([]prompbmarshal.Label, []DebugStep) {
|
2023-11-29 08:03:04 +00:00
|
|
|
// Protect from overwriting labels between len(labels) and cap(labels) by limiting labels capacity to its length.
|
2024-02-14 12:27:05 +00:00
|
|
|
labels = labels[:len(labels):len(labels)]
|
|
|
|
|
|
|
|
inStr := LabelsToString(labels)
|
|
|
|
var dss []DebugStep
|
|
|
|
if pcs != nil {
|
|
|
|
for _, prc := range pcs.prcs {
|
|
|
|
labels = prc.apply(labels, 0)
|
|
|
|
outStr := LabelsToString(labels)
|
|
|
|
dss = append(dss, DebugStep{
|
|
|
|
Rule: prc.String(),
|
|
|
|
In: inStr,
|
|
|
|
Out: outStr,
|
|
|
|
})
|
|
|
|
inStr = outStr
|
|
|
|
if len(labels) == 0 {
|
|
|
|
// All the labels have been removed.
|
|
|
|
return labels, dss
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
labels = removeEmptyLabels(labels, 0)
|
|
|
|
outStr := LabelsToString(labels)
|
|
|
|
if outStr != inStr {
|
|
|
|
dss = append(dss, DebugStep{
|
|
|
|
Rule: "remove empty labels",
|
|
|
|
In: inStr,
|
|
|
|
Out: outStr,
|
|
|
|
})
|
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
return labels, dss
|
2020-05-03 09:41:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
// Apply applies pcs to labels starting from the labelsOffset.
|
2023-11-29 08:03:04 +00:00
|
|
|
//
|
2024-02-14 12:27:05 +00:00
|
|
|
// This function may add additional labels after the len(labels), so make sure it doesn't corrupt in-use labels
|
2023-11-29 08:03:04 +00:00
|
|
|
// stored between len(labels) and cap(labels).
|
2022-10-09 11:51:14 +00:00
|
|
|
func (pcs *ParsedConfigs) Apply(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label {
|
2021-02-22 14:33:55 +00:00
|
|
|
if pcs != nil {
|
|
|
|
for _, prc := range pcs.prcs {
|
2022-12-10 10:09:21 +00:00
|
|
|
labels = prc.apply(labels, labelsOffset)
|
|
|
|
if len(labels) == labelsOffset {
|
2021-02-22 14:33:55 +00:00
|
|
|
// All the labels have been removed.
|
2024-02-14 12:27:05 +00:00
|
|
|
return labels
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 13:41:33 +00:00
|
|
|
labels = removeEmptyLabels(labels, labelsOffset)
|
2024-02-14 12:27:05 +00:00
|
|
|
return labels
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-03 13:41:33 +00:00
|
|
|
// FinalizeLabels removes labels with "__" in the beginning (except of "__name__").
|
2020-02-23 11:35:47 +00:00
|
|
|
func FinalizeLabels(dst, src []prompbmarshal.Label) []prompbmarshal.Label {
|
2022-10-07 19:39:28 +00:00
|
|
|
for _, label := range src {
|
2020-02-23 11:35:47 +00:00
|
|
|
name := label.Name
|
2020-05-03 13:41:33 +00:00
|
|
|
if strings.HasPrefix(name, "__") && name != "__name__" {
|
2020-02-23 11:35:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-07 19:39:28 +00:00
|
|
|
dst = append(dst, label)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2021-02-22 14:33:55 +00:00
|
|
|
// apply applies relabeling according to prc.
|
2020-02-23 11:35:47 +00:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
|
2021-02-22 14:33:55 +00:00
|
|
|
func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label {
|
2020-02-23 11:35:47 +00:00
|
|
|
src := labels[labelsOffset:]
|
2023-08-11 13:37:48 +00:00
|
|
|
if !prc.If.Match(src) {
|
2022-02-24 00:26:15 +00:00
|
|
|
if prc.Action == "keep" {
|
|
|
|
// Drop the target on `if` mismatch for `action: keep`
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
|
|
|
// Do not apply prc actions on `if` mismatch.
|
|
|
|
return labels
|
|
|
|
}
|
2020-06-18 23:20:29 +00:00
|
|
|
switch prc.Action {
|
2022-06-16 17:24:19 +00:00
|
|
|
case "graphite":
|
2022-11-30 05:22:12 +00:00
|
|
|
metricName := getLabelValue(src, "__name__")
|
2022-06-16 17:24:19 +00:00
|
|
|
gm := graphiteMatchesPool.Get().(*graphiteMatches)
|
|
|
|
var ok bool
|
|
|
|
gm.a, ok = prc.graphiteMatchTemplate.Match(gm.a[:0], metricName)
|
|
|
|
if !ok {
|
|
|
|
// Fast path - name mismatch
|
|
|
|
graphiteMatchesPool.Put(gm)
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
// Slow path - extract labels from graphite metric name
|
|
|
|
bb := relabelBufPool.Get()
|
|
|
|
for _, gl := range prc.graphiteLabelRules {
|
|
|
|
bb.B = gl.grt.Expand(bb.B[:0], gm.a)
|
2023-01-04 06:14:20 +00:00
|
|
|
valueStr := bytesutil.InternBytes(bb.B)
|
2022-06-16 17:24:19 +00:00
|
|
|
labels = setLabelValue(labels, labelsOffset, gl.targetLabel, valueStr)
|
|
|
|
}
|
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
graphiteMatchesPool.Put(gm)
|
|
|
|
return labels
|
2020-02-23 11:35:47 +00:00
|
|
|
case "replace":
|
2022-02-24 00:26:15 +00:00
|
|
|
// Store `replacement` at `target_label` if the `regex` matches `source_labels` joined with `separator`
|
2022-08-21 19:46:54 +00:00
|
|
|
replacement := prc.Replacement
|
2020-02-23 11:35:47 +00:00
|
|
|
bb := relabelBufPool.Get()
|
2022-08-21 19:46:54 +00:00
|
|
|
if prc.hasLabelReferenceInReplacement {
|
|
|
|
// Fill {{labelName}} references in the replacement
|
|
|
|
bb.B = fillLabelReferences(bb.B[:0], replacement, labels[labelsOffset:])
|
2023-01-04 06:14:20 +00:00
|
|
|
replacement = bytesutil.InternBytes(bb.B)
|
2022-08-21 19:46:54 +00:00
|
|
|
}
|
2020-06-18 23:20:29 +00:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
2022-08-26 12:23:41 +00:00
|
|
|
if prc.RegexAnchored == defaultRegexForRelabelConfig && !prc.hasCaptureGroupInTargetLabel {
|
2022-08-21 19:46:54 +00:00
|
|
|
if replacement == "$1" {
|
2021-02-21 22:50:57 +00:00
|
|
|
// Fast path for the rule that copies source label values to destination:
|
|
|
|
// - source_labels: [...]
|
|
|
|
// target_label: foobar
|
2023-01-04 06:14:20 +00:00
|
|
|
valueStr := bytesutil.InternBytes(bb.B)
|
2021-02-21 22:50:57 +00:00
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
return setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
|
|
|
|
}
|
|
|
|
if !prc.hasCaptureGroupInReplacement {
|
|
|
|
// Fast path for the rule that sets label value:
|
|
|
|
// - target_label: foobar
|
|
|
|
// replacement: something-here
|
|
|
|
relabelBufPool.Put(bb)
|
2022-08-21 19:46:54 +00:00
|
|
|
labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, replacement)
|
2021-02-21 22:50:57 +00:00
|
|
|
return labels
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2022-09-30 07:48:22 +00:00
|
|
|
sourceStr := bytesutil.ToUnsafeString(bb.B)
|
|
|
|
if !prc.regex.MatchString(sourceStr) {
|
2022-08-26 12:35:16 +00:00
|
|
|
// Fast path - regexp mismatch.
|
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
return labels
|
|
|
|
}
|
2022-09-30 09:25:02 +00:00
|
|
|
var valueStr string
|
|
|
|
if replacement == prc.Replacement {
|
|
|
|
// Fast path - the replacement wasn't modified, so it is safe calling stringReplacer.Transform.
|
|
|
|
valueStr = prc.stringReplacer.Transform(sourceStr)
|
|
|
|
} else {
|
|
|
|
// Slow path - the replacement has been modified, so the valueStr must be calculated
|
|
|
|
// from scratch based on the new replacement value.
|
|
|
|
match := prc.RegexAnchored.FindSubmatchIndex(bb.B)
|
|
|
|
valueStr = prc.expandCaptureGroups(replacement, sourceStr, match)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
2020-06-18 23:20:29 +00:00
|
|
|
nameStr := prc.TargetLabel
|
|
|
|
if prc.hasCaptureGroupInTargetLabel {
|
2022-09-30 09:25:02 +00:00
|
|
|
// Slow path - target_label contains regex capture groups, so the target_label
|
|
|
|
// must be calculated from the regex match.
|
|
|
|
match := prc.RegexAnchored.FindSubmatchIndex(bb.B)
|
2020-06-18 23:20:29 +00:00
|
|
|
nameStr = prc.expandCaptureGroups(nameStr, sourceStr, match)
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
relabelBufPool.Put(bb)
|
2020-06-18 23:20:29 +00:00
|
|
|
return setLabelValue(labels, labelsOffset, nameStr, valueStr)
|
2020-02-23 11:35:47 +00:00
|
|
|
case "replace_all":
|
2022-06-01 08:02:37 +00:00
|
|
|
// Replace all the occurrences of `regex` at `source_labels` joined with `separator` with the `replacement`
|
2022-02-24 00:26:15 +00:00
|
|
|
// and store the result at `target_label`
|
2020-02-23 11:35:47 +00:00
|
|
|
bb := relabelBufPool.Get()
|
2020-06-18 23:20:29 +00:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
2023-01-04 06:14:20 +00:00
|
|
|
sourceStr := bytesutil.InternBytes(bb.B)
|
2020-02-23 11:35:47 +00:00
|
|
|
relabelBufPool.Put(bb)
|
2022-09-30 09:25:02 +00:00
|
|
|
valueStr := prc.replaceStringSubmatchesFast(sourceStr)
|
|
|
|
if valueStr != sourceStr {
|
2021-02-22 14:33:55 +00:00
|
|
|
labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
|
|
|
|
}
|
|
|
|
return labels
|
2023-11-29 09:00:48 +00:00
|
|
|
case "keep_if_contains":
|
|
|
|
// Keep the entry if target_label contains all the label values listed in source_labels.
|
|
|
|
// For example, the following relabeling rule would leave the entry if __meta_consul_tags
|
|
|
|
// contains values of __meta_required_tag1 and __meta_required_tag2:
|
|
|
|
//
|
|
|
|
// - action: keep_if_contains
|
|
|
|
// target_label: __meta_consul_tags
|
|
|
|
// source_labels: [__meta_required_tag1, __meta_required_tag2]
|
|
|
|
//
|
|
|
|
if containsAllLabelValues(src, prc.TargetLabel, prc.SourceLabels) {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
case "drop_if_contains":
|
|
|
|
// Drop the entry if target_label contains all the label values listed in source_labels.
|
|
|
|
// For example, the following relabeling rule would drop the entry if __meta_consul_tags
|
|
|
|
// contains values of __meta_required_tag1 and __meta_required_tag2:
|
|
|
|
//
|
|
|
|
// - action: drop_if_contains
|
|
|
|
// target_label: __meta_consul_tags
|
|
|
|
// source_labels: [__meta_required_tag1, __meta_required_tag2]
|
|
|
|
//
|
|
|
|
if containsAllLabelValues(src, prc.TargetLabel, prc.SourceLabels) {
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
|
|
|
return labels
|
2020-06-23 14:17:58 +00:00
|
|
|
case "keep_if_equal":
|
|
|
|
// Keep the entry if all the label values in source_labels are equal.
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// - source_labels: [foo, bar]
|
|
|
|
// action: keep_if_equal
|
|
|
|
//
|
|
|
|
// Would leave the entry if `foo` value equals `bar` value
|
|
|
|
if areEqualLabelValues(src, prc.SourceLabels) {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
case "drop_if_equal":
|
|
|
|
// Drop the entry if all the label values in source_labels are equal.
|
|
|
|
// For example:
|
|
|
|
//
|
|
|
|
// - source_labels: [foo, bar]
|
|
|
|
// action: drop_if_equal
|
|
|
|
//
|
|
|
|
// Would drop the entry if `foo` value equals `bar` value.
|
|
|
|
if areEqualLabelValues(src, prc.SourceLabels) {
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
|
|
|
return labels
|
2022-12-22 03:55:57 +00:00
|
|
|
case "keepequal":
|
|
|
|
// Keep the entry if `source_labels` joined with `separator` matches `target_label`
|
|
|
|
bb := relabelBufPool.Get()
|
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
targetValue := getLabelValue(labels[labelsOffset:], prc.TargetLabel)
|
|
|
|
keep := string(bb.B) == targetValue
|
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
if keep {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
case "dropequal":
|
|
|
|
// Drop the entry if `source_labels` joined with `separator` doesn't match `target_label`
|
|
|
|
bb := relabelBufPool.Get()
|
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
targetValue := getLabelValue(labels[labelsOffset:], prc.TargetLabel)
|
|
|
|
drop := string(bb.B) == targetValue
|
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
if !drop {
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
return labels[:labelsOffset]
|
2020-02-23 11:35:47 +00:00
|
|
|
case "keep":
|
2022-02-24 00:26:15 +00:00
|
|
|
// Keep the target if `source_labels` joined with `separator` match the `regex`.
|
2022-08-26 12:23:41 +00:00
|
|
|
if prc.RegexAnchored == defaultRegexForRelabelConfig {
|
2022-02-24 00:26:15 +00:00
|
|
|
// Fast path for the case with `if` and without explicitly set `regex`:
|
|
|
|
//
|
|
|
|
// - action: keep
|
|
|
|
// if: 'some{label=~"filters"}'
|
|
|
|
//
|
|
|
|
return labels
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
bb := relabelBufPool.Get()
|
2020-06-18 23:20:29 +00:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
2022-08-26 12:23:41 +00:00
|
|
|
keep := prc.regex.MatchString(bytesutil.ToUnsafeString(bb.B))
|
2020-02-23 11:35:47 +00:00
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
if !keep {
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "drop":
|
2022-02-24 00:26:15 +00:00
|
|
|
// Drop the target if `source_labels` joined with `separator` don't match the `regex`.
|
2022-08-26 12:23:41 +00:00
|
|
|
if prc.RegexAnchored == defaultRegexForRelabelConfig {
|
2022-02-24 00:26:15 +00:00
|
|
|
// Fast path for the case with `if` and without explicitly set `regex`:
|
|
|
|
//
|
|
|
|
// - action: drop
|
|
|
|
// if: 'some{label=~"filters"}'
|
|
|
|
//
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
bb := relabelBufPool.Get()
|
2020-06-18 23:20:29 +00:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
2022-08-26 12:23:41 +00:00
|
|
|
drop := prc.regex.MatchString(bytesutil.ToUnsafeString(bb.B))
|
2020-02-23 11:35:47 +00:00
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
if drop {
|
|
|
|
return labels[:labelsOffset]
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "hashmod":
|
2022-02-24 00:26:15 +00:00
|
|
|
// Calculate the `modulus` from the hash of `source_labels` joined with `separator` and store it at `target_label`
|
2020-02-23 11:35:47 +00:00
|
|
|
bb := relabelBufPool.Get()
|
2020-06-18 23:20:29 +00:00
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
|
|
|
h := xxhash.Sum64(bb.B) % prc.Modulus
|
2020-02-23 11:35:47 +00:00
|
|
|
value := strconv.Itoa(int(h))
|
|
|
|
relabelBufPool.Put(bb)
|
2020-06-18 23:20:29 +00:00
|
|
|
return setLabelValue(labels, labelsOffset, prc.TargetLabel, value)
|
2020-02-23 11:35:47 +00:00
|
|
|
case "labelmap":
|
2022-02-24 00:26:15 +00:00
|
|
|
// Replace label names with the `replacement` if they match `regex`
|
2022-08-26 18:47:20 +00:00
|
|
|
for _, label := range src {
|
2022-09-30 07:43:31 +00:00
|
|
|
labelName := prc.replaceFullStringFast(label.Name)
|
|
|
|
if labelName != label.Name {
|
2021-02-22 14:33:55 +00:00
|
|
|
labels = setLabelValue(labels, labelsOffset, labelName, label.Value)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "labelmap_all":
|
2022-07-18 09:08:15 +00:00
|
|
|
// Replace all the occurrences of `regex` at label names with `replacement`
|
2020-02-23 11:35:47 +00:00
|
|
|
for i := range src {
|
|
|
|
label := &src[i]
|
2022-09-30 09:25:02 +00:00
|
|
|
label.Name = prc.replaceStringSubmatchesFast(label.Name)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
return labels
|
|
|
|
case "labeldrop":
|
2022-02-24 00:26:15 +00:00
|
|
|
// Drop labels with names matching the `regex`
|
2020-02-23 11:35:47 +00:00
|
|
|
dst := labels[:labelsOffset]
|
2022-08-26 18:47:20 +00:00
|
|
|
re := prc.regex
|
|
|
|
for _, label := range src {
|
|
|
|
if !re.MatchString(label.Name) {
|
|
|
|
dst = append(dst, label)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
case "labelkeep":
|
2022-02-24 00:26:15 +00:00
|
|
|
// Keep labels with names matching the `regex`
|
2020-02-23 11:35:47 +00:00
|
|
|
dst := labels[:labelsOffset]
|
2022-08-26 18:47:20 +00:00
|
|
|
re := prc.regex
|
|
|
|
for _, label := range src {
|
|
|
|
if re.MatchString(label.Name) {
|
|
|
|
dst = append(dst, label)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
2022-06-01 08:02:37 +00:00
|
|
|
case "uppercase":
|
|
|
|
bb := relabelBufPool.Get()
|
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
2023-01-04 06:14:20 +00:00
|
|
|
valueStr := bytesutil.InternBytes(bb.B)
|
2022-06-01 08:02:37 +00:00
|
|
|
relabelBufPool.Put(bb)
|
2022-06-01 11:51:26 +00:00
|
|
|
valueStr = strings.ToUpper(valueStr)
|
|
|
|
labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
|
2022-06-01 08:02:37 +00:00
|
|
|
return labels
|
|
|
|
case "lowercase":
|
|
|
|
bb := relabelBufPool.Get()
|
|
|
|
bb.B = concatLabelValues(bb.B[:0], src, prc.SourceLabels, prc.Separator)
|
2023-01-04 06:14:20 +00:00
|
|
|
valueStr := bytesutil.InternBytes(bb.B)
|
2022-06-01 08:02:37 +00:00
|
|
|
relabelBufPool.Put(bb)
|
2022-06-01 11:51:26 +00:00
|
|
|
valueStr = strings.ToLower(valueStr)
|
|
|
|
labels = setLabelValue(labels, labelsOffset, prc.TargetLabel, valueStr)
|
2022-06-01 08:02:37 +00:00
|
|
|
return labels
|
2020-02-23 11:35:47 +00:00
|
|
|
default:
|
2020-06-18 23:20:29 +00:00
|
|
|
logger.Panicf("BUG: unknown `action`: %q", prc.Action)
|
2020-02-23 11:35:47 +00:00
|
|
|
return labels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 07:43:31 +00:00
|
|
|
// replaceFullStringFast replaces s with the replacement if s matches '^regex$'.
|
|
|
|
//
|
|
|
|
// s is returned as is if it doesn't match '^regex$'.
|
|
|
|
func (prc *parsedRelabelConfig) replaceFullStringFast(s string) string {
|
2021-02-22 14:33:55 +00:00
|
|
|
prefix, complete := prc.regexOriginal.LiteralPrefix()
|
2022-09-30 07:43:31 +00:00
|
|
|
replacement := prc.Replacement
|
|
|
|
if complete && !prc.hasCaptureGroupInReplacement {
|
2021-02-22 14:33:55 +00:00
|
|
|
if s == prefix {
|
2022-09-30 07:43:31 +00:00
|
|
|
// Fast path - s matches literal regex
|
|
|
|
return replacement
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
2022-09-30 07:43:31 +00:00
|
|
|
// Fast path - s doesn't match literal regex
|
|
|
|
return s
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
|
|
|
if !strings.HasPrefix(s, prefix) {
|
2022-10-01 08:50:21 +00:00
|
|
|
// Fast path - s doesn't match literal prefix from regex
|
2022-09-30 07:43:31 +00:00
|
|
|
return s
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
|
|
|
if replacement == "$1" {
|
|
|
|
// Fast path for commonly used rule for deleting label prefixes such as:
|
|
|
|
//
|
|
|
|
// - action: labelmap
|
|
|
|
// regex: __meta_kubernetes_node_label_(.+)
|
|
|
|
//
|
|
|
|
reStr := prc.regexOriginal.String()
|
|
|
|
if strings.HasPrefix(reStr, prefix) {
|
|
|
|
suffix := s[len(prefix):]
|
|
|
|
reSuffix := reStr[len(prefix):]
|
|
|
|
switch reSuffix {
|
|
|
|
case "(.*)":
|
2022-09-30 07:43:31 +00:00
|
|
|
return suffix
|
2021-02-22 14:33:55 +00:00
|
|
|
case "(.+)":
|
|
|
|
if len(suffix) > 0 {
|
2022-09-30 07:43:31 +00:00
|
|
|
return suffix
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
2022-09-30 07:43:31 +00:00
|
|
|
return s
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 07:48:22 +00:00
|
|
|
if !prc.regex.MatchString(s) {
|
|
|
|
// Fast path - regex mismatch
|
|
|
|
return s
|
|
|
|
}
|
2022-09-30 07:43:31 +00:00
|
|
|
// Slow path - handle the rest of cases.
|
|
|
|
return prc.stringReplacer.Transform(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// replaceFullStringSlow replaces s with the replacement if s matches '^regex$'.
|
|
|
|
//
|
|
|
|
// s is returned as is if it doesn't match '^regex$'.
|
|
|
|
func (prc *parsedRelabelConfig) replaceFullStringSlow(s string) string {
|
2021-02-22 14:33:55 +00:00
|
|
|
// Slow path - regexp processing
|
2022-08-26 12:23:41 +00:00
|
|
|
match := prc.RegexAnchored.FindStringSubmatchIndex(s)
|
2021-02-22 14:33:55 +00:00
|
|
|
if match == nil {
|
2022-09-30 07:43:31 +00:00
|
|
|
return s
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
2022-09-30 07:43:31 +00:00
|
|
|
return prc.expandCaptureGroups(prc.Replacement, s, match)
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-30 09:25:02 +00:00
|
|
|
// replaceStringSubmatchesFast replaces all the regex matches with the replacement in s.
|
|
|
|
func (prc *parsedRelabelConfig) replaceStringSubmatchesFast(s string) string {
|
|
|
|
prefix, complete := prc.regexOriginal.LiteralPrefix()
|
|
|
|
if complete && !prc.hasCaptureGroupInReplacement && !strings.Contains(s, prefix) {
|
|
|
|
// Fast path - zero regex matches in s.
|
|
|
|
return s
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
2022-09-30 09:25:02 +00:00
|
|
|
// Slow path - replace all the regex matches in s with the replacement.
|
|
|
|
return prc.submatchReplacer.Transform(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// replaceStringSubmatchesSlow replaces all the regex matches with the replacement in s.
|
|
|
|
func (prc *parsedRelabelConfig) replaceStringSubmatchesSlow(s string) string {
|
|
|
|
return prc.regexOriginal.ReplaceAllString(s, prc.Replacement)
|
2021-02-22 14:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (prc *parsedRelabelConfig) expandCaptureGroups(template, source string, match []int) string {
|
2020-06-18 23:20:29 +00:00
|
|
|
bb := relabelBufPool.Get()
|
2022-08-26 12:23:41 +00:00
|
|
|
bb.B = prc.RegexAnchored.ExpandString(bb.B[:0], template, source, match)
|
2023-01-04 06:14:20 +00:00
|
|
|
s := bytesutil.InternBytes(bb.B)
|
2020-06-18 23:20:29 +00:00
|
|
|
relabelBufPool.Put(bb)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
var relabelBufPool bytesutil.ByteBufferPool
|
|
|
|
|
2023-11-29 09:00:48 +00:00
|
|
|
func containsAllLabelValues(labels []prompbmarshal.Label, targetLabel string, sourceLabels []string) bool {
|
|
|
|
targetLabelValue := getLabelValue(labels, targetLabel)
|
|
|
|
for _, sourceLabel := range sourceLabels {
|
|
|
|
v := getLabelValue(labels, sourceLabel)
|
|
|
|
if !strings.Contains(targetLabelValue, v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:17:58 +00:00
|
|
|
func areEqualLabelValues(labels []prompbmarshal.Label, labelNames []string) bool {
|
|
|
|
if len(labelNames) < 2 {
|
|
|
|
logger.Panicf("BUG: expecting at least 2 labelNames; got %d", len(labelNames))
|
|
|
|
return false
|
|
|
|
}
|
2022-11-30 05:22:12 +00:00
|
|
|
labelValue := getLabelValue(labels, labelNames[0])
|
2020-06-23 14:17:58 +00:00
|
|
|
for _, labelName := range labelNames[1:] {
|
2022-11-30 05:22:12 +00:00
|
|
|
v := getLabelValue(labels, labelName)
|
2020-06-23 14:17:58 +00:00
|
|
|
if v != labelValue {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
func concatLabelValues(dst []byte, labels []prompbmarshal.Label, labelNames []string, separator string) []byte {
|
|
|
|
if len(labelNames) == 0 {
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
for _, labelName := range labelNames {
|
2024-02-14 12:27:05 +00:00
|
|
|
labelValue := getLabelValue(labels, labelName)
|
|
|
|
dst = append(dst, labelValue...)
|
2020-02-23 11:35:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
func getLabelValue(labels []prompbmarshal.Label, name string) string {
|
|
|
|
for _, label := range labels {
|
|
|
|
if label.Name == name {
|
|
|
|
return label.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-02-23 11:35:47 +00:00
|
|
|
// 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 11:11:54 +00:00
|
|
|
|
2020-11-07 14:16:56 +00:00
|
|
|
// CleanLabels sets label.Name and label.Value to an empty string for all the labels.
|
|
|
|
//
|
|
|
|
// This should help GC cleaning up label.Name and label.Value strings.
|
|
|
|
func CleanLabels(labels []prompbmarshal.Label) {
|
2024-03-01 19:26:50 +00:00
|
|
|
clear(labels)
|
2020-11-07 14:16:56 +00:00
|
|
|
}
|
2021-06-04 17:27:55 +00:00
|
|
|
|
2022-12-10 10:09:21 +00:00
|
|
|
// LabelsToString returns Prometheus string representation for the given labels.
|
|
|
|
//
|
|
|
|
// Labels in the returned string are sorted by name,
|
|
|
|
// while the __name__ label is put in front of {} labels.
|
|
|
|
func LabelsToString(labels []prompbmarshal.Label) string {
|
2021-06-04 17:27:55 +00:00
|
|
|
labelsCopy := append([]prompbmarshal.Label{}, labels...)
|
|
|
|
SortLabels(labelsCopy)
|
|
|
|
mname := ""
|
2022-12-10 10:09:21 +00:00
|
|
|
for i, label := range labelsCopy {
|
2021-06-04 17:27:55 +00:00
|
|
|
if label.Name == "__name__" {
|
|
|
|
mname = label.Value
|
2022-12-10 10:09:21 +00:00
|
|
|
labelsCopy = append(labelsCopy[:i], labelsCopy[i+1:]...)
|
2021-06-04 17:27:55 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-12-10 10:09:21 +00:00
|
|
|
if mname != "" && len(labelsCopy) == 0 {
|
2021-06-04 17:27:55 +00:00
|
|
|
return mname
|
|
|
|
}
|
|
|
|
b := []byte(mname)
|
|
|
|
b = append(b, '{')
|
|
|
|
for i, label := range labelsCopy {
|
|
|
|
b = append(b, label.Name...)
|
|
|
|
b = append(b, '=')
|
|
|
|
b = strconv.AppendQuote(b, label.Value)
|
|
|
|
if i+1 < len(labelsCopy) {
|
|
|
|
b = append(b, ',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = append(b, '}')
|
|
|
|
return string(b)
|
|
|
|
}
|
2022-08-21 19:46:54 +00:00
|
|
|
|
2022-11-30 05:22:12 +00:00
|
|
|
// SortLabels sorts labels in alphabetical order.
|
|
|
|
func SortLabels(labels []prompbmarshal.Label) {
|
2024-02-14 12:27:05 +00:00
|
|
|
x := promutils.GetLabels()
|
2024-02-14 14:05:29 +00:00
|
|
|
labelsOrig := x.Labels
|
2024-02-14 12:27:05 +00:00
|
|
|
x.Labels = labels
|
2022-11-30 05:22:12 +00:00
|
|
|
x.Sort()
|
2024-02-14 14:05:29 +00:00
|
|
|
x.Labels = labelsOrig
|
2024-02-14 12:27:05 +00:00
|
|
|
promutils.PutLabels(x)
|
2022-11-30 05:22:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-21 19:46:54 +00:00
|
|
|
func fillLabelReferences(dst []byte, replacement string, labels []prompbmarshal.Label) []byte {
|
|
|
|
s := replacement
|
|
|
|
for len(s) > 0 {
|
|
|
|
n := strings.Index(s, "{{")
|
|
|
|
if n < 0 {
|
|
|
|
return append(dst, s...)
|
|
|
|
}
|
|
|
|
dst = append(dst, s[:n]...)
|
|
|
|
s = s[n+2:]
|
|
|
|
n = strings.Index(s, "}}")
|
|
|
|
if n < 0 {
|
|
|
|
dst = append(dst, "{{"...)
|
|
|
|
return append(dst, s...)
|
|
|
|
}
|
|
|
|
labelName := s[:n]
|
|
|
|
s = s[n+2:]
|
2022-11-30 05:22:12 +00:00
|
|
|
labelValue := getLabelValue(labels, labelName)
|
2022-08-21 19:46:54 +00:00
|
|
|
dst = append(dst, labelValue...)
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
2022-09-28 06:59:36 +00:00
|
|
|
|
2023-08-14 14:14:40 +00:00
|
|
|
// SanitizeLabelName replaces unsupported by Prometheus chars in label names with _.
|
2022-09-28 06:59:36 +00:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
|
2023-08-14 14:14:40 +00:00
|
|
|
func SanitizeLabelName(name string) string {
|
|
|
|
return labelNameSanitizer.Transform(name)
|
2022-09-28 06:59:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 23:18:33 +00:00
|
|
|
// SplitMetricNameToTokens returns tokens generated from metric name divided by unsupported Prometheus characters
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
|
|
|
|
func SplitMetricNameToTokens(name string) []string {
|
|
|
|
return nonAlphaNumChars.Split(name, -1)
|
2024-03-29 12:51:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 23:18:33 +00:00
|
|
|
var nonAlphaNumChars = regexp.MustCompile(`[^a-zA-Z0-9]`)
|
|
|
|
|
2023-08-14 14:14:40 +00:00
|
|
|
var labelNameSanitizer = bytesutil.NewFastStringTransformer(func(s string) string {
|
2024-07-17 10:18:38 +00:00
|
|
|
return unsupportedLabelNameChars.ReplaceAllLiteralString(s, "_")
|
2022-09-28 07:39:01 +00:00
|
|
|
})
|
2022-09-28 06:59:36 +00:00
|
|
|
|
2023-08-14 14:14:40 +00:00
|
|
|
var unsupportedLabelNameChars = regexp.MustCompile(`[^a-zA-Z0-9_]`)
|
|
|
|
|
|
|
|
// SanitizeMetricName replaces unsupported by Prometheus chars in metric names with _.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
|
|
|
|
func SanitizeMetricName(value string) string {
|
|
|
|
return metricNameSanitizer.Transform(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var metricNameSanitizer = bytesutil.NewFastStringTransformer(func(s string) string {
|
2024-07-17 10:18:38 +00:00
|
|
|
return unsupportedMetricNameChars.ReplaceAllLiteralString(s, "_")
|
2023-08-14 14:14:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var unsupportedMetricNameChars = regexp.MustCompile(`[^a-zA-Z0-9_:]`)
|