Search code examples
performanceprometheuspromql

How to calculate average by labels grouping with prometheus histograms?


I have batched DB queries in my program and would like to measure the average time for batched queries to finish based on batch size, currently I have

rate(batched_query_duration_seconds_sum{job="myprogram"}[5m]) / rate(batched_query_duration_seconds_count{job="myprogram"}[5m])

that metric has the label batch_size, I think the correct query would be something like

rate by (batch_size) (batched_query_duration_seconds_sum{job="myprogram"}[5m]) / rate by (batch_size) (batched_query_duration_seconds_count{job="myprogram"}[5m])

but it's syntax error. How can I do that? Thanks.


Solution

  • Try the following query:

    sum(rate(batched_query_duration_seconds_sum{job="myprogram"}[5m])) by (batch_size) / sum(rate(batched_query_duration_seconds_count{job="myprogram"}[5m])) by (batch_size)