From 8e04ab6dd4eb3fc3a2c67f974f07f8ac2b852fa8 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 16 Jan 2024 17:49:43 +0000 Subject: [PATCH] promql: refactor: extract generateGroupingLabels function Signed-off-by: Bryan Boreham --- promql/engine.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 2f7dcb222..56a7774c6 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -2691,19 +2691,7 @@ func (ev *evaluator) aggregation(e *parser.AggregateExpr, grouping []string, par group, ok := result[groupingKey] // Add a new group if it doesn't exist. if !ok { - var m labels.Labels - enh.resetBuilder(metric) - switch { - case without: - enh.lb.Del(grouping...) - enh.lb.Del(labels.MetricName) - m = enh.lb.Labels() - case len(grouping) > 0: - enh.lb.Keep(grouping...) - m = enh.lb.Labels() - default: - m = labels.EmptyLabels() - } + m := generateGroupingLabels(enh, metric, without, grouping) newAgg := &groupedAggregation{ labels: m, floatValue: s.F, @@ -2969,6 +2957,21 @@ func generateGroupingKey(metric labels.Labels, grouping []string, without bool, return metric.HashForLabels(buf, grouping...) } +func generateGroupingLabels(enh *EvalNodeHelper, metric labels.Labels, without bool, grouping []string) labels.Labels { + enh.resetBuilder(metric) + switch { + case without: + enh.lb.Del(grouping...) + enh.lb.Del(labels.MetricName) + return enh.lb.Labels() + case len(grouping) > 0: + enh.lb.Keep(grouping...) + return enh.lb.Labels() + default: + return labels.EmptyLabels() + } +} + // btos returns 1 if b is true, 0 otherwise. func btos(b bool) float64 { if b {