首页 > 解决方案 > 如何查看已为 Pushgateway 生成数据的实例的主机名

问题描述

我有 prometheus 服务器,它对运行 WMI 导出器的 Azure VM 使用自我发现。在 Grafana 中,我使用仪表板变量进行过滤(参见屏幕截图)。

在虚拟机上,我创建了一个自定义导出器,它为每个服务器输出值为 1 的指标,并且每个服务器将这些值发送到在 etc/prometheus/prometheus.yaml 中配置的单个 Pushgateway

- job_name: 'push-gateway'
  static_configs:
  - targets: ['localhost:9091']

当我查看抓取的指标时,我总是看到实例:localhost:9091 和作业:push-gateway,无论指标来自哪个服务器。如果我手动添加这些标签,我会看到“导出”前缀(见屏幕截图)。

我感到困惑的是,如何确保自定义创建的指标的“作业”和“实例”具有与生成指标的服务器匹配的相同“作业”和“实例”值,以便我可以使用仪表板为所选服务器提取正确数据的变量?

在此处输入图像描述

在此处输入图像描述

标签: prometheusgrafanaprometheus-pushgateway

解决方案


您可以在抓取后使用metric_relabel_configs重写标签。一个例子:

- job_name: pushgateway
  # This is necessary if metrics in pushgateway have "job" or "instance" labels.
  # With "honor_labels: true" Prometheus will save those as "exported_job" and "exported_instance" respectively.
  honor_labels: true
  static_configs:
  - targets:
    - my-pushgateway.com:9091
  metric_relabel_configs:
  # copy pushgateway address from "instance" to "source" label
  - source_labels: [instance]
    target_label: source

  # replace "instance" label value with one from "exported_instance" label
  - source_labels: [exported_instance]
    target_label: instance

  # remove "exported_instance" label
  - source_labels: [exported_instance]
    action: labeldrop

如果您以前有这样的指标:

my_metric{job="pushgateway", instance="my-pushgateway.com:9091", exported_instance="example.com"}

然后使用上面示例中的配置,它们将如下所示:

my_metric{job="pushgateway", instance="example.com", source="my-pushgateway.com:9091"}

推荐阅读