Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group data by year/month/day in prometheus

Tags:

prometheus

As the title says, I am looking for a way to get the maximum value of a time series, which resets daily. After this task is achieved I would like to create a sum over those values to get a value per month/year. What is the best way to achieve this in Prometheus?

like image 679
Eggi Avatar asked Oct 27 '25 09:10

Eggi


2 Answers

As described in my post on prometheus-developers, here is a possible option, although it's far from readable:

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 both instances of up{job="prometheus"} with whatever series selector you need. No idea how efficient this is, though. :o)

like image 135
Alin Sînpălean Avatar answered Oct 30 '25 07:10

Alin Sînpălean


I just wanted to add to the excellent answer by Alin Sînpălean. He splits the data into time series per day. You can similarly split them per month and then use the Grafana reduce "series to rows" transform to calculate the total per month. You need another sort transform to get the rows back in order. This only works well when you add a leading zero to the month number. Then you can use "{{year}}-{{zero}}{{month}}" as your legend which sorts correctly.

label_replace(
  up{job="prometheus"} + ignoring(year, month) group_right
    count_values without() ("year", year(timestamp(
      count_values without() ("month", month(timestamp(
        up{job="prometheus"}
      )))
    ))) * 0,
  "zero",
  "0",
  "month",
  "^[0-9]$"
)
like image 40
RichieB Avatar answered Oct 30 '25 08:10

RichieB