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
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.
- 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"