Skip to content
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

PromQL, binary operators for range vectors #14069

Closed
Azmisov opened this issue May 9, 2024 · 4 comments
Closed

PromQL, binary operators for range vectors #14069

Azmisov opened this issue May 9, 2024 · 4 comments

Comments

@Azmisov
Copy link

Azmisov commented May 9, 2024

Proposal

AFAIK, binary operators only work on instant vectors. This is a feature request to add binary operators for range vectors as well.

Use case: I want to take the difference of two gauge metrics, and then take the moving average over 15m. Syntax could look like this in PromQL: avg_over_time(foo[15m] - bar[15m]). There is no way to compute this by eagerly converting to instant vectors first; e.g. avg_over_time(foo[15m]) - avg_over_time(bar[15m]) is not equivalent; see Simpson's paradox.

I'm imagining the range vectors the operator applies to must have the same number of points, or else it is disallowed or NaN's are given where one of the vectors has more points. Whatever is sensible given the current aggregation engine. E.g. Functionally, I would assume it behaves the same as though you were performing the operator on two instant vector time series, except this time it is repeated across many lookback ranges of a range vector.

@darshanime
Copy link
Contributor

dupe of #7263, see comment

@beorn7
Copy link
Member

beorn7 commented May 15, 2024

This example nicely illustrates why binary operators for range vectors are not as easy as it looks. foo and bar could have samples at entirely different timestamps. Any implementation of this needed to think about how to sample both series to have the same timestamps. And exactly that is already possible with subqueries:

avg_over_time((foo - bar)[15m:1m])

While @darshanime is right that binary operators for range vectors is covered by #7263, this particular use case is already supported.

@beorn7 beorn7 closed this as completed May 15, 2024
@Azmisov
Copy link
Author

Azmisov commented May 15, 2024

Okay interesting. I did try avg_over_time((foo - bar)[15m]) but I get the error expected object type in Grafana. But adding an interval explicitly seems to do the trick. Perhaps resolution is not optional after all?

@beorn7
Copy link
Member

beorn7 commented May 15, 2024

Resolution is optional, you can write avg_over_time((foo - bar)[15m:]).

What's not optional is the colon. Without the colon, you are not writing a subquery, but a range selector.

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

No branches or pull requests

3 participants