Home > Mobile >  Prometheus dns_sd metric_relabel_configs
Prometheus dns_sd metric_relabel_configs

Time:09-06

I'm looking to use prometheus to scrape EC2 instances registered in CloudMap. Because the generic dns_sd doesn't provide much in terms of labels, I'm trying to extract them from the DNS name of the instance, which I have in the following format:

instance_identifier.port.instance-id.service.example.local

The instance identifier can either be a word or two words with underscores or a word and a number with an underscore (e.g. test.3000.i-abc123.service.example.local and test_2.3005.i-rewq873.service.example.local).

I'm trying to get parts of the hostname into their own labels with:

  - job_name: 'job'
    scrape_interval: 15s
    dns_sd_configs:
      - names: 
        - service.example.local
    metric_relabel_configs:
      - source_labels: [__meta_dns_srv_record_target] # test_2.3005.i-rewq873.service.example.local.
        replacement: $1
        regex: ([^.]*)
        target_label: "instance_type"

which according to regex101.com should extract test_2, but in prometheus this doesn't seem to work.

What would be the correct way to extract the string before the first dot and the string between any two dots (the instance ID) into their own labels?

CodePudding user response:

You can use

regex: ^([^.]*).*

See the regex demo.

Details:

  • ^ - start of string
  • ([^.]*) - Group 1: any zero or more chars other than a dot
  • .* - any zero or more chars other than line break chars as many as possible.
  • Related