mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-15 16:30:55 +01:00
132 lines
3.8 KiB
Go
132 lines
3.8 KiB
Go
package promrelabel
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
)
|
|
|
|
// WriteMetricRelabelDebug writes /metric-relabel-debug page to w with the given metric and relabelConfigs fields.
|
|
func WriteMetricRelabelDebug(w io.Writer, metric, relabelConfigs string) {
|
|
if metric == "" {
|
|
metric = "{}"
|
|
}
|
|
labels, err := promutils.NewLabelsFromString(metric)
|
|
if err != nil {
|
|
err = fmt.Errorf("cannot parse metric: %s", err)
|
|
WriteMetricRelabelDebugSteps(w, nil, metric, relabelConfigs, err)
|
|
return
|
|
}
|
|
pcs, err := ParseRelabelConfigsData([]byte(relabelConfigs))
|
|
if err != nil {
|
|
err = fmt.Errorf("cannot parse relabel configs: %s", err)
|
|
WriteMetricRelabelDebugSteps(w, nil, metric, relabelConfigs, err)
|
|
return
|
|
}
|
|
|
|
dss := newDebugRelabelSteps(pcs, labels, false)
|
|
WriteMetricRelabelDebugSteps(w, dss, metric, relabelConfigs, nil)
|
|
}
|
|
|
|
// WriteTargetRelabelDebug writes /target-relabel-debug page to w with the corresponding args.
|
|
func WriteTargetRelabelDebug(w io.Writer, targetID, metric, relabelConfigs string, err error) {
|
|
if metric == "" {
|
|
metric = "{}"
|
|
}
|
|
if targetID == "" {
|
|
WriteTargetRelabelDebugSteps(w, targetID, nil, metric, relabelConfigs, err)
|
|
return
|
|
}
|
|
labels, err := promutils.NewLabelsFromString(metric)
|
|
if err != nil {
|
|
err = fmt.Errorf("cannot parse metric: %s", err)
|
|
WriteTargetRelabelDebugSteps(w, targetID, nil, metric, relabelConfigs, err)
|
|
return
|
|
}
|
|
pcs, err := ParseRelabelConfigsData([]byte(relabelConfigs))
|
|
if err != nil {
|
|
err = fmt.Errorf("cannot parse relabel configs: %s", err)
|
|
WriteTargetRelabelDebugSteps(w, targetID, nil, metric, relabelConfigs, err)
|
|
return
|
|
}
|
|
dss := newDebugRelabelSteps(pcs, labels, true)
|
|
WriteTargetRelabelDebugSteps(w, targetID, dss, metric, relabelConfigs, nil)
|
|
}
|
|
|
|
func newDebugRelabelSteps(pcs *ParsedConfigs, labels *promutils.Labels, isTargetRelabel bool) []DebugStep {
|
|
// The target relabeling below must be in sync with the code at scrapeWorkConfig.getScrapeWork if isTragetRelabeling=true
|
|
// and with the code at scrapeWork.addRowToTimeseries when isTargetRelabeling=false
|
|
|
|
// Prevent from modifying the original labels
|
|
labels = labels.Clone()
|
|
|
|
// Apply relabeling
|
|
labelsResult, dss := pcs.ApplyDebug(labels.GetLabels())
|
|
labels.Labels = labelsResult
|
|
outStr := LabelsToString(labels.GetLabels())
|
|
|
|
if isTargetRelabel {
|
|
// Add missing instance label
|
|
if labels.Get("instance") == "" {
|
|
address := labels.Get("__address__")
|
|
if address != "" {
|
|
inStr := outStr
|
|
labels.Add("instance", address)
|
|
outStr = LabelsToString(labels.GetLabels())
|
|
dss = append(dss, DebugStep{
|
|
Rule: "add missing instance label from __address__ label",
|
|
In: inStr,
|
|
Out: outStr,
|
|
})
|
|
}
|
|
}
|
|
|
|
// Remove labels with __ prefix
|
|
inStr := outStr
|
|
labels.RemoveLabelsWithDoubleUnderscorePrefix()
|
|
outStr = LabelsToString(labels.GetLabels())
|
|
if inStr != outStr {
|
|
dss = append(dss, DebugStep{
|
|
Rule: "remove labels with __ prefix",
|
|
In: inStr,
|
|
Out: outStr,
|
|
})
|
|
}
|
|
} else {
|
|
// Remove labels with __ prefix except of __name__
|
|
inStr := outStr
|
|
labels.Labels = FinalizeLabels(labels.Labels[:0], labels.Labels)
|
|
outStr = LabelsToString(labels.GetLabels())
|
|
if inStr != outStr {
|
|
dss = append(dss, DebugStep{
|
|
Rule: "remove labels with __ prefix except of __name__",
|
|
In: inStr,
|
|
Out: outStr,
|
|
})
|
|
}
|
|
}
|
|
|
|
// There is no need in labels' sorting, since LabelsToString() automatically sorts labels.
|
|
return dss
|
|
}
|
|
|
|
func getChangedLabelNames(in, out *promutils.Labels) map[string]struct{} {
|
|
inMap := in.ToMap()
|
|
outMap := out.ToMap()
|
|
changed := make(map[string]struct{})
|
|
for k, v := range outMap {
|
|
inV, ok := inMap[k]
|
|
if !ok || inV != v {
|
|
changed[k] = struct{}{}
|
|
}
|
|
}
|
|
for k, v := range inMap {
|
|
outV, ok := outMap[k]
|
|
if !ok || outV != v {
|
|
changed[k] = struct{}{}
|
|
}
|
|
}
|
|
return changed
|
|
}
|