It's even easier
sum by (group) (my_metric)
Answer from uamanager on Stack OverflowIt's even easier
sum by (group) (my_metric)
Yes, you can you use label replace to group all the misc together:
sum by (new_group) (
label_replace(
label_replace(my_metric, "new_group", "$1", "group", ".+"),
"new_group", "misc", "group", "misc group.+"
)
)
The inner label_replace copies all values from group into new_group, the outer overwrites those which match "misc group.+" with "misc", and we then sum by the "new_group" label. The reason for using a new label is the series would no longer be unique if we just overwrote the "group" label, and the sum wouldn't work.
Sum values grup by instance
Division of a grouped sum in prometheus - Stack Overflow
Group data by year/month/day in prometheus - Stack Overflow
Group by option in query for prometheus
What is the difference between group by and sum by in Prometheus?
How can I group labels in a Prometheus query?
How do I use the "group by" function with labels in Prometheus queries?
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)
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]$"
)