Uri Hellerman
Uri Hellerman

Reputation: 33

Prometheus offset in seconds since midnight

I have a counter metric of api calls. I would like to check how many calls were made TODAY (i.e since midnight). my initial though was to use the delta function which seems to fit, but I cannot figure out how do I calculate the offset from time() to midnight

Upvotes: 2

Views: 3338

Answers (3)

thoredge
thoredge

Reputation: 12631

Inspired by @brian-brazil's solution here I was able to do this by creating a query variable. Go to dashboard Settings (⚙) and Variables.

I created a query variable seconds_since_midnight like this query_result(${__to:date:seconds} % 86400 + 3600). Notice that I added 3600 to account for being one hour ahead of GMT.

This variable can then be used in ranges like this:

blocking_time_nanos[${seconds_since_midnight}s]

... or even in query time shifts in order to timeshift to yesterday.

OBS: There is a problem getting this variable updated. You can choose between On dashboard load or On time range change. The best would of course had been On demand :-(. However it is not a problem as long as you are using a moving time window like for example Last 24 hours. It seems to count as a time range change.

Upvotes: 0

eventhorizon
eventhorizon

Reputation: 3377

When you are using grafana, you can tryout something like this: delta(your_counter[${__range_s}s]). There are several special variables in grafana: https://grafana.com/docs/grafana/latest/variables/variable-types/global-variables/

Upvotes: 2

brian-brazil
brian-brazil

Reputation: 34172

time() % 86400 will give the number of seconds since midnight UTC.

Upvotes: 3

Related Questions