kubernetes prometheus dynamic metrics_path

zbq4xfa0  于 2024-01-06  发布在  Kubernetes
关注(0)|答案(4)|浏览(153)

Prometheus允许我从一个.json文件中动态加载目标文件file_sd_config,如下所示:

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

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

u3r8eeie

u3r8eeie1#

您可以在Prometheus config中使用relabel_config来更改__metrics_path__标签配置。
原则是以host:port/path/of/metrics的形式在目标中提供指标路径(注意:删除http://,它在scrape_configscheme参数中)

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

字符串
然后将相关的元标签替换为

- 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


您可以在配置时对任何已知的标签重复使用此方法,以修改刮取的配置。
在Prometheus上,使用服务发现页面检查您的配置是否已正确修改。
服务发现的官方列表在配置文档中:在索引中查找*_sd_config

xe55xuns

xe55xuns2#

有一种稍微优雅的方法可以做到这一点,它不涉及操作地址。您可以为目标创建一个标签,该标签可以作为您的“重新标记”操作的源,类似于以下内容:

- labels:
    __meta_discovery_path: '/first-metrics'
  targets:
  - 'node1:8080'
- labels:
    __meta_discovery_path: '/second-metrics'
  targets:
  - 'node2:8080'

字符串
然后,您的重新标记将简单地:

relabel_configs:
    - source_labels: [__meta_discovery_path]
      target_label: __metrics_path__  # change metrics path


由于正在使用的标签以“__"开头,因此它将从正在提取的指标中剥离,使其变得美观和干净。

8iwquhpp

8iwquhpp3#

我想要的东西像下面的端点和实现与此配置

192.168.1.1:80/metrics/my-path1
192.168.1.2:80/metrics/my-path1

字符串
这将用_new_path值替换$1

- job_name: prometheus_dynamic_metrics_path
  honor_labels: true
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  follow_redirects: true
  relabel_configs:
  - source_labels: [_new_path]
    separator: ;
    regex: (.*)
    target_label: __metrics_path__
    replacement: /metrics/$1
    action: replace
  static_configs:
  - targets:
    - 192.168.1.1:80
    labels:
      _new_path: my-path1
  - targets:
    - 192.168.1.2:80
    labels:
      _new_path: my-path2

5jvtdoz2

5jvtdoz24#

您可以使用特殊标签在目标级别设置指标路径和方案。您甚至可以更改刮取间隔,超时和作业名称:

[
  {
    "targets": [ "yourhost:1234" ],
    "labels": {
      "job": "override job's name",
      "__metrics_path__": "/my/custom/path",
      "__scheme__": "https",
      "__scrape_interval__": "30s",
      "__scrape_timeout__": "30s"
    }
  }
]

字符串

相关问题