Labels replacing

83 views
Skip to first unread message

Nikolay Buhryk

unread,
Oct 11, 2021, 12:11:00 PM10/11/21
to Prometheus Users
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.

Kevin Cameron

unread,
Oct 14, 2021, 9:18:38 AM10/14/21
to Prometheus Users
Is the issue that your target list is made up of IPs and you would like to be able to add a label "hostname" via a DNS lookup dynamically when the job runs rather than manually adding the label to your target list?  I'd love a way to do that if you find a way.

KC

Brian Candler

unread,
Oct 15, 2021, 7:24:52 AM10/15/21
to Prometheus Users
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 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"

Nikolay Buhryk

unread,
Oct 20, 2021, 10:32:59 AM10/20/21
to Brian Candler, Prometheus Users
Hi Brian,

I modified my target to the new view e.g. 'hostname ip' = 'foo 10.11.12.13' and i received only this   instance="1"

I can’t understand where issue still exists

--
You received this message because you are subscribed to a topic in the Google Groups "Prometheus Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/prometheus-users/_-NNuiz8lVs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to prometheus-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/773d176e-bbe7-44bd-984b-05cce4cbe90cn%40googlegroups.com.

Brian Candler

unread,
Oct 20, 2021, 1:39:20 PM10/20/21
to Prometheus Users
I think it's a copy-paste error.  Can you paste back the entire relabel_configs section from your config.
Reply all
Reply to author
Forward
0 new messages