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.
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__.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With