-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add step(), min() and max() in promql duration expressions #16777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
6253826
to
ff7572c
Compare
b549206
to
140abe4
Compare
140abe4
to
6b35e78
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this.
f0d7b27
to
73b2a12
Compare
I have updated the PR with your comments. |
Funnily enough, rate [5m x 5m] was parsed correctly. Fixed by this PR too. |
0eeced5
to
532e386
Compare
a065ef3
to
62d830c
Compare
@@ -203,6 +205,10 @@ When using offset with duration expressions, you must wrap the expression in | |||
parentheses. Without parentheses, only the first duration value will be used in | |||
the offset calculation. | |||
|
|||
Additionally, `step()` can be used in duration expressions. | |||
For a **range query**, it resolves to the step width of the range query. | |||
For an **instant query**, it resolves to `0s`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also add a line or two about where step() might be useful? it won't be useful in selecting range window sizes for rate(), cmiiw?
aside: we recommend window size as being at least 4 times as large as the scrape interval, so would it make sense to introduce a scrape_interval() function to allow: rate(my_counter[4*scrape_interval()])?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that we do not know the scrape interval for all the metrics. All jobs can be different, it can be overriden by target meta label, come from federation or recording rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but I wonder if adding min/max was intentional?
If yes, please update PR description as well.
@@ -478,7 +479,7 @@ offset_expr: expr OFFSET offset_duration_expr | |||
$$ = $1 | |||
} | |||
| expr OFFSET error | |||
{ yylex.(*parser).unexpected("offset", "number or duration"); $$ = $1 } | |||
{ yylex.(*parser).unexpected("offset", "number, duration, or step()"); $$ = $1 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ yylex.(*parser).unexpected("offset", "number, duration, or step()"); $$ = $1 } | |
{ yylex.(*parser).unexpected("offset", "number, duration or step()"); $$ = $1 } |
@@ -574,11 +575,11 @@ subquery_expr : expr LEFT_BRACKET positive_duration_expr COLON positive_durati | |||
| expr LEFT_BRACKET positive_duration_expr COLON positive_duration_expr error | |||
{ yylex.(*parser).unexpected("subquery selector", "\"]\""); $$ = $1 } | |||
| expr LEFT_BRACKET positive_duration_expr COLON error | |||
{ yylex.(*parser).unexpected("subquery selector", "number or duration or \"]\""); $$ = $1 } | |||
{ yylex.(*parser).unexpected("subquery selector", "number, duration, or step() or \"]\""); $$ = $1 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ yylex.(*parser).unexpected("subquery selector", "number, duration, or step() or \"]\""); $$ = $1 } | |
{ yylex.(*parser).unexpected("subquery selector", "number, duration or step() or \"]\""); $$ = $1 } |
| expr LEFT_BRACKET positive_duration_expr error | ||
{ yylex.(*parser).unexpected("subquery or range", "\":\" or \"]\""); $$ = $1 } | ||
| expr LEFT_BRACKET error | ||
{ yylex.(*parser).unexpected("subquery selector", "number or duration"); $$ = $1 } | ||
{ yylex.(*parser).unexpected("subquery or range selector", "number, duration, or step()"); $$ = $1 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ yylex.(*parser).unexpected("subquery or range selector", "number, duration, or step()"); $$ = $1 } | |
{ yylex.(*parser).unexpected("subquery or range selector", "number, duration or step()"); $$ = $1 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC we used the Oxford comma in Prometheus, did that change?
docs/feature_flags.md
Outdated
@@ -203,6 +205,10 @@ When using offset with duration expressions, you must wrap the expression in | |||
parentheses. Without parentheses, only the first duration value will be used in | |||
the offset calculation. | |||
|
|||
Additionally, `step()` can be used in duration expressions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR is also adding min
and max
operators, let's mention them here if it was intentional.
@@ -195,6 +195,24 @@ func TestCalculateDuration(t *testing.T) { | |||
expected: -5 * time.Second, | |||
allowedNegative: true, | |||
}, | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing tests for min, max operator.
@@ -614,6 +614,43 @@ var testExpr = []struct { | |||
fail: true, | |||
errMsg: "1:11: parse error: unexpected <ignoring>", | |||
}, | |||
// Vector selectors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing tests for min/max operator
21885d5
to
c4bdbb7
Compare
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>
c4bdbb7
to
02cca53
Compare
No description provided.