Quote from Prometheus Count and sum of observations doc:
To calculate the average request duration during the last 5 minutes from a histogram or summary called http_request_duration_seconds, use the following expression:
rate(http_request_duration_seconds_sum[5m]) / rate(http_request_duration_seconds_count[5m])
I should mention that I understand:
rate
function doesHowever I'm not interested in the increase rate of request duration, but rather in the request duration itself!
Can someone explain why everybody while looking for average count/value in any given moment of time has to use a rate
function, when it doesn't provide that?
P.S. there's seemingly a duplicate question with a checked answer, however all answers in it explain what rate
function is, how it does what it does, etc. I already understand what rate
function does. I just don't understand why we are supposed to use it in first place, especially when the result that it provides has nothing to do with what we're looking for.
Let's show that the formula quoted from the Prometheus manual, making use of the function named rate()
, computes the exact value you are looking for.
According to the way a counter works, we know that each time the counter named http_request_duration_seconds_sum
takes into account a new value, that is the sum of durations of all the requests that happened from the last time, it adds this sum to its previous value.
Therefore, rate(http_request_duration_seconds_sum[5m])
is the sum of the durations of the requests that occurred during 5 minutes, divided by 5 minutes.
And each time the counter http_request_duration_seconds_count
takes into account a new value, that is the number of requests that happened from the last time, this counter adds this number of requests to its previous value.
Therefore, rate(http_request_duration_seconds_count[5m])
is the number of requests that occurred during 5 minutes, divided by 5 minutes.
So, let's inject the formulas discovered in the two previous paragraphs into the following fraction:
equals to:
You can simplify this formula by removing 5 minutes
, because it is present in the numerator and in the denominator.
Finally, the following formula:
is equal to the following one:
The second part of this equality is the value you want to compute: the average duration of requests during 5 minutes. This is why it is computed using the first part of this equality.