max_over_time - start of month

1,409 views
Skip to first unread message

fiala...@gmail.com

unread,
Oct 5, 2022, 4:27:50 AM10/5/22
to Prometheus Users
Hi,

It is possible to get maximum value in current month?

My query is:
round((max_over_time(rack_max_power_watts{job="Max power on path /racks/max-power"}[4w])

Thank you.

Julius Volz

unread,
Oct 5, 2022, 8:10:05 AM10/5/22
to fiala...@gmail.com, Prometheus Users
Hi,

When you say "current month", I guess you care about the time window starting exactly at the beginning of the calendar month, not an arbitrary 4 weeks ago?

In that case, you would have to manually choose the time window just large enough (relative to the overall query evaluation timestamp) to only go back exactly to that time.

There's also a complex construct you could write using a subquery to select a rack power output only for those timestamps within the subquery that fall into a given month, but I'm not sure you want to go that far, and it introduces other potential issues, such as skipping over raw underlying values in case you don't choose the subquery resolution high enough. It's also more expensive.

In any case, I'm thinking of something like:

round(
  max_over_time(
    (

        rack_max_power_watts{job="Max power on path /racks/max-power"}
      and on()  # Filter rack power watt values down to only those from October
        month() == 10
    )[5w:]
  )
)

...where 10 is the current month (October) and the 5w window is chosen generously to always cover at least a full month going backward from now.

Cheers,
Julius

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/11446946-70b1-4d49-a411-8f0cc8df735en%40googlegroups.com.


--
Julius Volz
PromLabs - promlabs.com

Brian Candler

unread,
Oct 5, 2022, 9:11:06 AM10/5/22
to Prometheus Users
Another option is to use the @ modifier to specify the exact execution time of the query, giving the end of the month as the timestamp.  This is now available by default in recent versions of prometheus (v.2.33.0+), but before that it was hidden behind a feature flag.

    max_over_time(rack_max_power_watts{job="Max power on path /racks/max-power"}[4w] @ 1667260800)    # up to midnight on Nov 1st

However, it's still up to you to calculate the execution time before running the query.  It won't work out "the current month" for you.  Also, a month isn't exactly 4 weeks; for October you'd want

    max_over_time(rack_max_power_watts{job="Max power on path /racks/max-power"}[31d] @ 1667260800)

fiala...@gmail.com

unread,
Oct 5, 2022, 9:32:22 AM10/5/22
to Prometheus Users
Thank you for tips but I'm still not sure, how to handle this.

I'm using my query in grafana where I want to display table with highest power usage in current calendar month. So I'm not able to calculate constants like 10 (October), or timestamps and I dont want to change it every month.

Brian Candler

unread,
Oct 5, 2022, 10:00:03 AM10/5/22
to Prometheus Users
Then I think that's a feature request for Grafana.  Prometheus is just the data source; the client (Grafana in this case) is responsible both for issuing the correct query and visualising the results.

Julius Volz

unread,
Oct 5, 2022, 11:59:06 AM10/5/22
to Brian Candler, Prometheus Users
Well, Grafana has a "${__to:date:seconds}" global template variable (see https://grafana.com/docs/grafana/v9.0/variables/variable-types/global-variables/#__from-and-__to) that you could use to derive the current month from. At least current with respect to the end of the configured graph range, which is what I hope you'd want? So:

  month() == month(${__to:date:seconds})

I haven't tried that out though.

Brian Candler

unread,
Oct 5, 2022, 12:11:11 PM10/5/22
to Prometheus Users
Interesting. AFACIS, $__from and $__to are the start and end time range of the currently displayed dashboard.  So ${__to:date:MM} might do the trick, although it would only be the "current" month if the selected time period ends in the current month (which I guess it normally would; and it might be useful to be able to look at this value historically anyway).

Alternatively, you could just do this:

    max_over_time(rack_max_power_watts{job="Max power on path /racks/max-power"}[{$__range_s}s])

and then you can get this value calculated over any particular time range the user wants, by selecting any start and end time for the dashboard.

Marek Fiala

unread,
Oct 6, 2022, 8:11:03 AM10/6/22
to Brian Candler, Prometheus Users
Thank you all!

"and on() month() == ${__to:date:MM}" solve my problem.

Best regards
Marek Fiala

You received this message because you are subscribed to a topic in the Google Groups "Prometheus Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/prometheus-users/6cxqUGoxZ-Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/293b8e78-5fb2-4675-9a45-688adc10edc2n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages