简体   繁体   English

如何在普罗米修斯中使用relable_config为指标添加前缀

[英]How to add prefix to metrics using relable_config in prometheus

finally running Harvest2 in a Docker environment with Prometheus and Grafana.最终在带有 Prometheus 和 Grafana 的 Docker 环境中运行 Harvest2。

Problem is, there´s also other systems reporting in that same Prometheus DB and NetApp Harvest does not add a prefix on it´s metric names like netapp_ to every netapp metric.问题是,在同一个 Prometheus DB 中还有其他系统报告,而 NetApp Harvest 没有在它的指标名称上添加前缀,例如 netapp_ 到每个 netapp 指标。 It is such a pain in the ass finding the correct metrics this way.以这种方式找到正确的指标真是太麻烦了。

I would like to use the relable_config option of Prometheus_config as a workaround.我想使用 Prometheus_config 的 reable_config 选项作为解决方法。 At the moment i have the following configuration for the harvest pollers:目前,我对收获轮询器有以下配置:

 - job_name: harvest
    scrape_interval: 1m
    scrape_timeout: 1m
    metrics_path: /metrics
    relabel_configs:
    - action: replace
      source_labels: [__name__]
      regex: (.*)
      target_label: __name__
      replacement: 'netapp_$1'

    - action: keep
      source_labels:
      - "custom_labels"
      - "custom_labels"
      - "custom_labels"
      - "custom_labels"
      regex: '.+;.+;.+;.+'

    file_sd_configs:
     - refresh_interval: 10s
       files:
       - targets/harvest.yml

But this leads to the harvest pollers not showing up in Grafana/Prometheus at all.但这导致收获轮询器根本没有出现在 Grafana/Prometheus 中。 Any idea on how to add the required prefix?关于如何添加所需前缀的任何想法?

The are two problems with this configuration but they have the same cause.此配置有两个问题,但它们的原因相同。 Furthermore, it's likely that adding prefix might not be the best idea in this case.此外,在这种情况下,添加前缀可能不是最好的主意。 Make sure you read the note at the end of this answer.请务必阅读本答案末尾的注释。

The problem is that relabel_configs contain relabel configurations that should be applied before scraping .问题是relabel_configs包含应该在抓取之前应用的重新标记配置。 For example, you can change __address__ , so that Prometheus will contact some another host instead of that given by service discovery.例如,您可以更改__address__ ,以便 Prometheus 将联系另一个主机,而不是服务发现提供的主机。 Naturally, __name__ is not available at this time because Prometheus has not scraped anything yet .当然, __name__不可用,因为Prometheus 还没有__name__任何东西

The solution is to move __name__ to metric_relabel_configs .解决方案是将__name__移动到metric_relabel_configs At this step metrics are already collected but not yet ingested and you can change their __name__ .在这一步,指标已经收集但尚未摄取,您可以更改它们的__name__ Here is an example that adds super_ prefix to all scraped metrics:这是一个为所有super_指标添加super_前缀的示例:

metric_relabel_configs:
- source_labels: [__name__]
  target_label: __name__
  replacement: super_$1

Pretty much the same is with the second relabeling in the question:问题中的第二次重新标记几乎相同:

    - action: keep
      source_labels:
      - "custom_labels"
      - "custom_labels"
      - "custom_labels"
      - "custom_labels"
      regex: '.+;.+;.+;.+'

Metrics are not yet scraped and so there are no custom labels yet.指标尚未抓取,因此尚无自定义标签。 This effectively drops all targets because none has the mentioned labels.这有效地删除了所有目标,因为没有提到的标签。 If you think you need this relabeling (it is not required for adding a prefix to label names), you have to put it under metric_relabel_configs .如果您认为需要重新标记(不需要为标签名称添加前缀),则必须将其放在metric_relabel_configs下。

Why adding a prefix to label names might be not the best idea为什么为标签名称添加前缀可能不是最好的主意

The problem is that because of different label names you will not be able to use the same dashboard/alerts with renamed metrics.问题在于,由于标签名称不同,您将无法使用具有重命名指标的相同仪表板/警报。 You will have to make separate dashboards for these metrics or use weird and ineffective queries, such as this:您必须为这些指标制作单独的仪表板或使用奇怪且无效的查询,例如:

{__name__=~".*my_metric_without_prefix"}

It might be better to simply add a label that will differentiate one set of metrics from the other one.最好简单地添加一个标签来区分一组指标和另一组指标。 You can either add your own new label or use different jobs to scrape those metrics.您可以添加自己的新标签或使用不同的作业来抓取这些指标。 In the last case you can distinguish one set of the metrics from the other by job label.在最后一种情况下,您可以通过job标签将一组指标与另一组指标区分开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM