普罗米修斯动态metrics_path [英] prometheus dynamic metrics_path

查看:1730
本文介绍了普罗米修斯动态metrics_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Prometheus允许我从像这样的.json文件中使用file_sd_config动态加载目标

Prometheus allows me to dynamically load targets with file_sd_config from a .json file like this

#prometheus.yaml
- job_name: 'kube-metrics'
  file_sd_configs:
  - files:
    - 'targets.json'

[
  {
    "labels": {
      "job": "kube-metrics"
    },
    "targets": [
      "http://node1:8080",
      "http://node2:8080"
    ]
  }
]

但是我的目标在metrics_path而不是主机上有所不同(我想为<kube-api-server>/api/v1/nodes/<node-name>/proxy/metrics/cadvisor上的每个kubernetes节点抓取度量标准),但是我只能在作业级别而不是每个目标上设置metrics_path.这是使用Prometheus甚至可以实现的,还是我必须编写自己的代码来抓取所有这些指标并将其导出到单个目标.我也找不到所有受支持的自动发现机制的列表,我在文档中错过了什么吗?

However my targets differ in the metrics_path and not the host (I want to scrape metrics for every kubernetes node on <kube-api-server>/api/v1/nodes/<node-name>/proxy/metrics/cadvisor) but I can only set the metrics_path at the job level and not per target. Is this even achievable with prometheus or do I have to write my own code to scrape all these metrics and export them at a single target. Also I couldn't find a list of all supported auto discovery mechanisms, did I miss something in the docs?

推荐答案

您可以使用

You can use relabel_config in Prometheus config to change __metrics_path__ label config.

原则是以host:port/path/of/metrics格式在目标中提供指标路径(注意:删除http://,它位于scrape_configscheme参数中)

The principe is to provide the metrics path in your targets under the form host:port/path/of/metrics (note: drop the http://, it is in scheme parameter of scrape_config)

[
  {
    "targets": [
      "node1:8080/first-metrics",
      "node2:8080/second-metrics"
    ]
  }
]

然后将相关的元标签替换为零件

And then replace the related meta-labels with the parts

- job_name: 'kube-metrics'
  file_sd_configs:
  - files:
    - 'targets.json'
  relabel_configs:
    - source_labels: [__address__]
      regex:  '[^/]+(/.*)'            # capture '/...' part
      target_label: __metrics_path__  # change metrics path
    - source_labels: [__address__]
      regex:  '([^/]+)/.*'            # capture host:port
      target_label: __address__       # change target

您可以在配置时已知的任何标签上重复使用此方法,以修改抓取的配置.

You can reuse this method on any label known at configuration time to modify the config of the scrape.

在Prometheus上,使用服务发现页面来检查您的配置是否已正确修改.

On Prometheus, use the service discovery page to check your config has been correctly modified.

服务发现的正式列表在配置文档:在索引中查找*_sd_config.

The official list of service discovery is in the configuration documentation: look for the *_sd_config in the index.

这篇关于普罗米修斯动态metrics_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆