You can use target relabelling, where each target entry can contain both a
name and an IP address and you separate them out in relabelling rules.
This also lets you generate meaningful instance labels
<https://www.robustperception.io/controlling-the-instance-label> that don't
have the exporter port number in them - this is very useful when you want a
query to join metrics from two different exporters.
Here is a sample config:
- job_name: node
file_sd_configs:
- files:
- /etc/prometheus/targets.d/node_targets.yml
metrics_path: /metrics
relabel_configs:
- source_labels: [__address__]
regex: '([^ ]+)' # single value (DNS name or address)
target_label: instance
- source_labels: [__address__]
regex: '(.+) (.+)' # name address
target_label: instance
replacement: '${1}'
- source_labels: [__address__]
regex: '(.+) (.+)' # name address
target_label: __address__
replacement: '${2}'
- source_labels: [__address__]
target_label: __address__
replacement: '${1}:9100'
With this configuration, each entry in your targets file can be either an
IP address, a DNS name, or "<name><space><address>", e.g. "foo 1.2.3.4"
In the latter case, the <name> becomes the 'instance' label, and the
<address> is the address used to scrape. The name you choose can be
anything you like - it doesn't have to relate to a DNS name.
Don't include the port number in your targets file, as this is added
automatically by the last relabelling rule. You could remove that rule if
you want though, in which case entries would be of the form "foo
1.2.3.4:9100"
On Monday, 11 October 2021 at 17:11:00 UTC+1 Nikolay Buhryk wrote:
> Hi,
>
> Can someone help with replacing labels on targets.
>
> I have file_sd configs with targets. Prometheus scrapes them and shows
> their ip addresses and labels that were defined.
>
> I need to have possibility scrape their hostnames instead of ip and add
> new label "hostname" for future use in Alertmanager notifications.
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/prometheus-users/773d176e-bbe7-44bd-984b-05cce4cbe90cn%40googlegroups.com.