Is it possible to sum a metric by day?

21,975 views
Skip to first unread message

shane miller

unread,
Jul 8, 2018, 9:44:34 PM7/8/18
to Prometheus Developers
I've been trying to display the sum of a counter metric for each calendar day, but have had no luck so far. For example, sum up all values for the last 7 days and display one sum for each day. 
The closest I've gotten is using "increase(mycounter[1d])" and set my min step in grafana to 1d, but this seems to give me 24h behind the current time, and then 24hrs beyond that etc.... whereas I need the midnight to midnight sum. 
Is this possible in promql?


Julius Volz

unread,
Jul 9, 2018, 12:42:37 PM7/9/18
to shane miller, Prometheus Developers
PromQL is a sliding-window query engine that doesn't understand much about such types of alignments.

You'd have to solve this externally by requesting your query with the right start/end timestamps. Not sure if/how this is possible with Grafana already, but conceptually it'd belong there.

--
You received this message because you are subscribed to the Google Groups "Prometheus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-developers+unsub...@googlegroups.com.
To post to this group, send email to prometheus-developers@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-developers/e9ecf2da-50b0-4e92-9189-6e79a6088910%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alin Sînpălean

unread,
Jul 10, 2018, 3:18:32 AM7/10/18
to Prometheus Developers
You could maybe (haven't tried it) start with

    your_series and group_right day_of_week(timestamp(your_series))

to obtain separate time series containing all samples in a day, but you'd have to have a recorded rule for that in order to then do a sum_over_time() over it.

As Julius pointed out (and as you might have observed), it would be much easier to simply run the query with a start argument that's a day boundary.

Alin.

Alin Sînpălean

unread,
Jul 10, 2018, 4:20:04 AM7/10/18
to Prometheus Developers
Sorry, didn't think this through. It's actually a bit more complicated:

    your_series + ignoring(day_of_month) group_right count_values without() ("day_of_month", day_of_month(timestamp(your_series))) * 0

LOL. It basically adds a day_of_month label to your metric. You can use the same approach to add month and year labels. If you have this as a recorded rule, you can then use sum_over_time(your_recorded_rule[1y]) to get separate sums over each day (although you probably want to record an increase(your_series) for that to be of any use). Probably not worth it, though. :o)

Cheers,
Alin.

--
You received this message because you are subscribed to a topic in the Google Groups "Prometheus Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/prometheus-developers/pul4HF4OS0E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to prometheus-devel...@googlegroups.com.
To post to this group, send email to prometheus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-developers/e95bc647-4335-4b22-9534-d63d3db58d48%40googlegroups.com.

eckerstor...@gmail.com

unread,
Sep 5, 2018, 4:16:48 AM9/5/18
to Prometheus Developers
Would it be possible for you to expand on this example and show how to add year/month labels?

Alin Sînpălean

unread,
Sep 5, 2018, 5:58:15 AM9/5/18
to eckerstor...@gmail.com, Prometheus Developers
It was a bit tricky to figure out, but here you go:

up{job="prometheus"} + ignoring(year, month, day) group_right
  count_values without() ("year", year(timestamp(
    count_values without() ("month", month(timestamp(
      count_values without() ("day", day_of_month(timestamp(
        up{job="prometheus"}
      )))
    )))
  ))) * 0

Replace up{job="prometheus"} with whatever series selector you want. No idea how efficient this is, though. :o)

Cheers,
Alin.

Reply all
Reply to author
Forward
0 new messages