运维别卷系列 - 云原生监控平台 之 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
- 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
joblabel is set to thejob_namevalue of the respective scrape configuration. The__address__label is set to the:address of the target. After relabeling, theinstancelabel 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
__tmplabel name prefix. This prefix is guaranteed to never be used by Prometheus itself.- 如果重新标记步骤只需要临时存储标签值(作为后续重新标记步骤的输入),请使用
__tmp标签名称前缀。这个前缀保证永远不会被 Prometheus 本身使用。
regex
<regex>是任何有效的 RE2 正则表达式。replace、keep、drop、labelmap、labeldrop和labelkeep操作需要它。- 正则表达式锚定在两端。要取消锚定正则表达式,请使用
.*<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与所有标签名称匹配。任何不匹配的标签都将从标签集中删除。
必须注意
labeldrop和labelkeep,以确保在删除标签后,指标仍是唯一标记的。
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 来做查询了


浙公网安备 33010602011771号