简体   繁体   中英

Prometheus: PromQL - Adding a label to the metric

I want to calculate sum of the metric my_metric by label my_label :

sum(my_metric) by (my_label)

As a result I get:

{my_label="A"}
{my_label="B"}

Also I want to calculate total sum of the metric:

sum(my_metric)

As a result I get the metric without labels. But what I want is:

{my_label="TOTAL"}

How can I achieve this?

My goal is to write one query like this:

sum(my_metric) by (my_label)
or
sum(my_metric) # add label my_label=TOTAL

with the result:

{my_label="A"}
{my_label="B"}
{my_label="TOTAL"}

You can't change the label value of a PromQL query result.

However, are you using Grafana to watch your metrics ? If so, you can change the legend and print TOTAL instead of the metric name. You can even format the legend in order to print the metric name + a custom label.
You can find an example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM