[REFACTOR] Relabel: Remove unnecessary Process() function

All uses can be replaced by ProcessBuilder, which is more efficient.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2025-11-13 18:31:54 +00:00
parent a588145bc1
commit 98c5bece06
3 changed files with 17 additions and 21 deletions

View file

@ -269,22 +269,8 @@ func (re Regexp) String() string {
return str[5 : len(str)-2]
}
// Process returns a relabeled version of the given label set. The relabel configurations
// are applied in order of input.
// There are circumstances where Process will modify the input label.
// If you want to avoid issues with the input label set being modified, at the cost of
// higher memory usage, you can use lbls.Copy().
// If a label set is dropped, EmptyLabels and false is returned.
func Process(lbls labels.Labels, cfgs ...*Config) (ret labels.Labels, keep bool) {
lb := labels.NewBuilder(lbls)
if !ProcessBuilder(lb, cfgs...) {
return labels.EmptyLabels(), false
}
return lb.Labels(), true
}
// ProcessBuilder is like Process, but the caller passes a labels.Builder
// containing the initial set of labels, which is mutated by the rules.
// ProcessBuilder applies relabeling configurations (rules) to the labels in lb.
// The rules are applied in order of input. Returns false if the rule says to drop.
func ProcessBuilder(lb *labels.Builder, cfgs ...*Config) (keep bool) {
for _, cfg := range cfgs {
keep = relabel(cfg, lb)

View file

@ -751,10 +751,11 @@ func TestRelabel(t *testing.T) {
require.NoError(t, cfg.Validate(model.UTF8Validation))
}
res, keep := Process(test.input, test.relabel...)
lb := labels.NewBuilder(test.input)
keep := ProcessBuilder(lb, test.relabel...)
require.Equal(t, !test.drop, keep)
if keep {
testutil.RequireEqual(t, test.output, res)
testutil.RequireEqual(t, test.output, lb.Labels())
}
}
}
@ -1064,9 +1065,11 @@ func BenchmarkRelabel(b *testing.B) {
require.NoError(b, err)
}
for _, tt := range tests {
lb := labels.NewBuilder(labels.EmptyLabels())
b.Run(tt.name, func(b *testing.B) {
for b.Loop() {
_, _ = Process(tt.lbls, tt.cfgs...)
lb.Reset(tt.lbls)
_ = ProcessBuilder(lb, tt.cfgs...)
}
})
}

View file

@ -1344,13 +1344,20 @@ func (api *API) targetRelabelSteps(r *http.Request) apiFuncResult {
return apiFuncResult{nil, &apiError{errorBadData, fmt.Errorf("error retrieving scrape config: %w", err)}, nil, nil}
}
lb := labels.NewBuilder(lbls)
rules := scrapeConfig.RelabelConfigs
steps := make([]RelabelStep, len(rules))
for i, rule := range rules {
outLabels, keep := relabel.Process(lbls, rules[:i+1]...)
keep := relabel.ProcessBuilder(lb, rule)
if !keep { // For all remaining rules, show blank output and keep=false.
for j, rule := range rules[i:] {
steps[i+j] = RelabelStep{Rule: rule}
}
break
}
steps[i] = RelabelStep{
Rule: rule,
Output: outLabels,
Output: lb.Labels(),
Keep: keep,
}
}