首页 > 解决方案 > Prometheus 中的实例标签问题

问题描述

Prometheus 似乎以不正确的方式为目标实例标记和设置其他元数据,可能是由于配置错误。

这是配置,用于抓取多个目标并标记每个目标:

global:
  scrape_interval: 5s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
        labels:
          host: 'Server0'
          service: 'Prometheus'

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
        labels:
          host: 'Server0'

  - job_name: 'wmi_exporter'
    static_configs:
      - targets: ['xxx.xxx.xxx.xxx:9100']
        labels:
          host: 'Server1'
      - targets: ['xxx.xxx.xxx.xxx:9100']
        labels:
          host: 'Server2'

所有目标都在 Prometheus 管理 UI 中显示为 UP。但是,当我对 运行查询时prometheus_sd_discovered_targets,它会返回具有相同job名称、instance地址hostservice标签的目标:

prometheus_sd_discovered_targets{config="node_exporter",host="Local",instance="localhost:9090",job="prometheus",name="scrape",service="Prometheus"}     1

prometheus_sd_discovered_targets{config="prometheus",host="Local",instance="localhost:9090",job="prometheus",name="scrape",service="Prometheus"}     1

prometheus_sd_discovered_targets{config="wmi_exporter",host="Local",instance="localhost:9090",job="prometheus",name="scrape",service="Prometheus"}     2

为什么所有目标的元数据值都相同,即使它们的配置和标签不同?

标签: prometheus

解决方案


prometheus_sd_discovered_targets是 Prometheus 导出的指标(不是节点导出器,不是 wmi 导出器),因此所有这些时间序列都来自您唯一的 Prometheus 实例,即host="Local",instance="localhost:9090",job="prometheus",service="Prometheus".

标签来自name="scrape",config="wmi_exporter"指标本身,因为如果您检查http://localhost:9090/metrics,您会发现(除其他外)如下内容:

prometheus_sd_discovered_targets{name="scrape",config="node_exporter"} 1
prometheus_sd_discovered_targets{name="scrape",config="prometheus"} 1
prometheus_sd_discovered_targets{name="scrape",config="wmi_exporter"} 2

推荐阅读