514 Commits

Author SHA1 Message Date
beorn7
0fa70e0f6c promql: Use HistogramStatsIterator more often
The current code stops the walk after we have found the first relevant
function. However, in expressions with multiple legs, we will then use
the `HistogramStatsIterator` at most once. This change should make
sure we explore all legs.

The added tests make sure we are not using `HistogramStatsIterator`
where we shouldn't (but the opposite can only be seen in a benchmark
or with a more explicit test).

Signed-off-by: beorn7 <beorn@grafana.com>
2025-09-09 16:09:22 +02:00
beorn7
0746f388b0 promql: Fix HistogramStatsIterator usage for subqueries
Signed-off-by: beorn7 <beorn@grafana.com>
2025-09-04 14:36:56 +02:00
beorn7
87d7c12563 promql: Fix trigger for HistogramStatsIterator
PR #16702 introduced a regression because it was too strict in
detecting the condition for using the `HistogramStatsIterator`. It
essentially required the triggering function to be buried at least one
level deep.

`histogram_count(sum(rate(native_histogram_series[2m]))` would not
trigger anymore, but
`1*histogram_count(sum(rate(native_histogram_series[2m]))` would.

Ironically, PR #16682 made the performance of the
`HistogramStatsIterator` so much worse that _not_ using it was often
better, but this has to be addressed in a separate commit.

This commit reinstates the previous `HistogramStatsIterator` detection
behavior, as PR #16702 intended to keep it.

Relevant benchmark changes with this commit (i.e. old is without using
`HistogramStatsIterator`, new is with `HistogramStatsIterator`):

name                                                                       old time/op    new time/op    delta
NativeHistograms/histogram_count_with_short_rate_interval-16                  802ms ± 3%     837ms ± 3%    +4.42%  (p=0.008 n=5+5)
NativeHistograms/histogram_count_with_long_rate_interval-16                   1.22s ± 3%     1.11s ± 1%    -9.46%  (p=0.008 n=5+5)
NativeHistogramsCustomBuckets/histogram_count_with_short_rate_interval-16     611ms ± 5%     751ms ± 6%   +22.87%  (p=0.008 n=5+5)
NativeHistogramsCustomBuckets/histogram_count_with_long_rate_interval-16      975ms ± 4%    1131ms ±11%   +16.04%  (p=0.008 n=5+5)

name                                                                       old alloc/op   new alloc/op   delta
NativeHistograms/histogram_count_with_short_rate_interval-16                  222MB ± 0%     531MB ± 0%  +139.63%  (p=0.008 n=5+5)
NativeHistograms/histogram_count_with_long_rate_interval-16                   323MB ± 0%     528MB ± 0%   +63.81%  (p=0.008 n=5+5)
NativeHistogramsCustomBuckets/histogram_count_with_short_rate_interval-16     179MB ± 0%     452MB ± 0%  +153.07%  (p=0.016 n=4+5)
NativeHistogramsCustomBuckets/histogram_count_with_long_rate_interval-16      175MB ± 0%     452MB ± 0%  +157.73%  (p=0.016 n=4+5)

name                                                                       old allocs/op  new allocs/op  delta
NativeHistograms/histogram_count_with_short_rate_interval-16                  4.48M ± 0%     8.95M ± 0%   +99.51%  (p=0.008 n=5+5)
NativeHistograms/histogram_count_with_long_rate_interval-16                   5.02M ± 0%     8.84M ± 0%   +75.89%  (p=0.008 n=5+5)
NativeHistogramsCustomBuckets/histogram_count_with_short_rate_interval-16     3.00M ± 0%     5.96M ± 0%   +98.93%  (p=0.008 n=5+5)
NativeHistogramsCustomBuckets/histogram_count_with_long_rate_interval-16      2.89M ± 0%     5.86M ± 0%  +102.69%  (p=0.016 n=4+5)

Signed-off-by: beorn7 <beorn@grafana.com>
2025-09-04 13:57:31 +02:00
Björn Rabenstein
5b153f80a5
Merge pull request #17094 from prometheus/beorn7/histogram
promql: Fix when to emit a HistogramCounterResetCollisionWarning
2025-09-04 13:55:41 +02:00
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
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
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
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
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
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
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
liangmulu
b1a7df2c0c chore: fix some minor issues in comments
Signed-off-by: liangmulu <liangmulu@outlook.com>
2025-07-09 18:05:41 +08:00
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
Michael Hoffmann
44ee5e2ad6 promql: fix topk error on NaN argument for non-existing series
Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
2025-07-07 06:19:39 +00:00
Julien Pivotto
ee7d5158a7 Add step(), min(a,b) and max(a,b) in promql duration expressions
step() is a new keyword introduced to represent the query step width in duration expressions.

min(a,b) and max(a,b) return the min and max from two duration expressions.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-07-02 11:17:17 +02:00
beorn7
ce809e625f promql: Re-introduce direct mean calculation for better accuracy
This commit brings back direct mean calculation (for `avg` and
`avg_over_time`) but isn't an outright revert of #16569. It keeps the
improved incremental mean calculation and features generally a bit
cleaner code than before.

Also, this commit...

- ...updates the lengthy comment explaining the whole situation and
  trade-offs.

- ...divides the running sum and the Kahan compensation term
  separately (in direct mean calculation) to avoid the (unlikely)
  possibility that sum and Kahan compensation together ovorflow
  float64.

- ...uncomments the tests that should now work again on darwin/arm64.

- ...uncomments the test that should now reliably yield the
  (inaccurate) value 0 on all hardware platforms. Also, the test
  description has been updated accordingly.

- ...adds avg_over_time tests for zero and one sample in the range.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-27 14:34:46 +02:00
Charles Korn
8d9dfa075d
promql: reuse histogramStatsIterator where possible, and expose it for other implementations to use (#16686)
* Expose type
* Add `Reset` method

---------

Signed-off-by: Charles Korn <charles.korn@grafana.com>
2025-06-18 23:53:46 +02:00
Björn Rabenstein
f69c83f5f1
Merge pull request #16702 from bboreham/simpler-detect
[Refactor] PromQL: Simplify detectHistogramStatsDecoding
2025-06-18 13:02:55 +02:00
Innokentii Konstantinov
94d5e0f41c
engine: fix PossibleNonCounterInfo annotation for rate and increase. (#16718)
* Comment exposed field

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

* Remove invalid test

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

* Fix PossibleNonCounterInfo annotation.

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

* lint

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>

---------

Signed-off-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>
2025-06-11 14:09:57 +01:00
Björn Rabenstein
8fc1750bcc
Merge pull request #16569 from prometheus/beorn7/promql2
promql: Simplify avg aggregation and avg_over_time
2025-06-07 00:16:20 +02:00
Bryan Boreham
91eab63d38 [Refactor] PromQL: Simplify detectHistogramStatsDecoding
Restarting the depth-first walk on each leg of a binary expression is
convoluted. ISTM the correct logic is to walk the path backwards to the
first relevant function.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2025-06-06 14:52:59 +01:00
Arthur Silva Sens
8d0a747237
PROM-39: Provide PromQL info annotations when rate()/increase() over series without __type__="counter" label (#16632)
* Provide PromQL info annotations when rate()/increase() over series without counter label

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

* Address comments

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

---------

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>
2025-06-06 10:01:20 -03:00
beorn7
ff67596a82 promql: Simplify avg aggregation and avg_over_time
As it turns out, if we combine Kahan summation and incremental mean
calculation properly, it works quite well and we do not need to switch
between simple mean calculation and incremental calculation based on
overflow.

This simplifies the code quite a bit.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-05 23:26:56 +02:00
tongjicoder
4fe20fa340 chore: fix some comments
Signed-off-by: tongjicoder <tongjicoder@icloud.com>
2025-05-27 23:14:41 +08:00
Bartlomiej Plotka
8e6b008608
feature: type-and-unit-labels (PROM-39 implementation) (#16228)
* feature: type-and-unit-labels (extended MetricIdentity)

Experimental implementation of https://github.com/prometheus/proposals/pull/39

Previous (unmerged) experiments:
* https://github.com/prometheus/prometheus/compare/main...dashpole:prometheus:type_and_unit_labels
* https://github.com/prometheus/prometheus/pull/16025

Signed-off-by: bwplotka <bwplotka@gmail.com>

feature: type-and-unit-labels (extended MetricIdentity)

Experimental implementation of https://github.com/prometheus/proposals/pull/39

Previous (unmerged) experiments:
* https://github.com/prometheus/prometheus/compare/main...dashpole:prometheus:type_and_unit_labels
* https://github.com/prometheus/prometheus/pull/16025

Signed-off-by: bwplotka <bwplotka@gmail.com>

* Fix compilation errors

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

Lint

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

Revert change made to protobuf 'Accept' header

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

Fix compilation errors for 'dedupelabels' tag

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>

* Rectored into schema.Metadata

Signed-off-by: bwplotka <bwplotka@gmail.com>

* texparse: Added tests for PromParse

Signed-off-by: bwplotka <bwplotka@gmail.com>

* add OM tests.

Signed-off-by: bwplotka <bwplotka@gmail.com>

* add proto tests

Signed-off-by: bwplotka <bwplotka@gmail.com>

* Addressed comments.

Signed-off-by: bwplotka <bwplotka@gmail.com>

* add schema label tests.

Signed-off-by: bwplotka <bwplotka@gmail.com>

* addressed comments.

Signed-off-by: bwplotka <bwplotka@gmail.com>

* fix tests.

Signed-off-by: bwplotka <bwplotka@gmail.com>

* add promql tests.

Signed-off-by: bwplotka <bwplotka@gmail.com>

* lint

Signed-off-by: bwplotka <bwplotka@gmail.com>

* Addressed comments.

Signed-off-by: bwplotka <bwplotka@gmail.com>

---------

Signed-off-by: bwplotka <bwplotka@gmail.com>
Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>
Co-authored-by: Arthur Silva Sens <arthursens2005@gmail.com>
2025-05-17 09:37:25 +00:00
Neeraj Gartia
8b0d33e5b2
promql: support variable scalar parameter in aggregations in range queries (#16404)
This fixes the regression introduced in https://github.com/prometheus/prometheus/issues/15971 while preserving the performance improvements.

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-05-11 15:40:31 +02:00
Charles Korn
2c0f02a702
promql: don't emit a value from histogram_fraction or histogram_quantile if classic and native histograms are present at the same timestamp
Signed-off-by: Charles Korn <charles.korn@grafana.com>
2025-05-05 16:23:08 +10:00
Arve Knudsen
e7e3ab2824
Fix linting issues found by golangci-lint v2.0.2 (#16368)
* Fix linting issues found by golangci-lint v2.0.2

---------

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
2025-05-03 19:05:13 +02:00
Michael Hoffmann
d6d9f97bac
promql: histogram_fraction for bucket histograms (#16095)
* promql: histogram_fraction for bucket histograms

This PR extends the histogram_fraction function to also work with classic bucket histograms. This is beneficial because it allows expressions like sum(increase(my_bucket{le="0.5"}[10m]))/sum(increase(my_total[10m])) to be written without knowing the actual values of the "le" label, easing the transition to native histograms later on.
It also feels natural since histogram_quantile also can deal with classic histograms.

Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>

* promql: histogram_fraction for bucket histograms

* Add documentation and reduce code duplication
* Fix a bug in linear interpolation between bucket boundaries
* Add more PromQL tests

Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>

* Update docs/querying/functions.md

Co-authored-by: Björn Rabenstein <github@rabenste.in>
Signed-off-by: Michael Hoffmann <mhoffm@posteo.de>

---------

Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
Signed-off-by: Michael Hoffmann <mhoffm@posteo.de>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
2025-04-23 00:28:31 +02:00
Julien
f4ca1368ff
Merge pull request #16249 from roidelapluie/arythmetics
PromQL: allow arithmetic operations in durations in PromQL parser
2025-04-22 10:17:41 +02:00
Julien Pivotto
2f6ad79edf Move calculation to the engine
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-04-03 10:56:02 +02:00
Neeraj Gartia
9084c5247f fix subquery bug
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-03-31 00:41:08 +05:30
Björn Rabenstein
7bbbb5cb97
Merge pull request #16006 from mmorel-35/revive/unused-parameter
chore: enable unused-parameter from revive
2025-02-21 13:56:04 +01:00
Charles Korn
8356990709
promql: fix double quoting in invalid label name error from count_values (#16054)
promql: fix double quoting in invalid label name error from `count_values`

---------

Signed-off-by: Charles Korn <charles.korn@grafana.com>
Signed-off-by: Charles Korn <charleskorn@users.noreply.github.com>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
2025-02-21 00:37:04 +01:00
Matthieu MOREL
c7d4b53ec1 chore: enable unused-parameter from revive
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-02-19 19:50:28 +01:00
Björn Rabenstein
906f6a33b6
Merge pull request #15987 from prometheus/beorn7/histogram
promql: Fix counter reset detection in subqueries with histograms
2025-02-11 20:41:19 +01:00
beorn7
14bb63c467 promql: Fix counter reset detection in subqueries with histograms
Signed-off-by: beorn7 <beorn@grafana.com>
2025-02-11 20:04:10 +01:00
Matthieu MOREL
b472ce7010 chore: enable early-return from revive
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-02-10 22:08:43 +01:00
Charles Korn
69ce0c24db
Fix issue
Signed-off-by: Charles Korn <charles.korn@grafana.com>
2025-02-05 11:48:47 +11:00
Neeraj Gartia
8be416a67c
[FIX] PromQL: Updates annotation for bin op between incompatible histograms (#15895)
PromQL: Updates annotation for bin op between incompatible histograms

---------

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-02-01 22:57:29 +01:00
Jan Fajerski
54cf0d6879
Merge pull request #15472 from tjhop/ref/jsonfilelogger-slog-handler
ref: JSONFileLogger slog handler, add scrape.FailureLogger interface
2025-01-27 20:17:46 +01:00
Linas Medziunas
940016e002 promql: use histogram stats decoder for histogram_avg
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2025-01-23 08:49:47 +02:00
machine424
9823a93c42
fix(main.go): avoid closing the query engine until it is guaranteed to no longer be in use.
partially reverts https://github.com/prometheus/prometheus/pull/14064

fixes https://github.com/prometheus/prometheus/issues/15232

supersedes https://github.com/prometheus/prometheus/pull/15533

reusing Engine.Close() outside of tests will require more consideration.

Signed-off-by: machine424 <ayoubmrini424@gmail.com>
2024-12-30 05:14:44 +01:00