In case you would like to dynamically define port by additional annotation, ie.:
selector:
matchLabels:
app: my-service
template:
metadata:
labels:
app: my-service
annotations:
prometheus: "true"
prometheus/port: "8888"
You can use the following transformation:
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_port]
action: replace
regex: ([^:]+)(?::\d+)?;(\d+)
replacement: ${1}:${2}
target_label: __address__
Source labels in prometheus are joined with ;
and in this case we will have ie. (if your server by default listening on 8080): http://10.52.9.79:8080;8888
In regex we have:
- first group - all signs without
:
, ie.: //10.52.9.79
- second non-capturing group with original port if exist
- third group with port from annotation
As a result you set original address and port from annotation. Could be useful if for example you have Spring Boot application and you want to use different management port than default application port.