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
🌐
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.
🌐
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.
Discussions

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
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
relabel and aggregate metrics
For future travelers here is what I found after looking around.Prometheus cant do what I'm asking for directly since running the sum (aggergation) query requires the data to be in the tsdb , which deafets the purpose of doing the aggregation on scrap. There are other ways to do this. writing your own exporter; basically hack a python script and expose /metrics that scrap rabbitmq and do the aggregation on demand without having a state. having another prometheus instance with a recording rule federated to the main instance, this will be the most compatible but will cause delays of queries since recording rules gets evaluated after the scrap getting stored using victoriametrics aggregation via a cheap single instance server with the lowest retention possible then using the /federate endpoint as a scrap endpoint for my main prometheus give up and just dont collect those metrics I choose the 3rd option since it is the most sane one. Although I dont like having this victoriametrics instance just to aggregate, and I dont want to maintain or solve the problems related to metrics aggregation with my own hacky python script, I have no other choice but to use this. If anyone else have a better solution let me know edit: note with victoriametrics, the only shared metrics with the federate endpoint are the aggregated ones, so you might need to scrap twice, once for prometheus with dropping the labels you want to aggregate, and second for victoriametrics with the labels you want to aggregate More on reddit.com
🌐 r/PrometheusMonitoring
4
2
July 21, 2023
replace - How to write nested label_replace queries in prometheus? - Stack Overflow
1 Is it a good solution to use "label_replace" in a prometheus query when doing math operations on two metrics with different labels for the same value · 2 PromQL/prometheus query label_replace() multiple 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
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

🌐
VictoriaMetrics
docs.victoriametrics.com › victoriametrics › metricsql
VictoriaMetrics: MetricsQL
This function is supported by PromQL. See also deg . prometheus_buckets(buckets) is a transform function , which converts VictoriaMetrics histogram buckets with vmrange labels to Prometheus histogram buckets with le labels.
🌐
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.
🌐
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.
Find elsewhere
🌐
Promlabs
promlabs.com › promql-cheat-sheet
PromLabs | PromQL Cheat Sheet
Go get our self-paced in-depth PromQL training! Select latest sample for series with a given metric name: node_cpu_seconds_total · Open in PromLens · Select 5-minute range of samples for series with a given metric name: node_cpu_seconds_total[5m] Open in PromLens · Only series with given label values: node_cpu_seconds_total{cpu="0",mode="idle"} Open in PromLens ·
🌐
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 ...
🌐
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", "","")
🌐
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…
🌐
Reddit
reddit.com › r/prometheusmonitoring › relabel and aggregate metrics
r/PrometheusMonitoring on Reddit: relabel and aggregate metrics
July 21, 2023 -

Hi,

I have rabbitmq metrics which contains the `channel` label. Since this label has high cardinality I decided I want to drop it, but faced an issue.

When prometheus drops it, there will be duplicates, and prometheus just take one of them, the exact situation here https://grafana.com/blog/2022/10/20/how-to-manage-high-cardinality-metrics-in-prometheus-and-kubernetes/#3-begin-optimizing-metrics in the `Reduce labels` section.

From what I can see, I need recording rule that would sum these metrics but im not sure about the order of operations.

If I have a metric_relabeling_rule in the scrapping config and a recording rule, which one will be applied first?

Is there a sensible way of recalculating all of the metrics that contains the `channel` label and take the sum of them such that no data is being dropped?

Or do I have to create a new metric name with the channel summed?

Edit:
In this response they say "maybe you need to aggregate over the duplicate series", but i dont know if they mean recording rules or what

Top answer
1 of 1
1
For future travelers here is what I found after looking around.Prometheus cant do what I'm asking for directly since running the sum (aggergation) query requires the data to be in the tsdb , which deafets the purpose of doing the aggregation on scrap. There are other ways to do this. writing your own exporter; basically hack a python script and expose /metrics that scrap rabbitmq and do the aggregation on demand without having a state. having another prometheus instance with a recording rule federated to the main instance, this will be the most compatible but will cause delays of queries since recording rules gets evaluated after the scrap getting stored using victoriametrics aggregation via a cheap single instance server with the lowest retention possible then using the /federate endpoint as a scrap endpoint for my main prometheus give up and just dont collect those metrics I choose the 3rd option since it is the most sane one. Although I dont like having this victoriametrics instance just to aggregate, and I dont want to maintain or solve the problems related to metrics aggregation with my own hacky python script, I have no other choice but to use this. If anyone else have a better solution let me know edit: note with victoriametrics, the only shared metrics with the federate endpoint are the aggregated ones, so you might need to scrap twice, once for prometheus with dropping the labels you want to aggregate, and second for victoriametrics with the labels you want to aggregate
🌐
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.
🌐
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".
🌐
Last9
last9.io › blog › mastering-prometheus-relabeling-a-comprehensive-guide
Mastering Prometheus Relabeling: A Comprehensive Guide | Last9
February 26, 2026 - This rule will take the current value of the service label and replace the value of the environment label with production.
🌐
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.
🌐
Google Groups
groups.google.com › g › prometheus-users › c › VKS6ANLJ3OQ
Replacing a specific character in a label value
August 23, 2022 - a maximum of 4? Then you can just use label_replace 4 times (or repeat the whole rewriting rule 4 times).