写点什么

Prometheus relabel 透析与实战

用户头像
卓丁
关注
发布于: 1 小时前
Prometheus relabel 透析与实战

relabel_config官方文档


Relabeling 简介

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.【译】重定义标签是一种强大的工具,可以在刮取targets目标之前,动态的重写其(源)标签集。 在每个scrape_config中可以配置多个标签重写的快。它们会按照在配置文件中出现的先后顺序而作用与每个目标的标签集。
复制代码


relabel_configs 的几种 action

<relabel_action> determines the relabeling action to take:

  • replace: Match regex against the concatenated source_labels. Then, set target_label to replacement, with match group references (${1}${2}, ...) in replacement substituted by their value. If regex does not match, no replacement takes place.

  • keep: Drop targets for which regex does not match the concatenated source_labels.

  • drop: Drop targets for which regex matches the concatenated source_labels.

  • hashmod: Set target_label to the modulus of a hash of the concatenated source_labels.

  • labelmap: Match regex against all label names. Then copy the values of the matching labels to label names given by replacement with match group references (${1}${2}, ...) in replacement substituted by their value.

  • labeldrop: Match regex against all label names. Any label that matches will be removed from the set of labels.

  • labelkeep: Match regex against all label names. Any label that does not match will be removed from the set of labels.

前置概念介绍

  • source_labels

源标签,指没有经过relabel处理之前的标签名;

  • target_label

目标标签,指经过relabel的相关action处理并生效后生成的新标签

源码介绍

Relabel_config

// Config is the configuration for relabeling of target label sets.type Config struct {	// A list of labels from which values are taken and concatenated	// with the configured separator in order.	SourceLabels model.LabelNames `yaml:"source_labels,flow,omitempty"`	// Separator is the string between concatenated values from the source labels.	Separator string `yaml:"separator,omitempty"`	// Regex against which the concatenation is matched.	Regex Regexp `yaml:"regex,omitempty"`	// Modulus to take of the hash of concatenated values from the source labels.	Modulus uint64 `yaml:"modulus,omitempty"`	// TargetLabel is the label to which the resulting string is written in a replacement.	// Regexp interpolation is allowed for the replace action.	TargetLabel string `yaml:"target_label,omitempty"`	// Replacement is the regex replacement pattern to be used.	Replacement string `yaml:"replacement,omitempty"`	// Action is the action to be performed for the relabeling.	Action Action `yaml:"action,omitempty"`}  
复制代码


最佳实际

  • 环境准备

macOs + Prometheus 2.26

  • 配置文件清单

  • prometheus.yaml

/etc/promethue/prometheus.yml

global:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s  scrape_configs:  - job_name: 'system:metrics'    file_sd_configs:      - files:        - /etc/prometheus/instance/targets.yml
复制代码
  • targets.yaml

/etc/prometheus/instance/targets.yml

- targets:  - 10.162.37.123:9100  labels:    idc: "HB-HLY"    area: "HB-HLY"    hostName: "host01"- targets:  - 10.162.38.31:9100  labels:    idc: "HB-HLY"    area: "HB-HLY"    hostName: "host02"- targets:  - 10.143.42.65:9100  labels:    idc: "BJ-TC"    area: "BJ-TC"    hostName: "host04"
复制代码
  • 启动与加载

注意:请将--storage.tsdb.path替换为你自己实际的路径,将--web.external-url 替换为自己机器的ip地址;这里暂省略.

/usr/local/bin/prometheus \ --storage.tsdb.retention.time=1d \--storage.tsdb.path=/Users/xxx/prometheus/data \--web.listen-address=0.0.0.0:9090 \--web.external-url=http://xxx.xxx.xxxx.xxx:9090 \--config.file=/etc/prometheus/prometheus.yml \--query.timeout=10m --log.level=info 
复制代码

启动加载效果如下


  • 各类 action 实战与特性验证

官方经典示例

  • replace

  • 用途说明

用正则匹配并提取原标签中的值,然后将相关部分作为目标标签的值,目标标签可以是已不存在的标签或新标

  • 配置前


  • 相关配置项

global:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml
### replacement expample relabel_configs: - action: replace source_labels: ['__address__'] regex: '(.+):9100' replacement: '${1}' target_label: 'ip'
复制代码
  • 配置生效后

  • replace 引申

假如被替换的目标标签是一个已经存在的标签,同样有效,验证如下:

  • 相关配置项

global:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml
### replacement expample relabel_configs: - action: replace source_labels: ['idc'] regex: '(.+)-(.+)' replacement: '${1}' target_label: 'area'
复制代码
  • 配置生效后

  • keep

  • 用途说明

如果相关的源标签的值跟给定的正则表达式不匹配,则放弃对该target 的刮取(scrape).

  • 配置前

  • 相关配置项

global:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml ### keep expample :only remain target in HB-HLY idc. relabel_configs: - action: keep source_labels: ['idc'] regex: 'HB-HLY'
复制代码
  • 配置生效后


  • drop

  • 用途说明

如果相关的源标签的值跟给定的正则表达式匹配,则放弃对该target的刮取(scrape).

  • 配置前

  • 相关配置项

obal:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml ### drop expample :only remain targets other than in HB-HLY idc. relabel_configs: - action: drop source_labels: ['idc'] regex: 'HB-HLY'
复制代码
  • 配置生效后


  • hashmod

  • 用途说明

Set target_label to the modulus of a hash of the concatenated source_labels.

针对hashmode的配置貌似有点模模糊糊,直接翻Prometheus源码:

https://github.com/prometheus/prometheus/blob/main/pkg/relabel/relabel.go#L196
https://github.com/prometheus/prometheus/blob/main/pkg/relabel/relabel.go#L33
https://github.com/prometheus/prometheus/blob/main/pkg/relabel/relabel.go#L261
取模算法: mod := sum64(md5.Sum([]byte(val))) % cfg.Modulus
用途补充:用某些标签做hash并取模,这样就可以让目标按此标签生成新的hash标签,可以按新生成hash标签对targets做进一步的区分;
复制代码
  • 配置前

  • 相关配置项

obal:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml relabel_configs: ### hashmod example - action: hashmod source_labels: ['idc'] modulus: 5 ## hash step. target_label: 'idc_hash_mod'
复制代码
  • 配置生效后


  • labeldrop

  • 用途说明

将跟正则表达式匹配的标签直接过滤(抛弃)掉;

  • 配置前


  • 相关配置项

global:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml relabel_configs: - action: labeldrop regex: '(idc|area)'
复制代码
  • 配置生效后


  • labelkeep

  • 用途说明

`将跟正则表达式不匹配的标签直接过滤(抛弃),仅保留可匹配上的;

  • 配置前

  • 相关配置项

global:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml
relabel_configs: - action: labelkeep regex: '(instance|job|__address__|__meta_filepath|__metrics_path__|__scheme__|hostName)' ##!!!记住,__xxx__类的源标签必须保留,比如 __address__等
复制代码
  • 配置生效后


  • labelmap

  • 用途说明

labelmap会根据regex去匹配Target实例所有标签的名称(注意是名称),并且将匹配到的部分作为为新标签的名称,匹配到标签的的值作为新标签的值

  • 配置前


  • 相关配置项

global:  scrape_interval:     15s # Set the scrape interval to every 10 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 10 seconds. The default is every 1 minute.  scrape_timeout: 10s
scrape_configs: - job_name: 'system:metrics' file_sd_configs: - files: - /etc/prometheus/instance/targets.yml relabel_configs: - action: labelmap regex: '(.+)Name'
复制代码
  • 配置生效后


发布于: 1 小时前阅读数: 12
用户头像

卓丁

关注

鸟过无痕 2017.12.10 加入

泰戈尔:虽然天空没有留下我的痕迹,但我已飞过。

评论

发布
暂无评论
Prometheus relabel 透析与实战