【问题标题】:How can we add extra label to Prometheus metrics?我们如何为 Prometheus 指标添加额外的标签?
【发布时间】:2017-12-04 07:34:57
【问题描述】:

假设我们在一个月内收集相同的指标,现在我们想要修改指标以具有额外的标签(在旧数据中也是如此),我们该怎么做。 现有指标:

mongodb_exporter_last_scrape_duration_seconds{instance="127.0.0.1:9216",job="mongo"}

想把它改成:

mongodb_exporter_last_scrape_duration_seconds{cluster="stage", instance="127.0.0.1:9216",job="mongo"}   

【问题讨论】:

    标签: prometheus prometheus-operator


    【解决方案1】:
    - job_name: 'your_job'                 
      honor_labels: true                         
      static_configs:                     
      - targets:                          
        - '127.0.0.1'          
        labels:                           
          cluster: 'stage'
    

    【讨论】:

    • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因
    • 这会将标签“stage”添加到新抓取的数据中。但我也想将该标签添加到以前收集的数据点。
    • 这是用于静态配置的,如果我必须为我的 kubernetes_sd_configs 添加标签,我应该在哪里添加这个附加标签?
    【解决方案2】:

    很遗憾,在 Prometheus 中无法更改旧指标的标签。

    存储仅由新的抓取更新,然后它变得不可变。

    【讨论】:

      【解决方案3】:

      如果你有服务发现配置,你也可以在 relabels_config 中添加标签

      relabel_configs:
        - target_label: cluster
          replacement: stage
      

      【讨论】:

        【解决方案4】:

        https://prometheus.io/docs/prometheus/latest/querying/functions/#label_replace https://medium.com/@texasdave2/replace-and-remove-a-label-in-a-prometheus-query-9500faa302f0

        其实 label_replace 方法只是给metrics添加一个新标签,而不是replace。因此,如果您不想更改 prometheus.yml 配置,我可以使用此方法添加新标签。

        对于您的情况:

        mongodb_exporter_last_scrape_duration_seconds{instance="127.0.0.1:9216",job="mongo"}

        label_replace(mongodb_exporter_last_scrape_duration_seconds{instance="127.0.0.1:9216",job="mongo"}, "cluster", "stage", "(.*)")

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多