1407 Commits

Author SHA1 Message Date
beorn7
2628320292 promql: Fix when to emit a HistogramCounterResetCollisionWarning
So far, we emitted a `HistogramCounterResetCollisionWarning` when
encountering conflicting counter resets in the calculation of (i)rate
and friends. We even tested for that. However, in the rate
calculation, we are not interested in those collisions. They are
actually expected.

On the other hand, we did not warn about those collisions when doing a
`sum` aggregation, where such a warning would be appropriate.

This commit removes the warning in the former case and adds it in the
latter. Sadly, we cannot really test this as we still remove the
counter reset hint for the first sample in a chunk. (And that's the
only sample where we could get a `NotCounterReset` hint.)

Signed-off-by: beorn7 <beorn@grafana.com>
2025-09-03 18:00:03 +02:00
beorn7
03588328d2 promqltest: Test for counter reset conflict warnings
This is an attempt to make sure that we are not accidentally warning
about conflicting counter resets in rate calculation, see
https://github.com/prometheus/prometheus/pull/17051#issuecomment-3226503416 .

This is done by being more explicit about the warn expectation.
However, as long as
https://github.com/prometheus/prometheus/issues/15346 is not
addressed, we won't be able to trigger the annotation this way anyway.

However, we can play a trick, by wrapping a suitable expression in
`histogram_count` or `histogram_sum`, which will invoke the
`HistogramStatsIterator`, which in turn creates counter reset hints on
the fly. So this commit also adds tests with that, both for absence of
an annotation with `rate` and presence of an annotation with
`sum_over_time`.

Signed-off-by: beorn7 <beorn@grafana.com>

test tbs

Signed-off-by: beorn7 <beorn@grafana.com>
2025-09-03 18:00:03 +02:00
Craig Ringer
30bf18f968 test: Add additional tests for mixed float/histogram series
Add further tests for first_over_time (also covering existing
last_over_time, count_over_time, etc) to exercise vectors
containing a mix of float and histogram samples where the
histogram samples do not come last in the series.

This tripped over https://github.com/prometheus/prometheus/issues/17025
so it's structured a bit oddly to work around that bug in the
appender as used by promtest.

Signed-off-by: Craig Ringer <craig.ringer@enterprisedb.com>
2025-09-02 10:24:37 +12:00
Craig Ringer
1ce84d8e2f feat(promql): add first_over_time and ts_of_first_over_time
Add a first_over_time function, and corresponding ts_of_first_over_time
function.  Both are behind the experimental functions feature flag.

Signed-off-by: Craig Ringer <craig.ringer@enterprisedb.com>
2025-09-02 10:24:31 +12:00
beorn7
747c5ee2b1 Apply analyzer "modernize" to the whole codebase
See
https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize
for details.

This ran into a few issues (arguably bugs in the modernize tool),
which I will fix in the next commit, so that we have transparency what
was done automatically.

Beyond those hiccups, I believe all the changes applied are
legitimate. Even where there might be no tangible direct gain, I would
argue it's still better to use the "modern" way to avoid micro
discussions in tiny style PRs later.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-08-27 14:48:41 +02:00
Bryan Boreham
153cdb2b0b [PERF] PromQL: Replace Fprintf %f with AppendFloat
The combination of `AvailableBuffer`` followed by `Write` is optimised
inside `bytes.Buffer`.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-22 10:58:01 +01:00
Bryan Boreham
b8d2d505f5 [PERF] PromQL: Replace some Sprintf with bytes.Buffer
Goes faster due to reduced memory allocation.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-21 11:38:05 +01:00
Bryan Boreham
49d9261693 [PERF] PromQL: Replace some simple Sprintf with string concat
This goes faster because there is no runtime format parsing.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-21 11:05:49 +01:00
Bryan Boreham
e44ee2f182 [TESTS] PromQL: Add BenchmarkExprString
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-21 11:05:38 +01:00
Bryan Boreham
66fbea97bb [TESTS] Check expr with function call in TestExprString
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-21 11:04:46 +01:00
Bryan Boreham
8b3f59e9c3
Merge pull request #16593 from bboreham/ast-child-iter
[PERF] PromQL: Reduce allocations when walking syntax tree
2025-08-21 09:14:41 +01:00
Julius Hinze
cdf7208478
annotations: histogram counter reset warning includes operation
Signed-off-by: Julius Hinze <julius.hinze@grafana.com>
2025-08-20 15:14:21 +02:00
Julius Hinze
77b5c3f217
Histograms: set annotation when adding or subtracting histograms that have not_reset and reset hints.
Signed-off-by: Julius Hinze <julius.hinze@grafana.com>
2025-08-20 15:00:45 +02:00
Arve Knudsen
0a40df33fb
Make metric/label name validation scheme explicit (#16928)
* Parameterize metric/label name validation scheme

Parameterized metric/label name validation scheme

---------

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Co-authored-by: Julius Hinze <julius.hinze@grafana.com>
2025-08-18 08:09:00 +00:00
Lukasz Mierzwa
7e22d2e5c0 Refactor TestParseExpressions to be more explicit about errors
Right now TestParseExpressions tests if a query returns an error but it only does a fuzzy check on returned errors.
The error returned by the parser is ParseErrors, which is a slice of ParseErr structs.
The Error() method on ParseErrors will return an error string based on the first error in that slice. This hides other returned errors so we can end up with bogus errors being returned but won't ever find this via this test.
This change makes the test compare returned error (which is always ParseErrors type) with expected ParseErrors slice.
The extra benefit of this is that current tests mostly ignore error positional range and only test for correct error message. Now errors must return expected positional information.
There are a few cases uncovered where the positional informatio of errors seems wrong, added FIXME for these lines.

Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>
2025-08-14 10:01:44 +01:00
Lukasz Mierzwa
7b308dc7fe Add a note about PositionRange values
Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>
2025-08-13 17:57:40 +01:00
Neeraj Gartia
2c0de4e7c2
Fix histogram_quantile annotation in range query when delayed name removal is disabled (#16794)
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-08-13 18:06:48 +02:00
Björn Rabenstein
4217d4ba46
Merge pull request #17046 from prometheus/beorn7/promql
promqltest: Add test for unary minus with native histograms
2025-08-13 16:57:09 +02:00
George Krajcsovits
2aaeefe8e1
fix(parser): wrong end position aggregate expression (#17031)
* fix(parser): wrong end position aggregate expression

Fixes: https://github.com/prometheus/prometheus/issues/16053

The position range of nested aggregate expression was wrong, for the
expression "(sum(foo))" the position of "sum(foo)" should be 1-9, but
the parser could not decide the end of the expression on pos 9, instead
it read ahead to pos 10 and then emitted the aggregate. But we only
kept the last closing position (10) and wrote that into the aggregate.

The reason for this is that the parser cannot know from "(sum(foo)" alone
if the aggregate is finished. It could be finished as in "(sum(foo))" but
equally it could continue with group modifier as "(sum(foo) by (bar))".

Previous fix in #16041 tried to keep track of parenthesis, but that is
complicated because the error happens after closing two parenthesis. That
fix introduced new bugs.

This fix now addresses the issue directly. Since we have to step outside
the parser state machine anyway, we can just add an algorithm to
detect and fix the issue. That's Lexer.findPrevRightParen().

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-08-13 15:52:52 +02:00
beorn7
e326049e43 promqltest: Add test for unary minus with native histograms
This verifies that a counter histogram becomes a gauge histogram if an
unary minus is applied to it.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-08-13 15:39:52 +02:00
Björn Rabenstein
caf24555a8
Merge pull request #17004 from juliusmh/nh_unary_minus_as_gauge
model: set native histogram GaugeType hint when subtracting or multiplying/dividing with negative factors
2025-08-13 15:25:06 +02:00
Bryan Boreham
7ab68550dc [PERF] PromQL: Unwrap superfluous parens during preprocessing
This means we only do it once, rather than on every step of a range
query. Also the code gets a bit shorter.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-13 14:01:47 +01:00
Bryan Boreham
384db72ede [PERF] PromQL: Stop checking step-invariant arguments
In aggregations and function calls. We no longer wrap the literal values
or matrix selectors that would appear here.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-13 14:01:47 +01:00
Bryan Boreham
94d3cac4ea [PERF] PromQL: Don't wrap matrix selectors as time-invariant
Matrix selectors have a Timestamp which indicates they are time-invariant,
so we don't need to wrap and then unwrap them when we come to use them.

Fix up tests that check this level of detail.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-13 14:01:47 +01:00
Bryan Boreham
b290e0ec17 [PERF] PromQL: Don't wrap constant expressions as time-invariant
This should mean we can stop unwrapping them later.

Fix up tests that check very specific details.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-13 10:54:40 +01:00
Julius Hinze
5855d973b0
model: set native histogram GaugeType hint when subtracting or multiplying/dividing with negative factors
Signed-off-by: Julius Hinze <julius.hinze@grafana.com>
2025-08-12 18:16:39 +02:00
Bryan Boreham
7512d13e00
Merge pull request #17002 from bboreham/reduce-promql-benchmark
[TESTS] PromQL: shrink work done in BenchmarkRangeQuery
2025-08-06 12:07:49 +01:00
Bartlomiej Plotka
5df982538f
Merge pull request #16994 from mmorel-35/unused-parameters
chore: enable unused-receiver rule from revive
2025-08-06 10:46:28 +01:00
Bartlomiej Plotka
3fc87f40e8
Merge pull request #16998 from mmorel-35/govet/enable-all
chore: enable-all rule from govet
2025-08-06 10:44:44 +01:00
Bryan Boreham
31eb3b286d [TESTS] PromQL: shrink work done in BenchmarkRangeQuery
Previously, BenchmarkRangeQuery would run each case with three data sizes
(1, 10, 100) and three range lengths (1, 100, 1000) for nine variations.

With 36 cases, running with `-count=6` to get dependable results, would
take 40-50 minutes in total.

This PR removes the middle option in both dimensions, thus shrinking to
four variations and about 20 minutes to run everything.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-08-05 14:21:23 +01:00
George Krajcsovits
457a2381f9
fix(parser): revert pr 16041 and 16754 (#16996)
* fix(parser): revert pr 16041 and 16754

Fix for https://github.com/prometheus/prometheus/issues/16053
turned out to be too complicated and lead to more errors than it solved.
See https://github.com/prometheus/prometheus/pull/16875.

* add TODOs to wrong end positions
* restore test and fix initialization bug

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-08-05 10:48:08 +02:00
Matthieu MOREL
0484a6d547 chore: enable-all rule from govet
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-08-04 18:06:36 +02:00
Matthieu MOREL
cef219c31c chore: enable unused-receiver rule from revive
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-08-04 09:43:33 +00:00
Bryan Boreham
e9cf792c47
Merge pull request #16895 from bboreham/noinline-kahan
[BUGFIX] PromQL: Noinline kahanSumInc, to reduce numerical errors
2025-07-28 15:55:19 +01:00
Carrie Edwards
44b0fbba1e
No info annotation for rate/increase when type is histogram (#16915)
Signed-off-by: Carrie Edwards <edwrdscarrie@gmail.com>
2025-07-24 10:11:37 -07:00
Darshan Chaudhary
9b00c296a8
[PERF] PromQL: Pass concrete types to promql functions (#16797)
Currently, the promql functions take the interface slice []parser.Value as an argument,
which is being implemented by the conrete types Vector, Matrix etc. This PR replaces
the interface with the concrete types, resulting in improved performance.
The inspiration for this PR came from #16698 which does this for binops. 
I extended the idea to all promql functions

Signed-off-by: darshanime <deathbullet@gmail.com>

* pass single Matrix

Signed-off-by: darshanime <deathbullet@gmail.com>

---------

Signed-off-by: darshanime <deathbullet@gmail.com>
2025-07-23 17:30:03 +01:00
Harry John
b09cf6be8d
fix(promql): Ensure native histogram values copied in subqueries (#16879)
Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com>
2025-07-21 08:54:54 +02:00
Bryan Boreham
358499f868 [BUGFIX] PromQL: Noinline kahanSum, to avoid compiler optimisations
Observed on go1.24.4 darwin/arm64, the compensation variable is not correctly calculated.

Note you may have to run the tests several times to see it, to get the right ordering of series.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-07-19 15:19:29 +01:00
Bryan Boreham
75b72c2e42 Apply review feedback
Make the order of aggregate parts the same as before.

Make error message match.

Fix up benchmark for changes elsewhere.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-07-11 14:10:31 +01:00
Bryan Boreham
e4789cf89f [PERF] PromQL: Reduce allocations when walking syntax tree
Currently it allocates at least once on every recursion, and since
`append` gives no extra space for 1 or 2 elements, will allocate
several times for even modestly complex expressions.

Instead, allocate space for 4 elements to begin, and defer adding
another until we need it.

Add a note that `path` may get overwritten; this was true previously.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-07-11 14:09:20 +01:00
Bryan Boreham
ceac4d2418 [PERF] PromQL: Walk syntax tree using iterator
Saves memory allocations. This is called a few times from the PromQL Engine.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-07-11 14:09:20 +01:00
Bryan Boreham
0339fbd5a3 [TESTS] PromQL: Add benchmark for PreprocessExpr
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-07-11 14:09:20 +01:00
George Krajcsovits
4662722a8e
chore(promql): improve doc string in promql/durations.go (#16863)
Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-07-11 12:07:54 +02:00
Björn Rabenstein
b7f984d6d2
Merge pull request #16585 from kapillamba4/fix/16393-strict
Convert PromQL tests to new syntax via basic migration mode
2025-07-10 15:45:38 +02:00
Björn Rabenstein
eb3ea163fa
promqltest: add tests for histogram_count(increase(...)) (#16854)
As `histogram_count` is playing tricks to improve performance, we
better make sure that the limitation of extrapolation below zero still
works as expected.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-07-10 15:44:02 +02:00
Björn Rabenstein
362141370d
Merge pull request #16828 from prometheus/beorn7/histogram2
promql(histograms): scale a histogram the same as the count
2025-07-10 13:26:15 +02:00
beorn7
107e4a00c3 promqltest: Test NaN sample values for quantile aggregator
Signed-off-by: beorn7 <beorn@grafana.com>
2025-07-09 13:38:19 +02:00
liangmulu
b1a7df2c0c chore: fix some minor issues in comments
Signed-off-by: liangmulu <liangmulu@outlook.com>
2025-07-09 18:05:41 +08:00
Kapil Lamba
df0e034314 address code review comments
Signed-off-by: Kapil Lamba <kapillamba4@gmail.com>
2025-07-09 07:25:31 +05:30
Björn Rabenstein
dbee82267a
Merge pull request #16725 from MichaHoffmann/mhoffmann/fix-topk-nan-arg-error-on-nonexisting-series
promql: fix topk error on NaN argument for non-existing series
2025-07-08 19:42:20 +02:00