Prometheus: how to drop a target based on Consul tags

烈酒焚心 提交于 2019-12-24 05:47:11

问题


My Prometheus server gets its list of targets (or "services", in Consul's lingo) from Consul. I only want to monitor a subset of these targets. This should be possible via Prometheus's regex mechanism, but the correct configuration eludes me. How is this done?


回答1:


I've scoured the web and there is not a single example showing how its done, so for posterity - the following configuration will drop all consul services marked with the 'ignore-at-prometheus' tag

# ignore consul services with 'ignore_at_prometheus' tag
# https://www.robustperception.io/little-things-matter/
relabel_configs
- source_labels: ['__meta_consul_tags']
  regex: '(.*),ignore-at-prometheus,(.*)'
  action: drop



回答2:


I've used a very similar solution to the problem using the following config. It allows to scrape only the services with a specific tag, rather than excluding services with a given tag.

Here's the scrape_configs section of my config:

scrape_configs:
  - job_name: 'consul_registered_services'
    scrape_interval: 5s
    metrics_path: '/prometheus'
    consul_sd_configs:
    - server: 'my-consul-server:8500'
      token: 'xyz'
    relabel_configs:
    - source_labels: ['__meta_consul_tags']
      regex: '^.*,metrics_method=prometheus-servlet,.*$'
      action: keep
    - source_labels: ['__meta_consul_node']
      target_label: instance
    - source_labels: ['__meta_consul_service']
      target_label: service
    - source_labels: ['__meta_consul_tags']
      target_label: tags

I then make sure to register all relevant services with the metrics_method=prometheus-servlet tag, and the rest will be ignored.

The documentation for the relabeling configuration is available here: https://prometheus.io/docs/operating/configuration/#relabel_config.

The documentation for the Consul service discovery configuration is available here: https://prometheus.io/docs/operating/configuration/#consul_sd_config.



来源:https://stackoverflow.com/questions/40376937/prometheus-how-to-drop-a-target-based-on-consul-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!