prometheus标签重写

    prometheus加载target实例后,会存在一些默认的标签,如下

 

常见内置标签
__address__              #当前Target实例的访问地址<host>:<port>
__scheme__               #采集目标服务访问地址的HTTP Scheme,HTTP或者HTTPS
__metrics_path__         #采集目标服务访问地址的访问路径
__param_<name>           #采集任务目标服务的中包含的请求参数

prometheus采集的数据中都包含了instance的标签,这些标签的内容对应到target实例中的__address__的标签,这里实际上是做 了一次标签的重写操作

relabel_config语法

relabel_configs: 
  [ source_labels: '[' <labelname> [, ...] ']' ]   ##源标签,指定对哪个现有标签进行操作
  [ separator: <string> | default = ; ]            ##多个源标签时连接的分隔符
  [ target_label: <labelname> ]                    ##要将源标签换成什么名字
  [ regex: <regex> | default = (.*) ]              ##怎么来匹配源标签,默认匹配所有
  [ modulus: <uint64> ]                            ##不怎么会用到
  [ replacement: <string> | default = $1 ]         ##替换正则表达式匹配到的分组,分组引用$1,$2,$3
  [ action: <relabel_action> | default = replace ] ##基于正则表达式匹配执行的操作,默认替换



action重新打标签动作
值                描述
replace                默认,通过正则匹配 source_label 的值,使用 replacement 来引用表达式匹配的分组
keep             删除 regex 于链接不匹配的目标 source_labels
drop             删除 regex 与连接匹配的目标 source_labels
labeldrop        匹配 Regex 所有标签名称
labelkeep        匹配 regex 所有标签名称
hashmod                设置 target_label 为 modulus 连接的哈希值 source_lanels
labelmap        匹配 regex 所有标签名称,复制匹配标签的值分组,replacement 分组引用 (${1},${2}) 替代

 

列子

1、将内置的标签__address__ 重写为test,而值则是直接继承之前的值
relabel_configs:
      - source_labels: [ '__address__' ]
        target_label:  test


2、将内置的标签__address__ 重写为test,值为自定义
relabel_configs:
      - source_labels: [ '__address__' ]
        target_label:  'test'
        replacement: 'localhost'  #自定义重写后标签的值

3、设置多个源标签
通过separator: _声明分隔符,来对目标标签的值更加精准的定义
relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target,group,module]
      separator: _
      target_label: instance

4、正则重写
relabel_configs:
      - source_labels: [ '__address__' ]
        target_label:  'test'
        regex: "(.*):(.*)"           #添加
        replacement: $1-$2           #修改

说明:正则匹配到$1 为127.0.0.1 $2 为9100,那么新的值为127.0.0.1-9100

 

- job_name: 'blackbox'
    scrape_interval: 45s
    metrics_path: /probe
    params:
      module: [icmp]  # Look for a HTTP 200 response.
    file_sd_configs:
    - files:
      - ./blackbox/*.yml
      refresh_interval: 10s
    relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target,group,module]
      separator: _
      target_label: instance
    - target_label: __address__
      replacement: 10.150.2.6:9115

 

posted @ 2022-02-17 17:02  泉love水  阅读(528)  评论(0编辑  收藏  举报