prometheuspromqlstatsd

Prometheus rate() function for a counter incrementing by 1 per second


I've setup a simple statsd to Prometheus integration to understand how the rate() function works. I have the following script which publishes a counter value of 1 every second to statsd.

import os
import time

while True:
    os.system('echo "sample2_counter.myservice:1|c" | nc -w 1 -u 127.0.0.1 8125')
    time.sleep(1)

On PromLens, I'm trying to visualise the graph. According to my undestanding, the rate() function captures the per second average rate of increase for a particular counter. I'm getting the following graph on PromLens:

enter image description here enter image description here

I'm not able to understand this graph, why the rate() is calculated as ~0.5. My script is increment the counter by 1 each second. Shouldn't the average rate of increase come out to be close to 1 in this case? What I'm I missing here?


Solution

  • Found the issue behind the rate being halved; the netcat command itself takes roughly about a second to publish results to statsd, and we have another second of sleep. So overall, the script increments once in 2 seconds and hence the rate() shows it as 0.5.