PromQL: comparison operators drop the metric name from the result
up == 0 and node_load1 > 5 come back without __name__. Prometheus keeps the left-hand metric name for comparison operators; only arithmetic operators and bool comparisons drop it.
Grafana legends for those panels render as {instance="..."} instead of node_load1{instance="..."}, and alert templates that read {{ $labels.__name__ }} get nothing.
Where
GreptimeDB stores the metric name as the table name, so the __name__ of a response is derived from the query AST, not from the data. That happens in collect_metric_names in src/servers/src/http/prometheus.rs:
PromqlExpr::Binary(BinaryExpr { lhs, op, .. }) => {
if matches!(op.id(), token::T_LAND | token::T_LOR | token::T_LUNLESS) {
collect_metric_names(lhs, metric_names)
} else {
metric_names.clear()
}
}Every binary operator that is not and/or/unless clears the name. Comparison operators should not.
The rule to implement
Prometheus drops the metric name when shouldDropMetricName(op) holds — + - * / % ^ atan2 — or when the comparison carries bool. A one-to-one on(...) also removes it, because the result keeps only the listed labels.
| Expression | __name__ |
|---|---|
a + b, a * 2 |
dropped |
a > bool b, a > bool 0.5 |
dropped |
a > 0.5, up == 0 |
a / up |
a > b |
a |
a > ignoring(x) b |
a |
a > on(x) b |
dropped |
a > on(x) group_left b |
a |
The last row is not a typo: Prometheus only applies the on/ignoring label reduction for CardOneToOne, so a group modifier leaves the left-hand name in place.
BinaryExpr carries what the decision needs: op.is_comparison_operator(), expr.return_bool(), and modifier.card / modifier.matching. The current match arm discards modifier.
Testing
test_retrieve_metric_name_and_result_type in the same file is table-driven and has no comparison cases yet — add one row per line of the table above. cargo nextest run -p servers retrieve_metric_name.
Worth checking the result against a real Prometheus for anything that looks ambiguous; promql/engine.go, resultMetric and shouldDropMetricName, is the reference.
Both the HTTP API and the gRPC PromQL gateway go through retrieve_metric_name_and_result_type, so one fix covers both.
Source: GreptimeTeam/greptimedb