10000 Add step(), min() and max() in promql duration expressions by roidelapluie · Pull Request #16777 · prometheus/prometheus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

roidelapluie
Copy link
Member

No description provided.

@roidelapluie roidelapluie force-pushed the add-step-promql branch 5 times, most recently from 6253826 to ff7572c Compare June 26, 2025 12:00
@roidelapluie roidelapluie changed the title [WIP] Add step() in promql duration expressions Add step() in promql duration expressions Jun 26, 2025
@roidelapluie roidelapluie force-pushed the add-step-promql branch 3 times, most recently from b549206 to 140abe4 Compare June 26, 2025 12:50
@roidelapluie roidelapluie requested review from beorn7 and krajorama June 26, 2025 12:51
@roidelapluie roidelapluie marked this pull request as ready for review June 26, 2025 12:52
Copy link
Member
@beorn7 beorn7 left a 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.

@roidelapluie
Copy link
Member Author

Thanks for doing this.

I have updated the PR with your comments.

@roidelapluie
Copy link
Member Author

Funnily enough, rate [5m x 5m] was parsed correctly.

Fixed by this PR too.

@@ -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`.
Copy link
Contributor

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()])?

Copy link
Member Author

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.

Copy link
Member
@krajorama krajorama left a 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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ 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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ 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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ yylex.(*parser).unexpected("subquery or range selector", "number, duration, or step()"); $$ = $1 }
{ yylex.(*parser).unexpected("subquery or range selector", "number, duration or step()"); $$ = $1 }

Copy link
Member Author

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?

@@ -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.
Copy link
Member
< 8000 div> @krajorama krajorama Jun 30, 2025

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,
},
{
Copy link
Member

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.
Copy link
Member

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

@roidelapluie roidelapluie force-pushed the add-step-promql branch 3 times, most recently from 21885d5 to c4bdbb7 Compare June 30, 2025 14:51
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>
@roidelapluie roidelapluie changed the title Add step() in promql duration expressions Add step(), min() and max() in promql duration expressions Jun 30, 2025
@roidelapluie roidelapluie requested a review from krajorama June 30, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0