简体   繁体   English

Prometheus:PromQL - 为指标添加标签

[英]Prometheus: PromQL - Adding a label to the metric

I want to calculate sum of the metric my_metric by label my_label :我想通过标签my_label计算指标my_metricmy_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.您无法更改 PromQL 查询结果的标签值。

However, are you using Grafana to watch your metrics ?但是,您是否使用 Grafana 来查看您的指标? If so, you can change the legend and print TOTAL instead of the metric name.如果是这样,您可以更改图例并打印TOTAL而不是度量名称。 You can even format the legend in order to print the metric name + a custom label.您甚至可以格式化图例以打印指标名称 + 自定义标签。
You can find an example here你可以在这里找到一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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