运维别卷系列 - 云原生监控平台 之 03.prometheus label 实践

label 简介

  • label 对于 Prometheus 来说,属于数据处理的方式,Prometheus 是通过指定的 label 来查询数据

  • Prometheus 的 target 中实例,都会包含一些默认的 metadata label,比如基于 kubernetes 服务发现的会有类似 __meta_kubernetes_namespace="monitor" 这种 label

  • 下面就是上次咱们基于 kubernetes_sd_configs 服务发现加入的 node-exporter,可以看到,Prometheus 注入了这些 metadata label,这些是不会显示在 /metrics 页面里面的

在这里插入图片描述

自定义标签

针对于 Prometheus 联邦集群,自定义标签就可以很好的去区分环境,也可以细化查询的维度

  • 比如我基于之前的 Prometheus 配置文件,给 Prometheus 增加了一个 label
- job_name: prometheus
  metrics_path: '/metrics'
  static_configs:
  - targets: ['prometheus-svc:9090']
    labels:
      env: study

可以从 target 里面看到我们增加的 label

在这里插入图片描述

graph 里面也可以指定我们自定义的 label 来查询数据(这里因为就一个 Prometheus,就演示一下效果)

在这里插入图片描述

relabel_configs

relabel_configs

  • Relabeling is a powerful tool to dynamically rewrite the label set of a target before it gets scraped. Multiple relabeling steps can be configured per scrape configuration. They are applied to the label set of each target in order of their appearance in the configuration file.
  • 重新标记是一个强大的工具,可以在目标被抓取之前动态重写目标的标签集。每个抓取配置可以配置多个重新标记步骤。它们将按照它们在配置文件中的出现顺序应用于每个目标的标签集。
  • Initially, aside from the configured per-target labels, a target’s job label is set to the job_name value of the respective scrape configuration. The __address__ label is set to the : address of the target. After relabeling, the instance label is set to the value of __address__ by default if it was not set during relabeling. The __scheme__ and __metrics_path__ labels are set to the scheme and metrics path of the target respectively. The __param_ label is set to the value of the first passed URL parameter called .
  • 最初,除了配置的每个目标标签外,目标的 job 标签设置为相应抓取配置的 job_name 值。__address__ 标签设置为目标的 <host>:<port> 地址。重新标记后,如果在重新标记期间未设置 instance 标签,则默认设置为 __address__ 值。__scheme____metrics_path__ 标签分别设置为目标的方案和指标路径。__param_ 标签设置为第一个传递的 URL 参数的值,称为 <name>
  • The __scrape_interval__ and __scrape_timeout__ labels are set to the target’s interval and timeout. This is experimental and could change in the future.
  • __scrape_interval____scrape_timeout__ 标签设置为目标的间隔和超时。这是实验性的,将来可能会改变。
  • Additional labels prefixed with __meta_ may be available during the relabeling phase. They are set by the service discovery mechanism that provided the target and vary between mechanisms.
  • 在重新标记阶段,可以使用以 __meta_ 为前缀的其他标签。它们由提供目标的服务发现机制设置,并且因机制而异。
  • Labels starting with __ will be removed from the label set after target relabeling is completed.
  • 目标重新标记完成后,将从标签集中删除以 __ 开头的标签。
  • If a relabeling step needs to store a label value only temporarily (as the input to a subsequent relabeling step), use the __tmp label name prefix. This prefix is guaranteed to never be used by Prometheus itself.
  • 如果重新标记步骤只需要临时存储标签值(作为后续重新标记步骤的输入),请使用 __tmp 标签名称前缀。这个前缀保证永远不会被 Prometheus 本身使用。

regex

  • <regex> 是任何有效的 RE2 正则表达式。
  • replacekeepdroplabelmaplabeldroplabelkeep 操作需要它。
  • 正则表达式锚定在两端。要取消锚定正则表达式,请使用 .*<regex>.*

relabel_action

重新标记操作有以下几个

  • replace:将 regex 与连接的 source_labels 匹配。然后,将 target_label 设置为 replacement,将 replacement 中的匹配组引用 (${1}${2}, …) 替换为其值。如果 regex 不匹配,则不进行替换。
  • lowercase:将连接的 source_labels 映射到其小写字母。
  • uppercase:将连接的 source_labels 映射到其大写字母。
  • keep:丢弃 regex 与连接的 source_labels 不匹配的目标。
  • drop:丢弃 regex 与连接 source_labels 匹配的目标。
  • keepequal:丢弃与连接的 source_labels 不匹配的目标 target_label
  • dropequal:丢弃与连接的 source_labels 匹配 target_label 的目标。
  • hashmod:将 target_label 设置为串联 source_labels 的哈希值的 modulus
  • labelmap:将 regex 与所有源标签名称匹配,而不仅仅是 source_labels 中指定的名称。然后将匹配标签的值复制到 replacement 给出的标签名称,并将 replacement 中的匹配组引用 (${1}${2}, …) 替换为其值。
  • labeldrop:将 regex 与所有标签名称匹配。任何匹配的标签都将从标签集中删除。
  • labelkeep:将 regex 与所有标签名称匹配。任何不匹配的标签都将从标签集中删除。

必须注意 labeldroplabelkeep,以确保在删除标签后,指标仍是唯一标记的。

metric_relabel_configs

metric_relabel_configs

  • Metric relabeling is applied to samples as the last step before ingestion. It has the same configuration format and actions as target relabeling. Metric relabeling does not apply to automatically generated timeseries such as up.
  • metric_relabel_configs 应用于样本,作为摄取前的最后一步。它具有与 relabel_config 相同的配置格式和操作。指标重新标记不适用于自动生成的时间序列,例如 up
  • One use for this is to exclude time series that are too expensive to ingest.
  • 这样做的一个用途是排除成本太高而无法摄取的时间序列。

两者的区别

  • relabel_configs 在抓取(采集)之前对任何目标及其标签进行高级修改。
  • metric_relabel_configs 在摄取(存储)之前对任何目标及其标签进行高级修改。

实践

  • 同样,采用咱们之前部署 node-exporter 的配置文件。这里就取一小段做演示
  • 这里是将 __meta_kubernetes_namespace 重新标记成 kubernetes_namespace
  - source_labels: [__meta_kubernetes_namespace]
    action: replace
    target_label: kubernetes_namespace

这样,就可以看到新增了一个 label

在这里插入图片描述

就可以用这个重新标记的 label 来做查询了

在这里插入图片描述

相关推荐

  1. 监控系统

    2024-05-16 12:36:09       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-16 12:36:09       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-16 12:36:09       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-16 12:36:09       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-16 12:36:09       18 阅读

热门阅读

  1. 2024.5.15晚训题解

    2024-05-16 12:36:09       10 阅读
  2. 【转】VS(Visual Studio)更改文件编码

    2024-05-16 12:36:09       11 阅读
  3. Sping @Autowired @Value @Resourece依赖注入原理

    2024-05-16 12:36:09       13 阅读
  4. spark分布式预测和保存过程中遇到的问题记录

    2024-05-16 12:36:09       11 阅读
  5. OpenCV 实时目标检测

    2024-05-16 12:36:09       11 阅读
  6. AI技术在内容生产中的革新:效率与质量的双赢

    2024-05-16 12:36:09       12 阅读
  7. 学习MySQL(四):记录的增删改查

    2024-05-16 12:36:09       10 阅读