210 Commits

Author SHA1 Message Date
Linas Medziunas
5ba581748c fix(PromQL): do not skip histogram buckets when trimming
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-03-09 15:25:24 +02:00
Andrew Hall
3bda9a3396
promql: allow timestamp() to be wrapped as a step invariant depending on arguments (#17313)
Some timestamp functions can be safely wrapped as a step invariant.
Then once we do that we never need to check or unwrap at evaluation time.

For instance; `timestamp(metric @ 1)` is step invariant, whereas
`timestamp(abs(metric @ 1))` is not.

Currently all `timestamp` `*parser.Call` are excluded from being
considered step invariant since it is listed in the
`AtModifierUnsafeFunctions` map.

This PR adds an extra check for timestamp functions which consider
the arguments, and if the argument is a simple VectorSelector then
the entire `*parser.Call` can be safely wrapped as a step invariant.

Signed-off-by: Andrew Hall <andrew.hall@grafana.com>
Co-authored-by: Bryan Boreham <bjboreham@gmail.com>
2026-03-02 12:01:14 +00:00
George Krajcsovits
5d3f9ee39b
Merge pull request #17904 from linasm/trim_histogram
promql: Implement </ and >/ operators for trimming native histograms
2026-02-24 17:16:24 +01:00
Linas Medžiūnas
5bd0d00f8c
PromQL: Add experimental histogram_quantiles variadic function (#17285)
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
Signed-off-by: Björn Rabenstein <github@rabenste.in>
Signed-off-by: beorn7 <beorn@grafana.com>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
Co-authored-by: beorn7 <beorn@grafana.com>
2026-02-18 17:32:29 +01:00
Linas Medziunas
38a7befa14 Add test case with join
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-18 15:49:23 +02:00
Linas Medziunas
c753252028 Update/fix test comments
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-18 15:38:27 +02:00
zenador
8937cbd395
promql: info function: support multiple name matchers (#17968)
* Add new test cases for multiple name matchers in PromQL info function
* Fix handling of multiple name matchers in PromQL info function

---------

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
2026-02-13 17:22:58 +01:00
Linas Medziunas
86248a5ceb Fix for nhcb +Inf bucket
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-13 16:43:25 +02:00
Linas Medziunas
c657e859e3 Fix for nhcb [-Inf; -x) bucket
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-13 16:21:12 +02:00
Linas Medziunas
9a6c170d15 Fix midpoint of NHCB 1st bucket
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-13 15:39:42 +02:00
Linas Medziunas
538ad96136 Don't use extra digits in expected float values
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-13 15:39:27 +02:00
Jeanette Tan
9fddb70b8c promql: Add test case for avg_over_time single histogram fix
Add regression test for the bug where avg_over_time with a single
histogram sample would produce +Inf count/sum and NaN zero bucket
due to division by zero. The test verifies that both regular
exponential histograms and native histograms with custom buckets
(NHCB) correctly return the histogram unchanged when averaging
a single sample.

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
2026-02-11 05:06:53 +08:00
Linas Medziunas
0615b6af4f Handle zero_bucket_only case (plus more tests)
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-10 16:02:01 +02:00
Linas Medziunas
5c49187719 Fix NHCB first bucket handling
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-10 14:37:13 +02:00
Linas Medziunas
34f71ba96f Detect more noop trimmings
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-10 14:32:30 +02:00
Linas Medziunas
b3066144d1 Add test cases trimming a biased zero bucket
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-10 13:42:24 +02:00
Linas Medziunas
16c801d04e Change interpolation within zero bucket
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-09 17:40:23 +02:00
Sasha
1dcdb07d30
promql: use Kahan summation for Native Histograms (#15687)
As for float samples, Kahan summation is used for the `sum` and `avg` aggregation and for the respective `_over_time` functions.

Kahan summation is not perfect. This commit also adds tests that even Kahan summation cannot reliably pass.
These tests are commented out.

Note that the behavior might be different on other hardware platforms. We have to keep an eye on test failing on other hardware platforms and adjust them accordingly.

Signed-off-by: Aleksandr Smirnov <5targazer@mail.ru>
2026-02-08 00:52:22 +01:00
Linas Medziunas
a3a52004ba Update native_histograms.test following the feedback
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-06 13:41:51 +02:00
Julien
eaf47798af
promql: fix panic with @ modifier on empty ranges (#18020)
When using the @ modifier with a timestamp that has no data, several
PromQL range functions were panicking with "index out of range [0]
with length 0". This was introduced by #16797 which changed function
signatures to use concrete types instead of interfaces.

The panic occurred because functions were accessing array elements
(matrixVal[0], vectorVals[0][0]) without checking if the arrays were
empty first.

Fixes #18018

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2026-02-06 08:30:15 +00:00
Linas Medziunas
35aba0bbde Compute resulting sum based on surviving buckets
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-02-02 15:46:32 +02:00
Julien Pivotto
55193c3058 promql: fix smoothed interpolation across counter resets
Fix incorrect interpolation when counter resets occur in smoothed range
selector evaluation. Previously, the asymmetric handling of counter
resets (y1=0 on left edge, y2+=y1 on right edge) produced wrong values.

Now uniformly set y1=0 when a counter reset is detected, correctly
modeling the counter as starting from 0 post-reset.

This fixes rate calculations across counter resets. For example,
rate(metric[10s] smoothed) where metric goes from 100 to 10 (a reset)
now correctly computes 0.666... by treating the counter as resetting
to 0 rather than producing inflated values from the old behavior.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2026-02-02 11:54:35 +01:00
Linas Medziunas
b69ec3f39c Only track signs of surviving buckets
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-01-28 15:11:10 +02:00
Linas Medziunas
e6c07126a6 Midpoint of negative bucket must be negative
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-01-28 15:07:24 +02:00
Linas Medziunas
d0b7fe402f Edge test cases with Exponential Histogram
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-01-27 11:17:26 +02:00
Linas Medziunas
001fc1bfea Handle infinity buckets conservatively
Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2026-01-27 09:37:27 +02:00
zenador
9a49316c59
promql: info function: fix unit test for ignoring info metrics themselves (#17911)
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
2026-01-22 15:24:41 +01:00
Björn Rabenstein
82fec75982
Merge pull request #17626 from aviralgarg05/fix-promqltest-counter-reset-hint-comparison
promqltest: Add optional counter reset hint comparison for native histograms
2026-01-22 15:03:20 +01:00
sujal shah
8528d5c446 promql: Implement </ and >/ operators for trimming native histograms.
This implements the TRIM_UPPER (</) and TRIM_LOWER (>/) operators
that allow removing observations below or above a threshold from
a histogram. The implementation zeros out buckets outside the desired
range. It also recalculates the sum, including only bucket counts within
the specified threshold range.

Fixes #14651.

Signed-off-by: sujal shah <sujalshah28092004@gmail.com>
2026-01-22 15:22:47 +02:00
sujal shah
e8bfcfcf1a promql: Implement </ and >/ operators for trimming native histograms.
This implements the TRIM_UPPER (</) and TRIM_LOWER (>/) operators
that allow removing observations below or above a threshold from
a histogram. The implementation zeros out buckets outside the desired
range. It also recalculates the sum, including only bucket counts within
the specified threshold range.

Fixes #14651.

Signed-off-by: sujal shah <sujalshah28092004@gmail.com>
2026-01-22 15:22:43 +02:00
zenador
e3b6eee437
promql: info function: fix series without identifying labels not being returned (#17898)
* promql: info function: fix series without identifying labels not being returned

---------

Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
2026-01-20 16:53:27 +01:00
Julius Volz
05440ffc3b Add a few fill modifier tests with keyword-like metric names
Signed-off-by: Julius Volz <julius.volz@gmail.com>
2026-01-16 20:11:38 +01:00
Julius Volz
57dd1f18b4 Add fill modifier PromQL tests
Signed-off-by: Julius Volz <julius.volz@gmail.com>
2026-01-15 07:56:23 +01:00
Arve Knudsen
16703766f4
promql: fix info() returning empty when filtering by overlapping labels (#17817)
When filtering by a label that exists on both the input metric and
target_info (e.g., info(metric, {host_name="orbstack"}) where host_name
exists on both), the function incorrectly returned empty results.

The bug was in combineWithInfoVector: when no new labels were added
(because they all overlapped with base metric labels), the code entered
the "no match" filtering block even though an info series WAS matched.

The fix checks len(seenInfoMetrics) == 0 to correctly identify when no
info series matched. If an info series matched (seenInfoMetrics is
non-empty), the series is kept even if no new labels were added.

Fixes #17813

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
2026-01-09 10:04:37 +01:00
Julien
48e6f6a751
Merge pull request #17670 from roidelapluie/roidelapluie/promql-range
PromQL: duration expression: add range()
2025-12-26 16:31:18 +01:00
Julien Pivotto
37b97a0200 PromQL: Fix collision in unary negation with non-overlapping series.
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-12-17 11:03:55 +01:00
Julien Pivotto
b336889d8f PromQL: Fix collision in label_join and label_replace with non-overlapping series.
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-12-16 16:58:53 +01:00
Julien Pivotto
6efbb873c7 promql: Fix collision error with delayed name removal for non-overlapping series
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-12-12 14:50:29 +01:00
Julien Pivotto
d0b122a711 PromQL: duration expression: add range()
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-12-10 16:27:18 +01:00
Julius Volz
4cad87cae8 PromQL: Fix insufficient cardinality checking for filter ops
Generally, binary operations between two vectors fail if there is a many-to-one
or one-to-many matching situation between series within a match group and no
`group_left()` or `group_right()` modifier is present. For filter ops this is
also generally the case, but there can be situations where multiple series on
one side can match a single series on the other side, but only 0 or 1 of those
multiple series remains after the filter operator has been applied. In this
case, the PromQL engine does not produce a matching error, since it only tracks
series matching for those series that survive the filtering. IMO this is
incorrect behavior (which can also erratically make a query sometimes fail and
sometimes succeed, depending on current sample values), and we should always
produce an error if there is a match error prior to applying the filter op.

This PR ensures that we do the cardinality / match tracking independently of
the result of the filter operation.

Signed-off-by: Julius Volz <julius.volz@gmail.com>
2025-12-10 11:31:46 +01:00
Jan Fajerski
fae20b73ff Merge branch 'release-3.8' into sync-release-3.8
Conflicts:
	storage/remote/write_handler.go
	storage/remote/write_handler_test.go
            Pick `main`

Signed-off-by: Jan Fajerski <jfajersk@redhat.com>
2025-12-02 14:24:53 +01:00
aviralgarg05
488466246f promqltest: Fix test expectation for counter reset hint comparison
The test at line 1283 for avg_over_time(nhcb_metric[13m]) incorrectly
expected counter_reset_hint:gauge in the result. However, the actual
avg_over_time implementation does not explicitly set the CounterResetHint
to GaugeType on its output histogram.

With the new counter reset hint comparison logic added to the promqltest
framework (which compares hints when explicitly specified in expected
results), this incorrect expectation was now being caught.

This fix removes the incorrect counter_reset_hint:gauge from the expected
result, allowing the test to correctly verify the avg_over_time behavior
without asserting a specific hint value that the function does not set.

The counter reset hint comparison logic works as designed: if the expected
histogram has UnknownCounterReset (the default when not specified), no
comparison is performed. Only when a hint is explicitly specified in the
test expectation will it be compared against the actual result.

Fixes the test failure introduced by the counter reset hint comparison
feature in promqltest.

Signed-off-by: Aviral Garg <aviralg2106@gmail.com>
Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
2025-11-30 18:07:51 +05:30
zenador
c64dd612ef
PromQL: Fix bug with inconsistent results for queries with OR expression and EnableDelayedNameRemoval (#17161)
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: zenador <zenador@users.noreply.github.com>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
2025-11-15 21:07:36 +01:00
Mohammad Alavi
0f5f1955e5
promql: fix histogram_fraction issue when lower falls within the first bucket (#17424)
Signed-off-by: Mohammad Alavi <m.alavi1986@gmail.com>
2025-11-13 11:17:51 +01:00
Julien Pivotto
554ea9ebfe promql: fix resets/changes to return empty for anchored selectors when samples outside range
The funcResets and funcChanges functions now correctly return no result when all float samples are at or before the range start for anchored selectors, consistent with the behavior of rate/increase functions.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-11-05 12:07:18 +01:00
geogrego
58dbe927d5 docs: minor improvement for docs
Signed-off-by: geogrego <geogrego@outlook.com>
2025-10-29 14:42:14 +08:00
Linas Medžiūnas
20815562d2
[BUGFIX] PromQL: fix more slice indexing bugs in info function (#17199)
* [BUGFIX] PromQL: fix more slice indexing bugs in info function

---------

Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
2025-10-23 17:39:22 +02:00
zenador
87eb072c0a
Move unit tests for info function from go file to testdata (#17307)
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
2025-10-23 11:47:42 +02:00
Linas Medžiūnas
44df626620
promql (histograms): reconcile mismatched NHCB bounds (#17278)
Fixes #17255.

The implementation happens mostly in the Add and Sub method, but the reconciliation works for all relevant operations. For example, you can now `rate` over a range wherein the custom bucket boundaries are changing.

Any custom bucket reconciliation is flagged with an info-level annotation.

---------

Signed-off-by: Linas Medziunas <linas.medziunas@gmail.com>
Signed-off-by: Linas Medžiūnas <linasm@users.noreply.github.com>
2025-10-18 01:03:52 +02:00
beorn7
ff49406cba promql: Expose bug of not checking 1st histogram for CRH
avg_over_time already correctly checked the counter reset hint fo all
histograms, but in sum_over_time, the 1st histogram was missed in the
loop. This commit exposes the bug in a test.

Signed-off-by: beorn7 <beorn@grafana.com>
2025-10-14 00:52:23 +02:00