I've got the following histogram metric db_query_duration_ms_sum
(it's histogram so I've got _sum
, _count
and _bucket
).
db_query_duration_ms_sum
has a param query_name
which is the name of a db query.
How do I create a bar gauge which displays the average duration per query name? Something like:
As you can see, I'm pretty close. I'm trying to calculate it using:
sum(rate(db_query_duration_ms_sum{}[$__rate_interval])) by (query_name) /
sum(rate(db_query_duration_ms_count{}[$__rate_interval])) by (query_name)
However, there are 2 issues:
NaN
. Within the bar gauge grafana options, I've selected "Value options/Calculation" to be Last (last non-null value)
but that doesn't seem to make a difference. I want to display the average duration per each query within the selected timeframe.sort_desc
but yet again, that doesn't do anything. How do I sort the rows?I'm not sure how you are getting Nan
s in your data, but to avoid Inf
s (due to number of requests being 0), you can use this query:
sum(increase(db_query_duration_ms_sum [$__range])) by (query_name)
/ sum(increase(db_query_duration_ms_count[$__range])) by (query_name) < +Inf
or sum(increase(db_query_duration_ms_count[$__range])) by (query_name)
It filters out values of infinity, and if the appear - replaces them with zeros.
And if you don't want zeros and prefer them to be missing - omit everything beginning with or
.
Based on OP's report, same query filters out Nan
s too. But since I cannot explain why it happened in the first place, I cannot explain how it helped too.