Using Regex to get cluster name from instance names prometheus relabel_configs - regex

I have a bunch of autodiscovered ec2 instances that form a bunch of clusters.
For example I will have abc-def01.otherstuff, abc-def02.otherstuff and abc-def03.otherstuff, and I need to give them the label cluster which will be abc-def.
I've tried to add this to the prometheus config like so:
- job_name: "ssl_mongo"
ec2_sd_configs:
- region: eu-central-1
port: 27018
relabel_configs:
- source_labels: ['__meta_ec2_tag_Name']
target_label: instance
replacement: '$1:27018'
- target_label: "role"
replacement: "mongo_ssl_exporter"
- source_labels: ['__meta_ec2_tag_Name']
regex: '(\w+-[a-z]{3})'
target_label: "cluster"
replacement: '$1'
I've tested the regex using regexr and it seemed to work but when I look at prometheus, there's no cluster label at all.

Two things to consider:
a) The Prometheus documentation says:
The regex is anchored on both ends. To un-anchor the regex, use .*<regex>.*
b) I think the "-" must be escaped
So, I would try the following regex:
(\w+\-[a-z]{3}).*
See the Prometheus documentation here

Related

My regex does not effective in spring-cloud-gateway

I'm learning spring-cloud-gateway, when I practice predicates, I want to try some regex like followings:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://www.google.com/
predicates:
- Cookie=token, hello*
I think hello* will matches hello,helloa,helloaaaaa..., but when I test by curl --cookies ..., it only matches hello, why helloa and helloaaaaa does not matches correctly?
Does regex in spring cloud application.yml need some changes?
I doesn't have anything to do with yaml or gateway just java regex
"helloaaaa".matches("hello*") // returns false.
But this works
"helloaaaa".matches("hello.*") // returns true.
Patter javadoc says X* matches X, zero or more times
So your regex would match "hellooooo"
"hellooooo".matches("hello*") // returns true.

Using regular expressions in Prometheus relabel_configs source_labels

I'm trying to save the meta labels retrieved from EC2 Service Discovery as target labels. I'm mostly concerned about the tags - every instance contains a lot of them and I would love to gather them all using one simple configuration entry with the usage of regular expression.
The perfect solution seems to be something like this:
relabel_configs:
- source_labels:
- '__meta_ec2_tag_(.*)'
target_label: '### use extracted regex group here'
Unfortunately, I get the following error:
\"__meta_ec2_tag_(.*)\" is not a valid label name"
Does that mean that I can't use regular expressions to describe source labels and that I have to specify each source label separately like in the sample below?
- source_labels:
- '__meta_ec2_tag_Name'
target_label: 'instance_name'
- source_labels:
- '__meta_ec2_tag_environment'
target_label: 'environment'
- source_labels:
- '__meta_ec2_tag_project'
target_label: 'project'
Try this:
relabel_configs:
- regex: '__meta_ec2_tag_(.*)'
replacement: $1
I just encountered the same problem but the previous answer didn't work for me with this error :
relabel configuration for replace action requires 'target_label' value
I found out that Prometheus now has a labelmap option for relabel_configs that does this : https://grafana.com/blog/2022/03/21/how-relabeling-in-prometheus-works/#labelmap
TLDR;
relabel_configs:
- action: labelmap
regex: "__meta_ec2_tag_(.*)"
replacement: "$1"

regex filter in replace module for ansible

Trying to get a regex replace in an ansible role for update autoscales going.
In my CFT I have the following mapping:
DevRegionSettings:
us-east-1:
primaryZone: us-east-1a
# secondaryZone: us-east-1b
# autoscale is wrong at point of instantiation
amiAutoscale: ami-234sefsrwerwer21
amiDB: ami-12313123
amiCoord: ami-12312312
amiWeb: ami-13123123
amiWorker: ami-12312312
I want to replace just the value of amiAutoscale with the latest ami that I find earlier on in the role.
I'm a regex noob and cannot figure it out for the life of me.
Been playing around with some of the regex from this thread:
Regex to match key in YAML
But still cant get it to do what I want :(
Any help would be appreciated!
The ansible task I had running was as follows:
- name: Replacing ami in the Dev Cloudformation Template
replace:
regexp: '(^\s*(?P<key>\w+_amiAutoscale):\s*(?P<value>\d+))'
replace: "{{ latest_ami.image_id }}"
path: "$path_to_cft.yaml"
So a couple of issues with your regex:
\w+_amiAutoscale - The line amiAutoscale: ami-234sefsrwerwer21 does not have an _ before amiAutoscale
(?P<value>\d+) - ami-234sefsrwerwer21 is not a sequence of digits.
This worked for me, but may be too open of a pattern: (^\s*(?P<key>amiAutoscale):\s*(?P<value>.+))
Example: https://regex101.com/r/76VGlJ/1
- name: Replacing ami in the Dev Cloudformation Template
replace:
regexp: '(^\s*(?P<key>amiAutoscale):\s*(?P<value>.+))'
replace: "{{ latest_ami.image_id }}"
path: "$path_to_cft.yaml"
Your regex does not match because the regex expect to match 1+ word characters followed by an underscore \w+_ right after the starting whitespace characters ^\s* which are not in the data.
Also, in the named capturing group (?P<value>\d+) you match 1+ digits which does not match ami-234sefsrwerwer21
What you also might do for your example data is to use only 2 capturing groups and a character class in the second group to specify what you would allow to match:
^\s*(?P<key>amiAutoscale)\s*:\s*(?P<value>[\w-]+)
Regex demo

Replace pod IP address with Host IP address keeping path and port same

Currently I am monitoring Kubernetes Pods using Prometheus. My base config is :
- job_name: 'kubernetes_pods'
tls_config:
insecure_skip_verify: true
kubernetes_sd_configs:
- api_server: http://k8s_master:8080
role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: pod_name
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
This basically results the Endpoints column stating the internal Kubernetes IP addresses rather than the host IP addresses on which the pods are deployed.
I want to change the pod IP addresses to the actual host IP addresses.
So I added the below config:
- source_labels: [__meta_kubernetes_pod_host_ip]
target_label: __address__
regex: (.*)
replacement: $1
Now what Im seeing is that the IPs have definitely been changed but for some reason everything after : is exactly the same which is wrong.
Is there something wrong with the regex ?
__address__ includes the port number, so you'll need to either preserve that or take it from another label. For example:
- source_labels: [__meta_kubernetes_pod_host_ip, __address__]
target_label: __address__
regex: (.*);.*:(\d+)
replacement: $1:$2

Prometheus/ create list of tag from __meta_gce_tags

Im using relabeling in order to take the gce tag to Prometheus labels, by using the following code in Prometheus yml:
relabel_configs:
- source_labels: [__meta_gce_tags]
target_label: tags
the tags look like this:
tags=",node_a,node_prod,node_centos,"
The problem is that it gives a string list, while I wish to have list of tags so I can query it. for example, instead of using:
sum(elasticsearch_node_stats_up{tags=~".*?noda_a.*node_prod.?"})
I will be able to use it without regex. Is there any way to do it?
You can use regex to extract values from a label value during relabeling. So try this, which is not exactly DRY, but works:
relabel_configs:
- source_labels: [__meta_gce_tags]
regex: ",node_([a-z]+),node_[a-z]+,node_[a-z]+"
target_label: node
replacement: '${1}'
- source_labels: [__meta_gce_tags]
regex: ",node_[a-z]+,node_([a-z]+),node_[a-z]+"
target_label: env
replacement: '${1}'
- source_labels: [__meta_gce_tags]
regex: "",node_[a-z]+,node_[a-z]+,node_([a-z]+)""
target_label: os
replacement: '${1}'
Note that there is probably a much more elegant regular expression to use.
If you can control the label values, here is a much more durable technique: https://www.robustperception.io/extracting-full-labels-from-consul-tags/