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)
Answer from Chus on Stack Overflow
Top answer
1 of 5
43

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)
2 of 5
26

I got tired of all the fragmented documentation and I feel I provided a better answer in this post here: https://medium.com/@texasdave2/replace-and-remove-a-label-in-a-prometheus-query-9500faa302f0

Replace is not a true REPLACE

Your goal is to simply replace the old label name “old_job_id” with a new label name “new_task_id”. Prometheus label_replace will really “add” the new label name. It will preserve the old label name as well… So, that could be a problem, it’s not a true “replace in place”.

So if you want to “add” your new label name and “remove” the old label name, you need to do this:

sum without (old_job_id) (label_replace(metric, "new_task_id", "$1", "old_job_id", "(.*)"))

Here’s how this reads:

  • sum without (old_job_id) will remove the old label name from the query output

  • metric is your metric, like “node_filesystem_avail_bytes”

  • “new_task_id” is where you would put your new label name

  • “$1” is regex for using the string in new label name, don’t change this

  • “old_job_id” is where you’ll put your old label, the one you want to get rid of (.*……. that mess is regex that will replace the whole label name

🌐
Prometheus
prometheus.io › docs › prometheus › latest › querying › functions
Query functions | Prometheus
Please note that sort_by_label only affects the results of instant queries, as range query results always have a fixed output ordering. ... This function has to be enabled via the feature flag --enable-feature=promql-experimental-functions.
Discussions

Query label_replace function - PromQL - Prometheus Monitoring System
label_replace( (global_peers_details), “peers_1”, "$1[$1] & $2[$2] ", “peers_1”, “(.),(.)” ) I have 3 nodes cluster. so The metric will shows the current node where it connects with other two nodes through label p… More on discuss.prometheus.io
🌐 discuss.prometheus.io
0
May 2, 2024
promql - How to change label values in Prometheus? - Stack Overflow
I can't execute PromQL query for change some of my label values. More on stackoverflow.com
🌐 stackoverflow.com
I want to replace label with a unique something
I want to replace this label with a unique label name so that all values align in the same row instead of appearing as separate columns under the value label. Using Promql replace_label I’m not sure how syntax works Giving unique label names to VALUE label by using replace_label Query1 :sum ... More on community.grafana.com
🌐 community.grafana.com
13
0
February 3, 2025
grafana - PromQL/prometheus query label_replace() multiple - Stack Overflow
Tried to find a way with Prometheus query to replace 2 or more labels, but did not find any good and "short" way of doing it. here is what I have: label_replace( label_replace( label_repl... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I filter by label values in PromQL?
Use curly brace selectors: http_requests_total{job="api", status="500"} for exact matches, {status=~"5.."} for regex matches, and {status!="200"} to exclude a value. Multiple label filters combine with AND logic.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
What is PromQL?
PromQL (Prometheus Query Language) is the query language built into Prometheus for selecting, filtering, and aggregating time series data. You use it to write expressions that power dashboards, alerts, and ad-hoc metric analysis.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
How do I predict future resource usage in PromQL?
Use predict_linear(metric[window], seconds). For example, predict_linear(node_filesystem_free_bytes[30d], 86400 7) predicts disk space 7 days from now based on the last 30-day trend. Use a long lookback window for more stable predictions.
🌐
last9.io
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
🌐
Medium
medium.com › @mohamedfaris2 › what-why-and-how-of-prometheus-label-replace-a5cbbef76b68
What, why and how of Prometheus label_replace
December 16, 2025 - The label_replace function can be used when you want a new label added to the existing metric or a sub-query result, with which you can do further query operations or grafana dashboarding.
🌐
SigNoz
signoz.io › guides › how to omit labels in promql series results
How to Omit Labels in PromQL Series Results | SigNoz
November 29, 2024 - When you need to modify or create labels in PromQL, label_replace is a versatile function.
🌐
Prometheus
discuss.prometheus.io › promql
Query label_replace function - PromQL - Prometheus Monitoring System
May 2, 2024 - label_replace( (global_peers_details), “peers_1”, "$1[$1] & $2[$2] ", “peers_1”, “(.),(.)” ) I have 3 nodes cluster. so The metric will shows the current node where it connects with other two nodes through label p…
🌐
Coralogix
coralogix.com › home › promql tutorial: 5 tricks to become a prometheus god
PromQL Tutorial: Basic Concepts & Examples - Coralogix
June 3, 2025 - PromQL’s two label manipulation commands are label_join and label_replace. label_join allows you to take values from separate labels and group them into one new label.
Find elsewhere
🌐
Last9
last9.io › blog › promql-cheat-sheet
PromQL Cheat Sheet: Queries, Functions, and Labels | Last9
September 12, 2024 - Label extraction in PromQL can be performed using the label_replace() function.
🌐
Deploy Live
deploy.live › blog › today-i-learned-adding-labels-to-prometheus-queries
Today I Learned: Adding labels to Prometheus queries // Deploy Live
May 16, 2020 - # Taking earlier Recording Rules - record: job:availability:999 expr: | 99.9 - record: job:availability:99 expr: | 99 # After - record: availability:availability:value expr: | label_replace(job:error_budget:999, "availability", "99.9", "","") - record: availability:availability:value expr: | label_replace(job:error_budget:99, "availability", "99", "","")
🌐
Grafana
community.grafana.com › prometheus
I want to replace label with a unique something - Prometheus - Grafana Labs Community Forums
February 3, 2025 - I want to replace this label with a unique label name so that all values align in the same row instead of appearing as separate columns under the value label. Using Promql replace_label I’m not sure how syntax works Giving unique label names to VALUE label by using replace_label Query1 :sum ...
🌐
Medium
medium.com › @skanagavelu › prometheus-label-replace-b5f7ea50d2ee
Prometheus label_replace - Kanagavelu Sugumar - Medium
November 11, 2022 - Query label_replace(up, “hostname”, “$2”, “instance”, “(.+)-(.+)-(\\d+):(\\d+)”) will return with new label hostname with second match group (“$2”) of the regEx.
🌐
Ac-programming
ac-programming.com › content › Prom › Functions › label.html
Prometheus Label Functions
These examples demonstrate how each label function can be used in PromQL to manipulate and transform time series data based on their labels: 1. label_replace: label_replace(my_metric{job="job1"}, "new_label", "$1", "source_label", "(.*)") This example replaces the value of the "new_label" with the value of "source_label" for the "my_metric" time series.
🌐
Promlabs
training.promlabs.com › training › relabeling › writing-relabeling-rules › setting-or-replacing-label-values
Setting or Replacing Label Values
A common use case for relabeling is to set or overwrite the value of a label. This can be done using the replace action, which is the default if the action field is not specified.
🌐
SigNoz
signoz.io › guides › how to group labels in prometheus queries - a practical guide
How to Group Labels in Prometheus Queries - A Practical Guide | SigNoz
July 24, 2024 - label_replace( group(node_disk_read_bytes_total) by (instance, device), "disk_type", "other", "device", "^(?!sda|sdb).*" ) This query groups all disk metrics, categorizing devices other than "sda" and "sdb" as "other".
🌐
GitHub
gist.github.com › jjo › 080ae9f49175279f52d744325b0eb482
PromQL fun abusing `vector()` and `label_replace()` to create ~arbitrary timeseries, note these are used from grafana's /explore (for those ${__to:...} expressions) · GitHub
round( 8 # <- number of nodes * (1-sin(vector((time()-(${__to:date:seconds}))/(3600*10) * pi()))) / 2 ) * on() group_right() ( label_replace(vector(0.0445000) , "spend", "cpu", "", "") * 8 # <- vCPU per node or label_replace(vector(0.0049225) , "spend", "mem", "", "") * 16 # <- GB per node ) * on() group_left(unit) ( label_replace(vector(1) , "unit", "usd_per_hour", "", "") )
🌐
Google Groups
groups.google.com › g › prometheus-users › c › AftHZOZ60kg
label_replace - extract string from label_value
sum without (image) (label_replace(kube_pod_container_info, "release", "$1", "image", "(.*)"))
🌐
Sysdig
sysdig.com › blog › faster-promql-servicevision-prometheus
Easier and faster PromQL queries with ServiceVision for Prometheus | Sysdig
December 15, 2025 - In PromQL you need to join (vector match) three metrics: sysdig_container_memory_used_bytes, that has the main information you want. sysdig_container_info, that knows which container belongs to which Pod. kube_pod_owner, which tells us which Kubernetes object is the Pod owner. Also, you'll often need to rely on the label_replace PromQL function if the label names don't match, which happens frequently.