Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename label within a metric in Prometheus

I have a query:

node_systemd_unit_state{instance="server-01",job="node-exporters",name="kubelet.service",state="active"} 1

I want the label name being renamed (or replaced) to unit_name ONLY within the node_systemd_unit_state metric. So, desired result is:

node_systemd_unit_state{instance="server-01",job="node-exporters",unit_name="kubelet.service",state="active"} 1

There are many other metrics with a label name name in the node-exporters job. That's why I can't use relabel config across the job.

like image 843
Konstantin Vustin Avatar asked Jan 17 '19 12:01

Konstantin Vustin


People also ask

How do you change your metric name in Prometheus?

To do that, you just need to do a metric_relabel configuration. This configuration applies to relabel (as the name already indicates) labels of your metrics in this case before being ingested but also allow us to use some notable terms to do different things, and one of these notable terms is __name__.

How do I add labels to Prometheus metrics?

Unfortunately, it is not possible to change the labels on old metrics in Prometheus. The storage is only updated by new scrapes and then it becomes immutable.

What is label in Prometheus metrics?

A label is a certain attribute of a metric. Generally, labels are populated by metric producers (servers in the example above). However, in Prometheus, it's possible to enrich a metric with some static labels based on the producer's identity while recording it on the Prometheus node's side.

How do you name metrics?

Universal: Metric names are made up of words separated by underscores. Words use only lowercase letters and numbers (7bit ASCII). Metrics names should start with a letter. Any other punctuation or symbols are proscribed as they might have special use in a downstream system.


1 Answers

you can use the label_replace function in promQL, but it also add the label, don't replace it

label_replace(
  <vector_expr>, "<desired_label>", "$1", "<existing_label>", "(.+)"
)

label_replace(
node_systemd_unit_state{instance="server-01",job="node-exporters",name="kubelet.service",state="active"},
"unit_name","$1","name", "(.+)"
)

So, to avoid the repetition you can add:

sum(label_replace(
    node_systemd_unit_state{instance="server-01",job="node-exporters",name="kubelet.service",state="active"},
    "unit_name","$1","name", "(.+)"
    )
)by(unit_name)
like image 97
Chus Avatar answered Oct 11 '22 11:10

Chus