Make PromQL scalar reduceTo operator configurable on the PromQuery spec
Problem
pkg/querier/promql_query.go:387-389 carries a TODO(srikanthccv):
"last" mirrors the instant-query semantics we have always followed for PromQL scalar requests, but it should be configurable on the PromQuery spec just like the builder reduceTo.
For every PromQL scalar request, the execute path at
pkg/querier/promql_query.go:390-396 reduces each series to its last point
with qbv5.FunctionReduceTo(s, qbv5.ReduceToLast). The builder path already
honors a configurable operator: pkg/querier/postprocess.go:222-227 reads
query.Aggregations[0].ReduceTo and applies it via applyMetricReduceTo
when set. The PromQL path has no equivalent field, so the operator is
fixed at last.
Motivation
A PromQL scalar request returns one value per series, so the reduce
operator is the only thing the caller can tune. The current hardcoded
last is the right default for instant-query semantics, but it is not the
right default for every dashboard or alert. With the field absent, a
caller that wants sum, avg, max, or min has no way to ask for it
on the PromQL path. The TODO explicitly asks for the builder parity.
Proposed solution
Add a reduceTo field to the PromQuery spec at
pkg/types/querybuildertypes/querybuildertypesv5/prom_query.go, using the
existing ReduceTo type from
pkg/types/querybuildertypes/querybuildertypesv5/builder_elements.go:297.
Wire it into the execute path: when the field is ReduceToUnknown
(empty) the existing last behavior is preserved, otherwise the
configured operator is used.
Concretely:
PromQuery.ReduceTo qbv5.ReduceTo \json:"reduceTo,omitzero"``- In
Execute, replace the hardcodedqbv5.ReduceToLastwith a helper that defaultsReduceToUnknowntoReduceToLastand otherwise applies the configured operator.
The same FunctionReduceTo helper is reused, so the actual reduction
logic and the NaN handling are unchanged.
Scope
pkg/types/querybuildertypes/querybuildertypesv5/prom_query.go— add theReduceTofield.pkg/querier/promql_query.go— small helper, updatedExecute, remove the TODO comment.pkg/querier/promql_query_test.go— table-driven tests for the new helper coveringReduceToUnknown(defaultlast),ReduceToSum,ReduceToAvg,ReduceToMax.
Acceptance criteria
- A
PromQuerywith noreduceToset produces byte-identical scalar output to the current implementation. - A
PromQuerywithreduceTo: "sum"reduces each series viaFunctionReduceTowithReduceToSum. - A
PromQuerywithreduceTo: "max"reduces each series viaFunctionReduceTowithReduceToMax. - The other supported operators (
min,avg,count,median) follow the same pattern. - An unknown operator (
reduceTo: "bogus") falls through toFunctionReduceTo's existing "unrecognized operator returns the original series" behavior. go vet ./pkg/querier/...andgo test -race -count=1 -short ./pkg/querier/...pass.- The
TODO(srikanthccv)comment atpkg/querier/promql_query.go:387is removed.
Backward compatibility
Adding a new optional field is non-breaking. Existing callers that omit
reduceTo get the same last behavior. The JSON tag uses omitzero
so the field is omitted from serialised output when not set.
Alternatives considered
- Add validation that rejects unknown operators. Rejected: the
existing
FunctionReduceToalready has a graceful fallback (returns the original series for unrecognized operators). Adding an upstream validator would be a behavior change, not a parity change, and the builder path does not validate either. - Promote the reduce step to a public helper in
pkg/querybuilder/. Rejected: the helper is querier-local, the same pattern lives next to its only caller, and the existingFunctionReduceToalready lives in the shared types package. - Add a separate
ScalarReducerinterface. Rejected: the field is the cleanest mirror of the builder'sReduceToand the maintainer TODO explicitly references that parity.
Source: SigNoz/signoz