From de37edd816c8c99adf4d6740a94d554ad4c951dd Mon Sep 17 00:00:00 2001 From: discord9 Date: Tue, 3 Nov 2026 17:21:35 +0800 Subject: [PATCH] fix(prometheus): report the metric name only where the aggregation keeps it (#10108) * fix(prometheus): report the metric name only where the aggregation keeps it An aggregate response carried a `__name__` label the aggregate does not have. The response layer derived the name from the selector and attached it whatever the aggregation did, so `sum(alpha_total{job="api"})` answered with `{"__name__":"alpha_total"}` where Prometheus answers with no labels at all, and `sum without (host)` likewise kept a name the aggregation discards. Prometheus keeps the metric name on an aggregate only when the aggregation groups by it: `by (__name__)`, or a `by` list containing `__name__`. Every `without` drops it, including `without ()` and exclusions of labels that do not exist, and an aggregate with no grouping modifier drops it too. The aggregate function does not matter, and neither does whether the selector names a metric. The rule is stated once now: keep the name only for a `by` whose labels include `__name__`, and clear otherwise. A chain of guards left the ungrouped case to fall through to the selector, which is how the name leaked in. The previous behaviour had a test asserting it, so the suite agreed with the code and neither agreed with Prometheus. The expectations here were measured against Prometheus 2.53.0 across all four modifier shapes and five aggregate functions before being written down. Signed-off-by: discord9 * fix(prometheus): keep the metric name for the aggregates that select series The naming rule added for aggregates was applied to every operator. It holds for the ones that fold a group's values into one: those drop the metric name unless the grouping names it. But `topk`, `bottomk`, `limitk` and `limit_ratio` return some of the input's series with their labels intact, so the name survives them, and the grouping modifier only decides which bucket to select from. Applying the folding rule to those four stripped `__name__` from a response that used to carry it - on the default path, not only under the native route, so every deployment saw it. Read the operator before the modifier: the four selectors recurse into their input and keep whatever name it contributes, and every other aggregate keeps the measured rule. Recursing rather than synthesising also keeps a name the inner expression already dropped, so `topk(1, sum(cpu_usage))` stays nameless. Signed-off-by: discord9 --------- Signed-off-by: discord9 --- src/servers/src/http/prometheus.rs | 155 +++++++++++++++++++++++++---- 1 file changed, 136 insertions(+), 19 deletions(-) diff --git a/src/servers/src/http/prometheus.rs b/src/servers/src/http/prometheus.rs index e417044ef43..7cf5ce19156 100644 --- a/src/servers/src/http/prometheus.rs +++ b/src/servers/src/http/prometheus.rs @@ -49,7 +49,7 @@ use futures::future::join_all; use futures::{StreamExt, TryStreamExt}; use itertools::Itertools; use promql_parser::label::{METRIC_NAME, MatchOp, Matcher, Matchers}; -use promql_parser::parser::token::TokenType; +use promql_parser::parser::token::{self, TokenType}; use promql_parser::parser::value::ValueType; use promql_parser::parser::{ AggregateExpr, BinModifier, BinaryExpr, Call, Expr as PromqlExpr, LabelModifier, @@ -1448,21 +1448,19 @@ fn binary_metric_name_side<'a>( /// Recursively collect all metric names from a PromQL expression fn collect_metric_names(expr: &PromqlExpr, metric_names: &mut HashSet) { match expr { - PromqlExpr::Aggregate(AggregateExpr { modifier, expr, .. }) => { - match modifier { + PromqlExpr::Aggregate(AggregateExpr { + op, modifier, expr, .. + }) => { + if !matches!( + op.id(), + token::T_TOPK | token::T_BOTTOMK | token::T_LIMITK | token::T_LIMIT_RATIO + ) && !matches!( + modifier, Some(LabelModifier::Include(labels)) - if !labels.labels.contains(&METRIC_NAME.to_string()) => - { - metric_names.clear(); - return; - } - Some(LabelModifier::Exclude(labels)) - if labels.labels.contains(&METRIC_NAME.to_string()) => - { - metric_names.clear(); - return; - } - _ => {} + if labels.labels.contains(&METRIC_NAME.to_string()) + ) { + metric_names.clear(); + return; } collect_metric_names(expr, metric_names) } @@ -3252,12 +3250,124 @@ mod tests { }, // Aggregation and function cases TestCase { - name: "metric with aggregation", - promql: "sum(cpu_usage)", + name: "topk preserves metric name", + promql: "topk(1, cpu_usage)", expected_metric: Some("cpu_usage"), expected_type: ValueType::Vector, should_error: false, }, + TestCase { + name: "bottomk preserves metric name", + promql: "bottomk(1, cpu_usage)", + expected_metric: Some("cpu_usage"), + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "topk without preserves metric name", + promql: "topk without(host) (1, cpu_usage)", + expected_metric: Some("cpu_usage"), + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "topk by preserves metric name", + promql: "topk by(job) (1, cpu_usage)", + expected_metric: Some("cpu_usage"), + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "limitk preserves metric name", + promql: "limitk(1, cpu_usage)", + expected_metric: Some("cpu_usage"), + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "topk does not restore folded metric name", + promql: "topk(1, sum(cpu_usage))", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "outer aggregate drops topk metric name", + promql: "sum(topk(1, cpu_usage))", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "ungrouped aggregate drops metric name", + promql: "sum(cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "aggregate by non-name label drops metric name", + promql: "sum by (job) (cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "aggregate without non-name label drops metric name", + promql: "sum without (host) (cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "aggregate without empty label list drops metric name", + promql: "sum without () (cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "aggregate without nonexistent label drops metric name", + promql: "sum without (nonexistent) (cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "aggregate by name and job keeps metric name", + promql: "sum by (__name__, job) (cpu_usage)", + expected_metric: Some("cpu_usage"), + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "minimum without host drops metric name", + promql: "min without (host) (cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "aggregate without name drops metric name", + promql: "sum without (__name__) (cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "ungrouped average drops metric name", + promql: "avg(cpu_usage)", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "nested ungrouped aggregate drops metric name", + promql: "sum(sum by (job) (cpu_usage))", + expected_metric: None, + expected_type: ValueType::Vector, + should_error: false, + }, TestCase { name: "complex aggregation", promql: r#"sum by (instance) (cpu_usage{job="node"})"#, @@ -3273,9 +3383,16 @@ mod tests { should_error: false, }, TestCase { - name: "complex aggregation", + name: "grouping by name preserves equality metric name", + promql: r#"sum by (__name__) ({__name__="alpha_total"})"#, + expected_metric: Some("alpha_total"), + expected_type: ValueType::Vector, + should_error: false, + }, + TestCase { + name: "aggregate without instance drops metric name", promql: r#"sum without (instance) (cpu_usage{job="node"})"#, - expected_metric: Some("cpu_usage"), + expected_metric: None, expected_type: ValueType::Vector, should_error: false, },