Commit Graph

249 Commits

Author SHA1 Message Date
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
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
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
beorn7
bcf7a822a0 promql: Prevent extrapolation below zero for histogram count
This deals with the count field of native histograms in the same way
as with simple float counters. It then scale the whole histogram with
the same factor as it has scaled the count. This will still allow
individual buckets to get extrapolated below zero, but maybe that is
fine.

This implements approach (2) as described in
https://github.com/prometheus/prometheus/issues/15976#issuecomment-3032095158

Signed-off-by: beorn7 <beorn@grafana.com>
2025-07-08 19:01:31 +02:00
Björn Rabenstein
9e73fb43b3
Merge pull request #16773 from prometheus/beorn7/promql
promql: Re-introduce direct mean calculation
2025-06-27 14:57:12 +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
George Krajcsovits
5b7ff92d95
fix(promql): histogram_quantile and histogram_fraction NaN observed in native histogram (#16724)
* fix(promql): histogram_quantile NaN observed in native histogram

Fixes: #16578

See the issue for detailed explanation.
When a histogram had only NaN observations and no normal observations,
we returned 0 from the quantile, which is completely wrong. If there were
normal observations but we went over them, we returned the upper bound of
the existing buckets, however that contradicts expectations on
histogram_fraction. Now we return NaN if the quantile is calculated to be
over all normal observations, falling into NaNs (in a virtual +Inf bucket).

We also return info level annotations if we see any NaN observations.
The annotation calls out if we returned NaN or even if we took the
virtual +Inf bucket into account.

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>

* fix(promql): histogram_fraction NaN observed in native histogram

Fixes: #16580

According to the specification we should not take NaN observations
into account when calculating the fraction. This commit fixes that
and adds an info level annotation to let the user know about this.

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-06-25 13:37:43 +02:00
🌲 Harry 🌊 John 🏔
59bf3d442b PromQL: Fix native histogram last_over_time with offset
Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com>
2025-06-17 10:49:06 -07:00
Michael Hoffmann
ec98e7f069 promql: fix ts_of_last_over_time for histogram vectors
This PR fixes a bug in ts_of_last_over_time where the float samples
where used when computing the last timestamp of the histogram samples.

Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
2025-06-16 13:09:49 +00:00
Michael Hoffmann
a5fa9431d8 promql: add ts_of_(max,min,last)_over_time functions
This commit adds the ts_of_(max,min,last)_over_time functions behind the experimental feature flag.

Signed-off-by: Michael Hoffmann <mhoffmann@cloudflare.com>
2025-06-12 15:03:28 +00:00
beorn7
68b1bcc648 promql: typo fix in comment
Signed-off-by: beorn7 <beorn@grafana.com>
2025-06-10 22:06:04 +02: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
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
591242901a
promql: Refactor some functions to make them more DRY (#16532)
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-05-11 15:16:15 +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
Aman
26bddcf068
promql: histogram_stddev and histogram_stdvar should use arithmetic mean for custom buckets
Signed-off-by: amanycodes <amanycodes@gmail.com>
2025-04-24 14:48:58 +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
Neeraj Gartia
a511164615
[REFACTOR] PromQL: DRY stddev and stdvar functions (#16451)
* DRY stddev and stdvar

---------

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-04-20 11:17:03 +02:00
Charles Korn
c88d0b0e0a
promql: return NaN from irate() if second-last sample is NaN (#16199)
promql: return NaN from `irate()` if either of last two samples is NaN

Signed-off-by: Charles Korn <charles.korn@grafana.com>

---------

Signed-off-by: Charles Korn <charles.korn@grafana.com>
2025-03-13 15:06:17 +01:00
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
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
Charles Korn
0964b6e584
promql: emit correct annotation in quantile_over_time when run over a range with histograms and floats
Signed-off-by: Charles Korn <charles.korn@grafana.com>
2025-02-12 16:46:57 +11:00
Neeraj Gartia
20371118b6
[FIX] PromQL: Ignore histograms in scalar, sort and sort_desc functions (#15964)
PromQL: Ignore histograms in scalar, sort and sort_desc functions

---------

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-02-06 16:02:03 +01:00
Björn Rabenstein
cb096a8ea8
Merge pull request #15902 from prometheus/beorn7/promql
promql: fix rate calculation with a counter reset after the 1st sample
2025-02-05 22:10:45 +01:00
Björn Rabenstein
fa1bd02c99
Merge pull request #15974 from jhesketh/jhesketh/utf-label-replace
Allow UTF-8 labels in label_replace
2025-02-05 20:49:36 +01:00
Joshua Hesketh
5a5fdea7ad
Fix duplicate output vector if delayed name removal is disabled (#15975)
Fix duplicate output vector if delayed name removal is disabled

This error is emitted in cleanupMetricLabels, but is skipped if
enableDelayedNameRemoval is false.

This makes it consistent with label_replace

Signed-off-by: Joshua Hesketh <josh@nitrotech.org>

---------

Signed-off-by: Joshua Hesketh <josh@nitrotech.org>
Signed-off-by: Björn Rabenstein <github@rabenste.in>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
2025-02-05 16:40:23 +01:00
Joshua Hesketh
e4037b3ec3 Allow UTF-8 labels in label_replace
This makes it consistent with label_join.

Signed-off-by: Joshua Hesketh <josh@nitrotech.org>
2025-02-05 22:24:13 +11:00
Neeraj Gartia
130cd024e0
[FEATURE] PromQL: Implements idelta and irate with histograms (#15853)
Add native histogram support to idelta and irate functions

---------

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-01-31 23:11:03 +01:00
beorn7
2581c7d057 promql: fix rate calculation with a counter reset after the 1st histogram
If a rate (or increase) is calculated on native histograms, and there
is a counter reset between the 1st and 2nd histogram, we never have to
touch the 1st histogram, so it doesn't even matter if it has an
incompatible bucket layout. So we should not error out in that case.

This simply nulls out the 1st histogram in that case.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-01-29 15:42:47 +01:00
Joshua Hesketh
665d1ba0cc
Ensure metric name is present on histogram_quantile annotations (#15828)
Ensure metric name is present on histogram_quantile annotations

Previously the series __name__ label was dropped before the annotation
was emitted causing the annotation message to have a blank value.

This fix also allows delayed name removal for classic histograms.

This also adds a test for malformed le label

This annotation could be split into two to be clearer for the user
(one for missing, and one for malformed). This is outside of the scope
of this change.

Fixes: #15411

---------

Signed-off-by: Joshua Hesketh <josh@nitrotech.org>
Signed-off-by: Björn Rabenstein <github@rabenste.in>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
2025-01-22 14:35:43 +01:00
Neeraj Gartia
b3e30d52ce
[BUGFIX] PromQL: Fix <aggr_over_time> functions with histograms (#15711)
fix aggr_over_time with histograms

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>

---------

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-01-09 16:38:42 +01:00
Neeraj Gartia
0e99ca3e8c
[BUGFIX] PromQL: Fix deriv, predict_linear and double_exponential_smoothing with histograms (#15686)
PromQL: Fix deriv, predict_linear and double_exponential_smoothing with histograms

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>

---------

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2024-12-19 23:50:28 +01:00
beorn7
ebfa1dd822 promql: Purge Holt-Winters from a doc comment
`funcDoubleExponentialSmoothing` did not get its doc comment updated
when we renamed it from the confusing `funcHoltWinters`.

Signed-off-by: beorn7 <beorn@grafana.com>
2024-12-11 12:03:19 +01:00
Neeraj Gartia
4aeed2c4b1 fix resets function for histograms
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2024-12-03 19:01:05 +05:30
Björn Rabenstein
dfbfe8b5e8
Merge pull request #15479 from NeerajGartia21/promql/datewrapper
[BUGFIX] PromQL: Ignore histograms in all time related functions
2024-11-29 00:26:52 +01:00
Neeraj Gartia
0de340c1b4 ignore histogram in dateWrapper
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2024-11-28 01:57:04 +05:30
Neeraj Gartia
36e0897f0f
[BUGFIX] PromQL: Fix behaviour of changes() for mix of histograms and floats (#15469)
PromQL: Fix behaviour of changes() for mix of histograms and floats

---------

Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2024-11-27 21:05:08 +01:00
Joshua Hesketh
8e3301eb44
Export quantile functions (#15190)
Export quantile functions

For use in Mimir's query engine, it would be helpful if these
functions were exported.

Co-authored-by: Björn Rabenstein <github@rabenste.in>
Signed-off-by: Joshua Hesketh <josh@hesketh.net.au>

---------

Signed-off-by: Joshua Hesketh <josh@nitrotech.org>
Signed-off-by: Joshua Hesketh <josh@hesketh.net.au>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
2024-11-27 13:20:23 +01:00
Neeraj Gartia
abfc3145a2
apply DRY on clamp (#15441)
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2024-11-24 17:08:59 +01:00
Joshua Hesketh
ed2668bbda
Merge branch 'main' into jhesketh/clamp
Signed-off-by: Joshua Hesketh <josh@nitrotech.org>
2024-11-12 10:20:58 +11:00
Bryan Boreham
6979b237b9 Merge branch 'release-2.55' into merge-2.55-into-main2 2024-11-04 12:58:38 +00:00
Ben Ye
b7aca45de7 fix round function ignoring enableDelayedNameRemoval feature flag
Signed-off-by: Ben Ye <benye@amazon.com>
2024-10-31 00:30:22 -07:00
Jan Fajerski
38fd48e6b5 v2.55.0
-----BEGIN SSH SIGNATURE-----
 U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgX42TrpDUXJbbi9yZ3hs6cDg+kz
 G6d3nAlAb2XQInrEgAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
 AAAAQGoSEKIFT/BfavtG2qW9n7NYonNQk/9r6gCLvxln9elt1hiY0ZGcwRhm1QNx6FotxJ
 Y3LB9dt4s5akB3fOPkYwc=
 -----END SSH SIGNATURE-----

Merge tag 'v2.55.0' into release-3.0.0-rc.0

v2.55.0
2024-10-25 14:16:22 +02:00
Björn Rabenstein
7c7116fea8
Merge pull request #15176 from jhesketh/jhesketh/round
Round function should ignore native histograms
2024-10-22 19:14:16 +02:00
Joshua Hesketh
14ef1ce8ab Round function should ignore native histograms
As per the documentation, native histograms are skipped. This is in line with other simpleFunc's.

Signed-off-by: Joshua Hesketh <josh@nitrotech.org>
2024-10-17 15:39:48 +11:00
Bryan Boreham
a846bf9a5e Merge branch 'release-2.55' into merge-2.55-into-main-3 2024-10-16 14:08:54 +01:00
Arve Knudsen
de16f5e387
[FEATURE] PromQL: Add experimental info function MVP (#14495)
The `info` function is an experiment to improve UX
around including labels from info metrics.
`info` has to be enabled via the feature flag `--enable-feature=promql-experimental-functions`.

This MVP of info simplifies the implementation by assuming:
* Only support for the target_info metric
* That target_info's identifying labels are job and instance

Also:
* Encode info samples' original timestamp as sample value
* Deduce info series select hints from top-most VectorSelector

---------

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Co-authored-by: Ying WANG <ying.wang@grafana.com>
Co-authored-by: Augustin Husson <augustin.husson@amadeus.com>
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
Co-authored-by: Bryan Boreham <bjboreham@gmail.com>
2024-10-16 13:52:11 +01:00
Joshua Hesketh
31d19381f6 Clamp functions should ignore native histograms
As per the documentation, native histograms are skipped. This is in line
with other `simpleFunc`'s.

Signed-off-by: Joshua Hesketh <josh@nitrotech.org>
2024-10-16 15:10:54 +11:00
Bryan Boreham
b8e5b7cda9 [REFACTOR] PromQL: remove label_join and label_replace stubs
These functions operate on whole series, not on samples, so they do not
fit into the table of functions that return a Vector. Remove the stub
entries that were left to help downstream users of the code identify
what changed.

We cannot remove the entries from the `FunctionCalls` map without
breaking `TestFunctionList`, so put some nils in to keep it happy.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-09-27 11:20:45 +01:00
Bryan Boreham
7f99d2930d [BUGFIX] PromQL: make sort_by_label stable
Go's sorting functions can re-order equal elements, so the strategy of
sorting by the fallback ordering first does not always work.
Pulling the fallback into the main comparison function is more reliable
and more efficient.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-09-26 11:12:33 +01:00