PromQL - how combine "sum_over_time" with "by"
Asked Answered
C

1

8

I'm trying to write a query that will return the following information:

for metric m1 (of type counter) - return the sum of values, grouped by (p1,p2) in a sliding window of 1h.

I think the base should be something like:

sum_over_time(m1[1h])

but this cannot be grouped by (p1,p2)

One way to do the grouping is by

sum(sum_over_time(m1[1h])) by (p1, p2) 

but i'm not sure if adding the external sum here, just in order to group, is the way to go.

Another possibility it to do:

topk(100, sum_over_time(m1[1h])) by (p1, p2) 

but in this case the grouping doesn't seem to happen right, as the results contain a lot of rows with the same (p1,p2) and different in some other labels (p3,p4)

what is the correct way to sum a metric, in a sliding window, by some lables?

Churchyard answered 14/12, 2021 at 14:18 Comment(1)
To me sum(sum_over_time(m1[1h])) by (p1, p2) makes sense :)Asthmatic
J
3

You can wrap sum_over_time into sum as a workaround and query will work smoothly.

E.g.:

topk(100, sum(sum_over_time(m1[1h])) by(p1, p2))
Jalisajalisco answered 28/3, 2022 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.