Krzysztof Rosiński
Krzysztof Rosiński

Reputation: 880

Prometheus - add target specific label in static_configs

I have job definition as follows:

  - job_name: 'test-name'
    static_configs:
      - targets: [ '192.168.1.1:9100', '192.168.1.1:9101', '192.168.1.1:9102' ]
        labels:
          group: 'development'

Is there any way to annotate targets with labels? For instance, I would like to add 'service-1' label to '192.168.1.1:9100', 'service-2' to '192.168.1.1:9101' etc.

Upvotes: 68

Views: 117575

Answers (6)

zxf曾爷
zxf曾爷

Reputation: 657

Can be like this

  - job_name: 'node'
    static_configs:
    - targets: ['192.168.1.117:9100']
      labels:
        instance: 'linux-ina'
    - targets: ['192.168.1.138:9100']
      labels:
        instance: 'linux-inb'

Tag name can be replaced with instance

enter image description here enter image description here

Upvotes: 44

Nils Schmidt
Nils Schmidt

Reputation: 3720

You can use file-based service discovery to achieve this. See this blog post for more details.

Upvotes: 2

David Tinker
David Tinker

Reputation: 9634

The targets do not have to be simple ip:port pairs, you can include extra info and use re-labelling to split out the ip and port from a nice instance name:

scrape_configs:
  - job_name: 'prometheus_metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter_metrics'
    scrape_interval: 5s
    static_configs:
      - targets: ["157.xxx.xxx.xxx:9100@db56a", "176.xxx.xxx.xxx:9100@db56b"]
        labels:
          role: 'postgres_ha'
      - targets: ["46.xxx.xxx.xxx:9100@p1a", "176.xxx.xxx.xxx:9100@p1b", "136.xxx.xxx.xxx:9100@p1c"]
        labels:
          role: 'pulsar'
    relabel_configs:
      - source_labels: [ __address__ ]
        regex: '(.*)@(.*)'
        replacement: $2
        target_label: instance
      - source_labels: [ __address__ ]
        regex: '(.*)@(.*)'
        replacement: $1
        target_label: __address__

Upvotes: 2

pan congwen
pan congwen

Reputation: 946

I have the same question before. Here is my solution:

  1. use job_name as the group label
  2. add more target option to separate instance and add labels

For you, the code may like this:

  - job_name: 'development'
      static_configs:
      - targets: [ '192.168.1.1:9100' ]
        labels:
          service: '1'
      - targets: [ '192.168.1.1:9101' ]
        labels:
          service: '2'

Upvotes: 93

Raposo
Raposo

Reputation: 620

You can find information in here - Prometheus Good Config

But I have used these and it worked


  - job_name:  'PostgreSQL-exporter'
    scrape_interval: 60s
    scrape_timeout: 60s
    static_configs:
      - targets: ['localhost:9187']
      - labels:
          name: value-for-the-name

Upvotes: 0

brian-brazil
brian-brazil

Reputation: 34142

For different services you should usually vary the job label, so I would suggest duplicating the scrape config with a job_name of service1 for one and service2 for the other.

Upvotes: 3

Related Questions