How to relabel instance names

55 views
Skip to first unread message

Danny de Waard

unread,
Mar 5, 2020, 12:20:01 PM3/5/20
to Prometheus Users

I have been reading all across the internet for a way to relabel my instance name.
Now all my dashboard have long instance names like lsrv1.server.nl:9100

I would like to have that reduced to lsrv1

My data is coming from different exporters like node_exporter and push gateway.
The last one i know you can have exported_instance and that works.
But other exporters i cant get it to work to relabel

Example (piece) of my prometheus.yml



- job_name: 'PreProd'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s

static_configs:
  - targets: ['lsrv1.server.nl:9100'] # node_exporter
  - targets: ['lsrv1.server.nl:9091'] # pushgateway
  - targets: ['lsrv1.server.nl:9901'] # some.log
  - targets: ['lsrv1.server.nl:9902'] # some_view.log
  - targets: ['lsrv1.server.nl:9903'] # some_services.log
  - targets: ['lsrv2.server.nl:9100'] # node_exporter
  - targets: ['lsrv2.server.nl:9091'] # pushgateway
  - targets: ['lsrv2.server.nl:9901'] # some.log
  - targets: ['lsrv2.server.nl:9902'] # some_view.log
  - targets: ['lsrv2.server.nl:9903'] # some_services.log


Can someone assist me with this?

Brian Candler

unread,
Mar 5, 2020, 12:54:50 PM3/5/20
to Prometheus Users
On Thursday, 5 March 2020 17:20:01 UTC, Danny de Waard wrote:

I have been reading all across the internet for a way to relabel my instance name.
Now all my dashboard have long instance names like lsrv1.server.nl:9100

I would like to have that reduced to lsrv1


The blog post at https://www.robustperception.io/controlling-the-instance-label describes this - at least, getting rid of the port.

You can also make the instance label a short name "lsrv1" whilst scraping target with DNS name "lsrv1.server.nl" or IP address "1.2.3.4". To do this, make a target which contains both parts, e.g. "name/address", and use relabelling to splitting the name into the "instance" label and the address:port into the "__address__" label.  See:

Danny de Waard

unread,
Mar 5, 2020, 1:30:54 PM3/5/20
to Prometheus Users
If i reads the article correct i should be able to use this relabel:
      - source_labels: [__address__]
        regex: '(.+)/(.+)'  # name/address
        target_label: __address__
        replacement: '${2}'
and fill in lsrv1/lsrv1.server.nl:9100 in my target line?

Or should i also use a node_targets.yml to simplify all this

[
    {
        "targets": [ "senta04.mydomain.com:9100" ],
        "labels": {
            "instance": "senta04"
        }
    },{
      "targets": [ "senta02.mydomain.com:9100" ],
      "labels": {
          "instance": "senta02"
      }
  }
]
Almost looks when if i use this yml i dont have to relabel...??

Op donderdag 5 maart 2020 18:54:50 UTC+1 schreef Brian Candler:

Brian Candler

unread,
Mar 5, 2020, 2:54:32 PM3/5/20
to Prometheus Users
On Thursday, 5 March 2020 18:30:54 UTC, Danny de Waard wrote:
If i reads the article correct i should be able to use this relabel:
      - source_labels: [__address__]
        regex: '(.+)/(.+)'  # name/address
        target_label: __address__
        replacement: '${2}'
and fill in lsrv1/lsrv1.server.nl:9100 in my target line?


Yes, although before overwriting __address__ like that you'll want to pull out the first part of __address__ to put in "instance".

Another possibility is if the instance label you want is always just the first part of the DNS name, you can extract that.  Then target "lsrv1.server.nl:9100" will give "lsrv1" as the label.

      - source_labels: [__address__]
        regex: '([^.]+).*'
        target_label: instance
        replacement: '${1}'

 
Or should i also use a node_targets.yml to simplify all this

[
    {
        "targets": [ "senta04.mydomain.com:9100" ],
        "labels": {
            "instance": "senta04"
        }
    },{
      "targets": [ "senta02.mydomain.com:9100" ],
      "labels": {
          "instance": "senta02"
      }
  }
]
Almost looks when if i use this yml i dont have to relabel...??

Yes, that will work too. It does mean that you have to be careful that each target group in the targets file contain only a single target.

Reply all
Reply to author
Forward
0 new messages