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

### 文章目录

  • [@[toc]](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [label 简介](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [自定义标签](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [relabel_configs](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [regex](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [relabel_action](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [metric_relabel_configs](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [两者的区别](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)
  • [实践](#文章目录 @[toc] label 简介 自定义标签 relabel_configs regex relabel_action metric_relabel_configs 两者的区别 实践)

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
yaml 复制代码
- 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
yaml 复制代码
  - source_labels: [__meta_kubernetes_namespace]
    action: replace
    target_label: kubernetes_namespace

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

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

相关推荐
胖大和尚15 分钟前
完整的 CentOS 6.10 虚拟机安装启动脚本
linux·运维·centos
酷爱码30 分钟前
Nginx的核心功能--正向代理、反向代理、缓存和Rewrite
运维·nginx·缓存
菜鸟康2 小时前
Linux——CMake的快速入门上手和保姆级使用介绍、一键执行shell脚本
linux·运维·服务器
金刚猿2 小时前
openfeign 拦截器实现微服务上下文打通
微服务·云原生·架构
卷卷的小趴菜学编程2 小时前
Linux系统之----基础IO
linux·运维·服务器·文件·fopen·文件操作符·位图传递标志位
lcw_lance2 小时前
技术中台-核心技术介绍(微服务、云原生、DevOps等)
微服务·云原生·devops
码上飞扬3 小时前
Java大师成长计划之第22天:Spring Cloud微服务架构
java·运维·云计算
重启就好3 小时前
【Ansible】之inventory主机清单
运维·ansible
Kusunoki_D4 小时前
速查 Linux 常用指令 II
linux·运维·服务器
xmweisi024 小时前
Ansible内置模块之 group
linux·运维·ansible·rhce·rhca·红帽认证