Reuse same targets, but different ports, for multiple jobs in Prometheus config

Viewed 609

I have a quit large list of hosts that I'm monitoring through Prometheus. For each host I have a couple of different end-points where I fetch metrics but it feels like a lot of repeating in the Prometheus-configuration .yml-file:

 job_name: job1
    static_configs:
      - targets: [host1:9100, host2:9100, host3:9100, ... , host50:9100]

 job_name: job2
    static_configs:
      - targets: [host1:9101, host2:9101, host3:9101, ... , host50:9101]

 job_name: job3
    static_configs:
      - targets: [host1:9102, host2:9102, host3:9102, ... , host50:9102]

Is there a way in the Prometheus configuration to create a group of hosts and then for each job just specify that group and then a port/metric-path?

I.e. something like - host_targets: [host1, host2, host3, ... , host50]

- job_name: job1
    - port: 9100
    static_configs:
      - targets: {{host_targets}}

- job_name: job2
    - port: 9101
    static_configs:
      - targets: {{host_targets}}

- job_name: job3
    - port: 9102
    static_configs:
      - targets: {{host_targets}}
1 Answers

No there is nothing like that. Your best bet is to use service discovery instead of static configs, and for example using file service discovery generate the config files with an external tool of your choice.

- job_name: job1
  file_sd_configs:
  - files:
    - /file/generated/for/job1.yml

You could also try to hack something like the following using one static file defining the hostnames, but I would recommend just generating separate files for separate jobs.

- job_name: job1
  file_sd_configs:
  - files:
    - /path/to/targethosts.yml
  relabel_configs:
  - source_labels: [__address__]
    target_label: __address__
    replacement: ${1}:9100
Related