1

I have a Prometheus gauge metric x that models a rate per second. My goal is to create a Grafana dashboard that shows a bar chart for this rate and is generic over various levels of aggregation such as "average rate over the last 60min".

For a fixed duration such as 60min, this is straightforward. We take the average in Prometheus and multiple with the number of seconds in the time window:

avg_over_time( x[60m] ) * 60 * 60

With this fixed query, it is also easy to create a Grafana dashboard that shows a bar chart with one 'bar' per hour (setting 'Min step' to 60m) whose magnitude is defined by the query above.

I want to make this visualization generic over the time span over which the mean value is taken. To this end, I have defined a "duration" variable $duration in Grafana. However, I am stuck on how to formulate the prometheus query:

avg_over_time( x[$duration] ) * ???

Since $duration is not a number but a string such as 60m, I can't simply take $duration * 60 here. How can I convert the contents of $duration to a number of seconds?

Thank you!

dassmann
  • 327
  • 1
  • 6
  • 13
  • Is this a metric that you're exposing yourself? Asking because if you exposed a COUNTER as opposed to a GAUGE, this would be a very trivial question to answer. Instead of the metric containing the actual rate, it should just contain the total number of requests. Then you can rate(metric[$__rate_interval]) for the RPS in any given duration. – Jacob Colvin Nov 21 '20 at 20:42

0 Answers0