prometheus

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"}

Solution

  • 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