• Prometheus Label 标签管理


    前言

    prometheus监控体系中。标签label是一个极为重要的参数,考虑到要明智的使用标签,需要使用标准的标签对整个集群进行管理控制,特别是在复杂的环境中。

    一些常见的标签操作案例:

    • 重命名标签名
    • 删除标签
    • 过滤目标

    特别注意的是,上列操作,只有两个阶段我们可以对标签进行操作:

    1. 第一阶段是重新标记来自服务发现的目标。

    这对于服务发现的元数据标签的信息引用到度量上的标签费用游泳,这些将在relabel_configs模块中完成。

    1. 第二阶段是在刮刮scape之后,但在保存到存储系统之前。

    这使我们能够确保我们保存了那些指标,删除了那些指标,以及这些指标将是什么样子,这些工作将在metric_relabel_configs模块中完成

    其实就是:在发生在 scape 之前 使用 relabel_configs,发生在 scape 之后 使用 metric_relabel_configs.

    action:重新标签动作

    • replace:默认,通过regex匹配source_label的值,使用replacement来引用表达式匹配的分组
    • keep:删除regex与连接不匹配的目标 source_labels
    • drop:删除regex与连接匹配的目标 source_labels
    • labeldrop:删除regex匹配的标签
    • labelkeep:删除regex不匹配的标签
    • hashmod:设置target_label为modulus连接的哈希值source_labels
    • labelmap:匹配regex所有标签名称。然后复制匹配标签的值进行分组,replacement分组引用(${1},${2},…)替代

    **relabel_configs **

    relable_configs:
      # 源标签
      [ source_labels: '[' <labelname> [, ...] ']' ]
      
      # 多个源标签时连接的分隔符
      [ separator: <string> | default = ; ]
      
      # 重新标记的标签
      [ target_label: <labelname> ]
      
      # 整则表达式匹配源标签的值
      [ regex: <regex> | default = (.*) ]
      
      # 用的少,占时略
      [ modulus: <uint64> ]
      
      # 替换正则表达式匹配的分组,分组引用 $1,$2,$3,....
      [ replacement: <string> | default = $1 ]
      
      # 基于正则表达式匹配执行的操作
      [ action: <relabel_action> | default = replace ]
    

    配置测试

    删除metric值

    测试基于 cAdvisor 监控容器 文章。

    删除 container_tasks_state | container_memory_failures_total 这两个标签

    在配置前先查看这两个标签的数据是正常的。

    然后再进行配置:

    编辑 prometheus.yml 文件,增加下来内容

    [root@es01 config]# cat prometheus.yml
    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    alerting:
      alertmanagers:
      - static_configs:
        - targets:
    
    rule_files:
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
        - targets: ['localhost:9090']
    
      - job_name: 'cadvisor'
        metrics_path: '/metrics'
        static_configs:
        - targets: ['10.0.20.11:8080']
        relabel_configs:
        - source_labels: [__name__]     # 匹配原标签的名字
          separator: ','                # 分隔符,拥有就匹配原标签时,有多个标签,则用逗号 `,` 分割
          regex: '(container_tasks_state|container_memory_failures_total)'  # 匹配要删除的标签名称,如果有多个正则,用分号隔开`;`
          action: drop              # 匹配的动作
    

    重新加载配置文件后测试

    [root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
    Checking /opt/prometheus-2.14/config/prometheus.yml
      SUCCESS: 0 rule files found
    
    [root@es01 config]# curl -X POST http://10.0.20.11:9090/-/reload
    

    重载配置文件完成后,稍等便宜,当再次查询的时候,会发现 container_tasks_state | container_memory_failures_total 这两个标签已无法查询到数据

    更换

    其实所谓的更换,是匹配到对应的标签内容后,再次增加一个key来对应匹配的值

    测试

    编辑 prometheus.yml 文件,增加下来内容

    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    alerting:
      alertmanagers:
      - static_configs:
        - targets:
    
    rule_files:
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
        - targets: ['localhost:9090']
    
      - job_name: 'cadvisor'
        metrics_path: '/metrics'
        static_configs:
        - targets: ['10.0.20.11:8080']
        relabel_configs:
        - source_labels: [__name__]
          separator: ','
          regex: '(container_tasks_state|container_memory_failures_total)'
          action: drop
        - source_labels: [id]   # 匹配原标签
          regex: '/docker/([a-z0-9]+)' # 匹配对应的值
          target_label: container_id    # 新的标签的key值
          replacement: '$1'             # 替换的内容 value
          action: replace               # 动作为替换
    

    重新加载配置文件后测试

    [root@es01 config]# /opt/prometheus-2.14/bin/promtool check config /opt/prometheus-2.14/config/prometheus.yml
    Checking /opt/prometheus-2.14/config/prometheus.yml
      SUCCESS: 0 rule files found
    
    [root@es01 config]# curl -X POST http://10.0.20.11:9090/-/reload
    

    由此可以看出,已经新增了一个标签。

    删除 Label 标签

    在获取到的 metric 数据中的一些label,无用则可以通过匹配后删除

    编辑 prometheus.yml 文件,增加下来内容

    [root@es01 config]# cat prometheus.yml
    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    alerting:
      alertmanagers:
      - static_configs:
        - targets:
    
    rule_files:
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
        - targets: ['localhost:9090']
    
      - job_name: 'cadvisor'
        metrics_path: '/metrics'
        static_configs:
        - targets: ['10.0.20.11:8080']
        relabel_configs:
        - source_labels: [__name__]
          separator: ','
          regex: '(container_tasks_state|container_memory_failures_total)'
          action: drop
        - source_labels: [id]
          regex: '/docker/([a-z0-9]+)'
          target_label: container_id
          replacement: '$1'
          action: replace
        - regex: 'kernelVersion'    # 正则匹配标签
          action: labeldrop         # 执行动作删除匹配的标签
    
  • 相关阅读:
    数组实现栈
    栈应用实例单词逆序
    使用JXMapViewer将地图集成到swing app中
    使用xbee连接地面站和飞控
    QWT编译、配置、使用(Qt Creator)
    Qt跨线程调用错误解析及解决办法
    SVN版本服务器搭建(服务端+客户端)
    opencv配置过程 (cmake,vs2013,qt 5.4)
    基数排序/Go实现
    c/c++ 编译器内存对齐问题
  • 原文地址:https://www.cnblogs.com/winstom/p/11932092.html
Copyright © 2020-2023  润新知