mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-03-21 15:45:01 +00:00
lib/promrelabel: factor out applyInternal code into ApplyDebug and Apply functions
This improves readability and maintanability Also remove memory allocation from SortLabels()
This commit is contained in:
parent
b564729d75
commit
b09bd6c42a
1 changed files with 44 additions and 45 deletions
|
@ -71,46 +71,29 @@ func (prc *parsedRelabelConfig) String() string {
|
||||||
// It returns DebugStep list - one entry per each applied relabeling step.
|
// It returns DebugStep list - one entry per each applied relabeling step.
|
||||||
func (pcs *ParsedConfigs) ApplyDebug(labels []prompbmarshal.Label) ([]prompbmarshal.Label, []DebugStep) {
|
func (pcs *ParsedConfigs) ApplyDebug(labels []prompbmarshal.Label) ([]prompbmarshal.Label, []DebugStep) {
|
||||||
// Protect from overwriting labels between len(labels) and cap(labels) by limiting labels capacity to its length.
|
// Protect from overwriting labels between len(labels) and cap(labels) by limiting labels capacity to its length.
|
||||||
labels, dss := pcs.applyInternal(labels[:len(labels):len(labels)], 0, true)
|
labels = labels[:len(labels):len(labels)]
|
||||||
return labels, dss
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply applies pcs to labels starting from the labelsOffset.
|
inStr := LabelsToString(labels)
|
||||||
//
|
|
||||||
// Apply() may add additional labels after the len(labels), so make sure it doesn't corrupt in-use labels
|
|
||||||
// stored between len(labels) and cap(labels).
|
|
||||||
func (pcs *ParsedConfigs) Apply(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label {
|
|
||||||
labels, _ = pcs.applyInternal(labels, labelsOffset, false)
|
|
||||||
return labels
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pcs *ParsedConfigs) applyInternal(labels []prompbmarshal.Label, labelsOffset int, debug bool) ([]prompbmarshal.Label, []DebugStep) {
|
|
||||||
var dss []DebugStep
|
var dss []DebugStep
|
||||||
inStr := ""
|
|
||||||
if debug {
|
|
||||||
inStr = LabelsToString(labels[labelsOffset:])
|
|
||||||
}
|
|
||||||
if pcs != nil {
|
if pcs != nil {
|
||||||
for _, prc := range pcs.prcs {
|
for _, prc := range pcs.prcs {
|
||||||
labels = prc.apply(labels, labelsOffset)
|
labels = prc.apply(labels, 0)
|
||||||
if debug {
|
outStr := LabelsToString(labels)
|
||||||
outStr := LabelsToString(labels[labelsOffset:])
|
|
||||||
dss = append(dss, DebugStep{
|
dss = append(dss, DebugStep{
|
||||||
Rule: prc.String(),
|
Rule: prc.String(),
|
||||||
In: inStr,
|
In: inStr,
|
||||||
Out: outStr,
|
Out: outStr,
|
||||||
})
|
})
|
||||||
inStr = outStr
|
inStr = outStr
|
||||||
}
|
if len(labels) == 0 {
|
||||||
if len(labels) == labelsOffset {
|
|
||||||
// All the labels have been removed.
|
// All the labels have been removed.
|
||||||
return labels, dss
|
return labels, dss
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
labels = removeEmptyLabels(labels, labelsOffset)
|
|
||||||
if debug {
|
labels = removeEmptyLabels(labels, 0)
|
||||||
outStr := LabelsToString(labels[labelsOffset:])
|
outStr := LabelsToString(labels)
|
||||||
if outStr != inStr {
|
if outStr != inStr {
|
||||||
dss = append(dss, DebugStep{
|
dss = append(dss, DebugStep{
|
||||||
Rule: "remove empty labels",
|
Rule: "remove empty labels",
|
||||||
|
@ -118,10 +101,27 @@ func (pcs *ParsedConfigs) applyInternal(labels []prompbmarshal.Label, labelsOffs
|
||||||
Out: outStr,
|
Out: outStr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return labels, dss
|
return labels, dss
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply applies pcs to labels starting from the labelsOffset.
|
||||||
|
//
|
||||||
|
// This function may add additional labels after the len(labels), so make sure it doesn't corrupt in-use labels
|
||||||
|
// stored between len(labels) and cap(labels).
|
||||||
|
func (pcs *ParsedConfigs) Apply(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label {
|
||||||
|
if pcs != nil {
|
||||||
|
for _, prc := range pcs.prcs {
|
||||||
|
labels = prc.apply(labels, labelsOffset)
|
||||||
|
if len(labels) == labelsOffset {
|
||||||
|
// All the labels have been removed.
|
||||||
|
return labels
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
labels = removeEmptyLabels(labels, labelsOffset)
|
||||||
|
return labels
|
||||||
|
}
|
||||||
|
|
||||||
func removeEmptyLabels(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label {
|
func removeEmptyLabels(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label {
|
||||||
src := labels[labelsOffset:]
|
src := labels[labelsOffset:]
|
||||||
needsRemoval := false
|
needsRemoval := false
|
||||||
|
@ -546,10 +546,8 @@ func concatLabelValues(dst []byte, labels []prompbmarshal.Label, labelNames []st
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
for _, labelName := range labelNames {
|
for _, labelName := range labelNames {
|
||||||
label := GetLabelByName(labels, labelName)
|
labelValue := getLabelValue(labels, labelName)
|
||||||
if label != nil {
|
dst = append(dst, labelValue...)
|
||||||
dst = append(dst, label.Value...)
|
|
||||||
}
|
|
||||||
dst = append(dst, separator...)
|
dst = append(dst, separator...)
|
||||||
}
|
}
|
||||||
return dst[:len(dst)-len(separator)]
|
return dst[:len(dst)-len(separator)]
|
||||||
|
@ -630,10 +628,11 @@ func LabelsToString(labels []prompbmarshal.Label) string {
|
||||||
|
|
||||||
// SortLabels sorts labels in alphabetical order.
|
// SortLabels sorts labels in alphabetical order.
|
||||||
func SortLabels(labels []prompbmarshal.Label) {
|
func SortLabels(labels []prompbmarshal.Label) {
|
||||||
x := &promutils.Labels{
|
x := promutils.GetLabels()
|
||||||
Labels: labels,
|
x.Labels = labels
|
||||||
}
|
|
||||||
x.Sort()
|
x.Sort()
|
||||||
|
x.Labels = nil
|
||||||
|
promutils.PutLabels(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillLabelReferences(dst []byte, replacement string, labels []prompbmarshal.Label) []byte {
|
func fillLabelReferences(dst []byte, replacement string, labels []prompbmarshal.Label) []byte {
|
||||||
|
|
Loading…
Reference in a new issue