Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Using $__range in promql queries

When using PromQL in cloud monitoring dashboard, I can see that the variable $__interval is available (and often automatically used by google cloud). But is there a way to get the time duration for the entire range in question? Grafana has __range, but i can't get that to work. 

I'm trying to create a metic that calculates sum over the period of time selected. I can put in a constant (like 24h), but then it doesn't change as the range changes. 

5 5 2,948
5 REPLIES 5

Hello @halfdanj,

Welcome to Google Cloud Community!

You're right, PromQL in Cloud Monitoring doesn't have a direct equivalent to Grafana's __range variable. However, to achieve your goal of calculating the sum over the selected time period in your metric, you may use Rate over Interval.

This approach utilizes the rate function along with the provided $__interval variable in PromQL:

sum(rate(<your_metric>[$__interval]))
  • rate(<your_metric>[$__interval]): This calculates the rate of change of your metric over the current $__interval.
  • sum(): This sums the rate values over the entire time range displayed in the dashboard (which is implicitly available in PromQL).

 

I'm unable to get that working. This is what i'm using at the moment : 

 

sum by (workflow_type,project_id)(increase(temporal_workflow_completed[${__interval}]))
 
 
Right now my time range is set to 12 hours, but ${__interval} resolves to i believe 60sec. So i only get the sum of the last 60 seconds instead of 12 hours. If i replace ${__interval} with "12h" then it correctly calculates the complete sum. 
 
I use increase instead of rate, because i'm trying to sum a "count" metric. But i have same issue if I do rate. 
 

Have you tried the $__timeFiltervariable? It doesn't provide the entire duration but it allows you to construct a dynamic time range based on your selection.

Hmm I'm not able to use it. Tried a few formattings, but always getting ": parse error: missing unit character in duration" . 

same error trying to use $__interval  "parse error: missing unit character in duration" 

edit: [${__interval}] is the correct syntax ,  it replaces it with 3600s however.
Top Solution Authors