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.
What are Prometheus labels?
What is the job label in Prometheus?
How can I group labels in a Prometheus query?
It is possible to use label_replace() function in order to extract the needed parts of the label into a separate label and then group by this label when summing the results. For example, the following query extracts the project.sample-y from project.sample-y.jksdjkfs-2f16-11e7-3454-005056bf2fbf.2018.03.11 value stored in the index label and puts the extracted value into project_name label. Then the sum() is grouped by project_name label values:
sum(
label_replace(metric, "project_name", "$1", "index", "(project[.][^.]+).+")
) by (project_name)
While it'd be best to fix the metrics, the next best thing is to use metric_relabel_configs using the same technique as this blog post:
metric_relabel_configs:
- source_labels: [index]
regex: 'project\.([^.]*)\..*'
replacement: '${1}'
target_label: project
You will then have a project label that you can use as usual.