2023-01-04 07:26:27 +00:00
|
|
|
package promrelabel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
|
|
)
|
|
|
|
|
2023-01-05 11:26:47 +00:00
|
|
|
// WriteMetricRelabelDebug writes /metric-relabel-debug page to w with the corresponding args.
|
2023-04-26 08:53:29 +00:00
|
|
|
func WriteMetricRelabelDebug(w io.Writer, targetID, metric, relabelConfigs, format string, err error) {
|
|
|
|
writeRelabelDebug(w, false, targetID, metric, relabelConfigs, format, err)
|
2023-01-04 07:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTargetRelabelDebug writes /target-relabel-debug page to w with the corresponding args.
|
2023-04-26 08:53:29 +00:00
|
|
|
func WriteTargetRelabelDebug(w io.Writer, targetID, metric, relabelConfigs, format string, err error) {
|
|
|
|
writeRelabelDebug(w, true, targetID, metric, relabelConfigs, format, err)
|
2023-01-05 11:26:47 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 08:53:29 +00:00
|
|
|
func writeRelabelDebug(w io.Writer, isTargetRelabel bool, targetID, metric, relabelConfigs, format string, err error) {
|
2023-01-04 07:26:27 +00:00
|
|
|
if metric == "" {
|
2023-05-12 22:58:03 +00:00
|
|
|
metric = "{}"
|
2023-01-04 07:26:27 +00:00
|
|
|
}
|
2023-05-12 22:58:03 +00:00
|
|
|
targetURL := ""
|
2023-01-06 00:35:20 +00:00
|
|
|
if err != nil {
|
2023-04-26 08:53:29 +00:00
|
|
|
WriteRelabelDebugSteps(w, targetURL, targetID, format, nil, metric, relabelConfigs, err)
|
2023-01-04 07:26:27 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-12 22:58:03 +00:00
|
|
|
labels, err := promutils.NewLabelsFromString(metric)
|
2023-01-04 07:26:27 +00:00
|
|
|
if err != nil {
|
2023-10-25 19:24:01 +00:00
|
|
|
err = fmt.Errorf("cannot parse metric: %w", err)
|
2023-04-26 08:53:29 +00:00
|
|
|
WriteRelabelDebugSteps(w, targetURL, targetID, format, nil, metric, relabelConfigs, err)
|
2023-01-04 07:26:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
pcs, err := ParseRelabelConfigsData([]byte(relabelConfigs))
|
|
|
|
if err != nil {
|
2023-10-25 19:24:01 +00:00
|
|
|
err = fmt.Errorf("cannot parse relabel configs: %w", err)
|
2023-04-26 08:53:29 +00:00
|
|
|
WriteRelabelDebugSteps(w, targetURL, targetID, format, nil, metric, relabelConfigs, err)
|
2023-01-04 07:26:27 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-05 11:26:47 +00:00
|
|
|
|
2023-05-12 22:58:03 +00:00
|
|
|
dss, targetURL := newDebugRelabelSteps(pcs, labels, isTargetRelabel)
|
2023-04-26 08:53:29 +00:00
|
|
|
WriteRelabelDebugSteps(w, targetURL, targetID, format, dss, metric, relabelConfigs, nil)
|
2023-01-04 07:26:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 05:07:52 +00:00
|
|
|
func newDebugRelabelSteps(pcs *ParsedConfigs, labels *promutils.Labels, isTargetRelabel bool) ([]DebugStep, string) {
|
|
|
|
// The target relabeling below must be in sync with the code at scrapeWorkConfig.getScrapeWork if isTargetRelabel=true
|
2023-01-04 07:26:27 +00:00
|
|
|
// and with the code at scrapeWork.addRowToTimeseries when isTargetRelabeling=false
|
2023-03-21 05:07:52 +00:00
|
|
|
targetURL := ""
|
2023-01-04 07:26:27 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:07:52 +00:00
|
|
|
// Generate targetURL
|
|
|
|
targetURL, _ = GetScrapeURL(labels, nil)
|
|
|
|
|
2023-01-04 07:26:27 +00:00
|
|
|
// 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.
|
2023-03-21 05:07:52 +00:00
|
|
|
return dss, targetURL
|
2023-01-04 07:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|