193 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
Julien Pivotto
984c8de0da PromQL: Fix printing +min()
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-07-02 11:17:17 +02:00
Julien Pivotto
3af0bdee68 PromQL: min/max/step: add more tests
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-07-02 11:17:17 +02: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
Simon Pasquier
71217a6e43
fix: prevent invalid array access in aggregate expression
This commit fixes the evaluation of invalid expressions like
`sum(rate(`. Before that, it would trigger a panic in the PromQL engine
because it tried to access an index which is out of range.

The bug was probably introduced by 06d0b063ea.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
2025-06-19 15:11:38 +02:00
Julien
998840c496
Merge pull request #16713 from roidelapluie/fix-pow-dur-expr
promql: Fix unary operator precedence in duration expressions
2025-06-16 11:13:34 +02:00
Julien Pivotto
6d56e77e0e promql: Fix unary operator precedence in duration expressions
Split unary operator handling in duration expressions into two specific
cases to fix precedence conflicts:
- Handle unary operators with number literals directly
- Handle unary operators with parenthesized expressions separately

This prevents unary minus from incorrectly binding to subsequent
operators in expressions like `foo offset -1^1`, ensuring it parses
as `(foo offset -1) ^ 1` rather than `foo offset (-1^1)`.

Fixes #16711

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-06-13 13:40:50 +02: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
Julien
1d9dfde989
Merge pull request #16041 from prymitive/parenExprEnd
Add a test for aggregation wrapped in ParenExpr
2025-05-22 09:32:17 +02:00
Julien
59874fd89c
Merge pull request #16478 from KofClubs/range-vector-1001ms
promql: function selector sometimes misses a sample on dense samples
2025-05-08 12:02:10 +02:00
György Krajcsovits
06d0b063ea fix(parser): parenthesis around aggregate expression parsing
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))".

The fix is to track ")" tokens in a stack in addition to the lastClosing
position, which is overloaded with other things like offset number tracking.


Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2025-05-07 11:17:39 +01:00
Lukasz Mierzwa
ba690a8c4c Add a test for aggregation wrapped in ParenExpr
Signed-off-by: Lukasz Mierzwa <l.mierzwa@gmail.com>
2025-05-07 11:17:24 +01: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
Zhang Zhanpeng
0658923bf0 fix conversion from time range in seconds to time duration at parser
Signed-off-by: Zhang Zhanpeng <zhangzhanpeng.zzp@alibaba-inc.com>
2025-04-24 16:29:33 +08: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
Neeraj Gartia
f83e964231 fix NH load expression with incrementation more than once
Signed-off-by: Neeraj Gartia <neerajgartia211002@gmail.com>
2025-04-05 23:37:39 +05:30
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
Julien Pivotto
9cfd6f7df8 PromQL duration expr: Add a check for durations our of range
Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-04-03 10:56:02 +02:00
Julien Pivotto
7370d62acf PromQL: allow arithmetic in durations in PromQL parser
Updated the parser to allow calculations in PromQL durations.

This enables durations in the form of:

  rate(http_requests_total[10m+2s])

The computation of the calculations is done directly at the parse level and does not hit the PromQL Engine.
The lexer has also been updated and improved, in particular for subqueries.

Buxfix: rate(http_requests_total[0]) is no longer allowed.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
2025-04-03 10:56:02 +02:00
Matthieu MOREL
5fa1146e21
chore: enable gci linter (#16245)
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-03-22 15:46:13 +00:00
Owen Williams
94b43c5d4c utf8: Remove support for legacy global validation setting
Global and Data Source configurations can specify legacy mode, but Prometheus now requires that the overall validation mode be set to UTF-8

Signed-off-by: Owen Williams <owen.williams@grafana.com>
2025-03-13 10:47:24 -04:00
Arve Knudsen
56929ffa42 Upgrade to Go v1.24 (#16180)
* Upgrade to Go v1.24
* Upgrade golangci-lint

---------

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
2025-03-07 11:28:26 +01:00
Giuliano Panzironi
2ef8706c27
fix: format float to avoid overflow (#16083)
Signed-off-by: giulianopz <panzironi.giuliano@gmail.com>
2025-03-03 09:03:45 +00:00
Mikel Olasagasti Uranga
9258e40589
parser: fix non-constant format string call (#15835)
Go 1.24 enhanced vet's printf analyzer to report calls of the form
fmt.Printf(s), where s is a non-constant format string, with no other
arguments. This change makes parser tests to fail.

Signed-off-by: Mikel Olasagasti Uranga <mikel@olasagasti.info>
2025-01-18 13:21:51 +01:00
Owen Williams
47563d942e parser: fix misleading error message in grouping processing
Signed-off-by: Owen Williams <owen.williams@grafana.com>
2025-01-10 15:23:33 -05:00
Owen Williams
8d4bcd2c77 promql: Fix various UTF-8 bugs related to quoting
Fixes UTF-8 aggregator label list items getting mutated with quote marks when String-ified.
Fixes quoted metric names not supported in metric declarations.
Fixes UTF-8 label names not being quoted when String-ified.

Fixes https://github.com/prometheus/prometheus/issues/15470
Fixes https://github.com/prometheus/prometheus/issues/15528

Signed-off-by: Owen Williams <owen.williams@grafana.com>
Co-authored-by: Bryan Boreham <bjboreham@gmail.com>
2024-12-04 14:18:59 -05:00
TJ Hoplock
aa8e067f13 Merge release-3.0 into main 2024-11-27 17:51:51 -05:00
newtonne
88675710f9 Add support for utf8 names on /v1/label/:name/values endpoint
Previously, the api was evaluating this regex to determine if the label
name was valid or not:

14bac55a99/model/labels.go (L94)

However, I believe that the `IsValid()` function is what ought to be
used in the brave new utf8 era.

**Before**

```
$ curl localhost:9090/api/v1/label/host.name/values
{"status":"error","errorType":"bad_data","error":"invalid label name: \"host.name\""}
```

**After**

```
$ curl localhost:9090/api/v1/label/host.name/values
{"status":"success","data":["localhost"]}
```

It's very likely that I'm missing something here or you were already
planning to do this at some point but I just encountered this issue and
figured I'd give it a go.

Signed-off-by: Owen Williams <owen.williams@grafana.com>
2024-11-25 11:48:45 -05:00
zenador
e13c28bd4a
Use a function to determine if an aggregation function is experimental (#15331)
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
2024-11-15 15:09:50 +01: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
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
Matthieu MOREL
ab64966e9d
fix: use "ErrorContains" or "EqualError" instead of "Contains(t, err.Error()" and "Equal(t, err.Error()" (#15094)
* fix: use "ErrorContains" or "EqualError" instead of "Contains(t, err.Error()" and "Equal(t, err.Error()"

---------

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
2024-10-06 16:35:29 +00:00
Björn Rabenstein
5b9148e552
Merge pull request #14820 from charleskorn/promqltest-native-histogram-format
promqltest: use test expression format for histograms in assertion failure messages and include reset hint in the test expression
2024-09-20 16:47:08 +02:00
Jan Fajerski
96e5a94d29 promql: rename holt_winters to double_exponential_smoothing
Signed-off-by: Jan Fajerski <jfajersk@redhat.com>
2024-09-19 15:29:01 +02:00
Björn Rabenstein
1639450172 Merge pull request #14821 from charleskorn/nh-negative-multiplication-division
promql: correctly handle unary negation of native histograms and add tests for multiplication and division of native histograms by negative scalars
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-09-19 14:07:37 +01:00