labelmap vs replace in scrape config

4,490 views
Skip to first unread message

Cemalettin Koc

unread,
May 2, 2017, 6:57:06 PM5/2/17
to Prometheus Users
I have some confusion regarding labelmap and replace usage in scrape configuration. I think that they can be used for each other. 

1. What is the difference between them? For which use cases they are more suitable?
2. "replace" is using "target_label" whereas "labelmap" is using "replacement" for targeting. Why is not labelmap using "target_label" as well? 

Thanks  

Julius Volz

unread,
May 2, 2017, 7:16:51 PM5/2/17
to Cemalettin Koc, Prometheus Users
"labelmap" maps a whole set of labels (that match a regex) over to the target labels. That's typically useful if you want to copy over a whole set of labels (like all starting with "__meta_kubernetes_service_label_") to the target's labels. For example: https://github.com/prometheus/prometheus/blob/86426c05669417cf1012fbc3a40fb7e0f5a8a101/documentation/examples/prometheus-kubernetes.yml#L113-L114

"replace" does a replacement of a single label only (possibly from multiple source labels). Thus you can't use "replace" to copy over an arbitrary number of labels (of which you possibly don't know all exact names), but only one per relabeling rule.

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-users+unsubscribe@googlegroups.com.
To post to this group, send email to prometheus-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/37905bb7-539c-4f85-9231-2fe4849bde9a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

wheresh...@gmail.com

unread,
Jan 10, 2018, 1:49:14 AM1/10/18
to Prometheus Users
hi, I check the demo yml file and found this :

- action: labelmap regex: __meta_kubernetes_service_label_(.+) - source_labels: [__meta_kubernetes_namespace] action: replace target_label: kubernetes_namespace
seems no target_label  to replace in the action labelmap . 
then what happens to it ?

在 2017年5月3日星期三 UTC+8上午7:16:51,Julius Volz写道:
"labelmap" maps a whole set of labels (that match a regex) over to the target labels. That's typically useful if you want to copy over a whole set of labels (like all starting with "__meta_kubernetes_service_label_") to the target's labels. For example: https://github.com/prometheus/prometheus/blob/86426c05669417cf1012fbc3a40fb7e0f5a8a101/documentation/examples/prometheus-kubernetes.yml#L113-L114

"replace" does a replacement of a single label only (possibly from multiple source labels). Thus you can't use "replace" to copy over an arbitrary number of labels (of which you possibly don't know all exact names), but only one per relabeling rule.
On Wed, May 3, 2017 at 12:57 AM, Cemalettin Koc <cemalet...@gmail.com> wrote:
I have some confusion regarding labelmap and replace usage in scrape configuration. I think that they can be used for each other. 

1. What is the difference between them? For which use cases they are more suitable?
2. "replace" is using "target_label" whereas "labelmap" is using "replacement" for targeting. Why is not labelmap using "target_label" as well? 

Thanks  

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
To post to this group, send email to promethe...@googlegroups.com.

David Lira

unread,
Sep 30, 2021, 11:59:56 PM9/30/21
to Prometheus Users
Great question wheresh. I had the exact same question too. And I was extremely confused. 

I think (please someone correct me if I'm wrong) that the action labelmap actually creates a new set of labels, instead of relabeling the labels:
     "labelmap: Match regex against all label names. Then copy the values of the matching labels to label names given by replacement..."

Also, I think that labelmap is not a replace action because all replace actions require a target_label:
     # Label to which the resulting value is written in a replace action. 
     # It is mandatory for replace actions. Regex capture groups are available. 
     [ target_label: <labelname> ]

So I think it just creates a new set of labels. 
The default replacement is $1, and in the example what is being captured in "regex: __meta_kubernetes_service_label_(.+)" is the very last part.
So this is what I think is happening:

Labels that were matched                                                     New Labels
_meta_kubernetes_service_label_somelabel1       ->         somelabel1
_meta_kubernetes_service_label_somelabel2       ->         somelabel2
_meta_kubernetes_service_label_somelabel3       ->         somelabel3
_meta_kubernetes_service_label_somelabel4       ->         somelabel4
_meta_kubernetes_service_label_somelabel5       ->         somelabel5

Someone please either confirm that this is what is happening, or provide an explanation of what is actually happening. 

Thank you!

Brian Candler

unread,
Oct 1, 2021, 4:20:29 AM10/1/21
to Prometheus Users
That's correct.  The "replacement" for the labelmap action is the new label *name*, and it defaults to $1

target_label is used for the replace action, and for that action the "replacement" is the *value* of the new label being created.

David Lira

unread,
Oct 1, 2021, 11:06:35 AM10/1/21
to Prometheus Users
Awesome! Thank you for the confirmation Brian Candler!
Reply all
Reply to author
Forward
0 new messages