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"
Related
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
I am tyring to set rule for deployment stage in gitlab-ci.yml file where if the git commit message is having a particular [STRING] in this format then it should deploy to that particular environment where this rule is written.
# Deploy to QAT environment
deploy-qat:
stage: deploy
extends: .helm_deploy
environment:
name: qat
tags:
- exe-prd
rules:
- if: $CI_COMMIT_MESSAGE =~ "/[QAT]$/|/[qat]$/" #&& $CI_COMMIT_REF_NAME == "example/qat"
when: always
I have wrote above rule however it is not working. I have tried below combinations of regular expressions however none of them are working.
"/\[QAT\]/|/\[qat\]/"
"/[QAT]/|/[qat]/"
"*\[QAT\]*|*\[qat\]*"
"\[\(QAT\|qat\)\]"
"\[\(QAT\|qat\)]"
"/\[(qat|QAT)\]/"
I tried following website for regular expression here which validates my requirement but it is not working inside gitlab-ci.yml file.
You can use
# Deploy to QAT environment
deploy-qat:
stage: deploy
extends: .helm_deploy
environment:
name: qat
tags:
- exe-prd
rules:
- if: $CI_COMMIT_MESSAGE =~ /\[(QAT|qat)]/
when: always
See more about how to format regex matching conditions at the rules:variables reference page.
NOTES:
/\[(QAT|qat)]/ should not be put inside quotes
You need to use /.../ regex literal syntax (the backslashes are regex delimiters)
\[(QAT|qat)] is a regex that matches [, then either QAT or qat, and then a ] char
=~ is a regex matching operator.
Try this block in your yml:
deploy-qat:
only:
message:
- /\[(qat|QAT)\]/
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
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
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/