【问题标题】:How can I add a label to the cadvisor and node-exporter metrics?如何为 cadvisor 和 node-exporter 指标添加标签?
【发布时间】:2021-01-04 09:35:30
【问题描述】:

我的节点导出器指标类似于:

process_cpu_seconds_total{instance="10.1.1.1:8080",job="node_info"}
process_cpu_seconds_total{instance="10.1.1.2:8080",job="node_info"}
process_cpu_seconds_total{instance="10.1.1.15:8080",job="node_info"}

顾问:

container_memory_usage_bytes{id="<id>",image="<image>",instance="10.1.1.1:8080",job="docker_info",name="<container name>"}
container_memory_usage_bytes{id="<id>",image="<image>",instance="10.1.1.3:8080",job="docker_info",name="<container name>"}
container_memory_usage_bytes{id="<id>",image="<image>",instance="10.1.1.16:8080",job="docker_info",name="<container name>"}

我想添加一个标签,例如machine_name,如下所示:

process_cpu_seconds_total{machine_name="cool_machine",instance="10.1.1.1:8080",job="node_info"}
container_memory_usage_bytes{machine_name="cool_machine",id="<id>",image="<image>",instance="10.1.1.1:8080",job="docker_info",name="<container name>"}

当我尝试按机器过滤时,我需要处理 IP (10.1.1.1),这对用户不是很友好。 我想配置 node-exporter 和 cadvisor 为所有指标添加标签,这样我就可以识别机器,无论它们现在拥有什么 IP。

顺便说一句,更改 DNS 以使机器在另一个地址中应答对我来说不是一个很好的选择。

我的 prometheus 配置类似于:

global:
  scrape_interval: 5s
  external_labels:
    monitor: 'machines_monitor'
scrape_configs:
  - job_name: 'node_info'
    static_configs:
      - targets:
          - 10.1.1.1:8080
          - 10.1.1.2:8080
          - 10.1.1.15:8080
  - job_name: 'docker_info'
    static_configs:
      - targets:
          - 10.1.1.1:8080
          - 10.1.1.3:8080
          - 10.1.1.16:8080

我可以为机器创建一个scrape_configs 并开始过滤,但我不知道这是否是个好主意,也许是 Prometheus 的性能问题。

我正在尝试为指标添加标签,但我非常欢迎其他有助于识别机器的方法。

【问题讨论】:

    标签: prometheus grafana prometheus-node-exporter cadvisor


    【解决方案1】:

    您可以尝试以下方法:

    scrape_configs:
      - job_name: 'node_info'
        static_configs:
          - targets:
              - 10.1.1.1:8080
              - 10.1.1.2:8080
              - 10.1.1.15:8080
        relabel_configs:
          - source_labels: [__address__]
            regex: '10\.1\.1\.1.+'
            replacement: cool_machine_1
            target_label: machine_name
          - source_labels: [__address__]
            regex: '10\.1\.1\.2.+'
            replacement: cool_machine_2
            target_label: machine_name
          ...
    

    【讨论】:

      【解决方案2】:

      我找到了解决方案,我可以使用 Prometheus metric_relabel_configs,我的配置是这样的:

      global:
        scrape_interval: 5s
        external_labels:
          monitor: 'machines_monitor'
      scrape_configs:
        - job_name: 'node_info'
          static_configs:
            - targets:
                - 10.1.1.1:8080
                - 10.1.1.2:8080
                - 10.1.1.15:8080
          metric_relabel_configs:
            - source_labels:
                - instance
              target_label: instance
              regex: '10\.1\.1\.1(.*)'
              action: replace
              replacement: cool_machine
      

      有一个相关的问题: Prometheus create label from metric label

      这里我们可以看到一些其他的例子: https://gist.github.com/trastle/1aa205354577ef0b329d4b8cc84c674a

      这是一个相关的帖子: https://www.robustperception.io/controlling-the-instance-label

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-21
        • 2021-10-27
        • 2022-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多