Average of a Summary/Histogram

If you have a Summary or Histogram how do you get the average latency/bytes/whatever?

Let's say you have these timeseries:

http_server_request_duration_seconds_count{job="somejob"}
http_server_request_duration_seconds_sum{job="somejob"}

Then this will get the 5m average:

  rate(http_server_request_duration_seconds_sum{job="somejob"}[5m])
/
  rate(http_server_request_duration_seconds_count{job="somejob"}[5m])

If you're writing this as a recording rule, do:

instance:http_server_request_duration_seconds:avg5m = 
    rate(http_server_request_duration_seconds_sum{job="somejob"}[5m])
  /
    rate(http_server_request_duration_seconds_count{job="somejob"}[5m])

And to aggregate:

job:http_server_request_duration_seconds:avg5m = 
    sum without (instance) (rate(http_server_request_duration_seconds_sum{job="somejob"}[5m]))
  /
    sum without (instance) (rate(http_server_request_duration_seconds_count{job="somejob"}[5m]))

Rate this tip

If you think this tip by bbrazilbbrazil is useful — rate it up!

rating: +1+x
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License