目 录CONTENT

文章目录

Prometheus系列04-labels标签管理和metric指标管理

cplinux98
2022-08-30 / 0 评论 / 0 点赞 / 1,404 阅读 / 2,213 字 / 正在检测是否收录...

00:文章简介

介绍prometheus的labels标签管理和metric指标管理。

01:什么是标签

Prometheus上可以对主机设置labels,便于我们在筛选的时候快速定位一个或多个同类型的主机。

标签有2种类型

  • 私有标签
    • 以"__label_name"样式存在,是获取监控目标的默认元数据属性
      • “__address__” 记录获取目标的地址
      • "__scheme__"记录获取目标的请求协议方法
      • "__metrics_path__"记录获取目标metrics的uri地址
  • 普通标签
    • 一般都是我们自定义的标签,或者metrics中自动生成的标签
    • 对于这些标签,我们应该
      • 删除不必要或敏感的标签
      • 添加、编辑、修改需要的标签

image-20220319195653133

标签和指标流程:

image-20220318160059668

  • 重新标记relable_configs时
    • 只是对主机的target进行配置
  • 重新标记metric_relable_configs
    • 只是对主机的metric进行配置

02:添加初始target标签

我们可以在提供资源清单时,对Job进行统一标签

- targets:
  - 192.168.31.11:9100
  - 192.168.31.12:9100
  - 192.168.31.13:9100
  - 192.168.31.21:9100
  - 192.168.31.22:9100
  - 192.168.31.23:9100
  labels:
    app: node_exporter

03:管理target标签

我们也可以在prometheus配置文件中,对某一个job中的target标签进行增加、修改、删除、映射

官网文档:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config

配置示例如下:

scrape_configs:
  - job_name: "nodes"
    metrics_path: '/metrics'
    static_configs:
      - targets: 
        - '192.168.31.11:9100'
    relabel_configs:
    - source_labels: [<label_name> [,...]]  # 原有标签,使用完整名称表达
      regex: '<regex> | default = (.*)'     # 存在source_labes时,表示匹配对应的值,不存在时,表示匹配source_labes的名字
      replacement: '<string> | default = $1' # 替换标签时,标签对应的值
      target_label: '<label_name>'          # 替换后的标签 
      action: '<relabel_action> | default = replace' # 标签管理的动作

relabel_config 和 metric_relabel_configs的区别

  • relabel_config
    • 用于发现目标前的标签设置,针对于target对象,最后是显示在target中
  • metric_relabel_configs
    • 用于target获取完成后,针对于metric中的监控数据,最后是作用于metric中
    • 它是在Prometheus保存数据前,对标签进行重新编辑

常见的action动作有:

  • replace,默认,通过regex匹配source_label的值,使用replacement来进行修改值,或target_label修改名称
  • keep,删除所有与regex不匹配的source_labels的target
  • drop,删除与regex匹配的source_labels的target,就是不对该target进行监控
  • labelkeep,删除regex不匹配的标签
  • labeldrop,删除regex匹配的标签
  • labelmap,将regex匹配的所有标签名中的replacement放置在指定的标签名中,类似与copy + rename

3.1:替换标签

在targets中,一般不会显示带有双下划线的label,如果我们想要在labels中显示,可以使用replace,replace后的label就可以在labels中显示。

  - job_name: "from cmdb"
    metrics_path: '/metrics'
    http_sd_configs:
    - url: http://192.168.31.89:80/api/hosts
      refresh_interval: 10s
    relabel_configs:
    - source_labels:
      - __scheme__
      - __address__
      - __metrics_path__
      regex: '(http|https)(.*)'
      separator: ""
      target_label: "endpoint"
      replacement: '${1}://${2}'
      action: replace

重启prometheus查看结果

image-20220319202429877

对比之前

image-20220319195653133

3.2:删除标签

  - job_name: "from cmdb"
    metrics_path: '/metrics'
    http_sd_configs:
    - url: http://192.168.31.89:80/api/hosts
      refresh_interval: 10s
    relabel_configs:
    - regex: '(job)'
      action: labeldrop

重启prometheus查看结果

image-20220319204214383

3.3:根据标签删除target

  - job_name: "from cmdb"
    metrics_path: '/metrics'
    http_sd_configs:
    - url: http://192.168.31.89:80/api/hosts
      refresh_interval: 10s
    relabel_configs:
    - source_labels:
      - __address__
      regex: '192\.168\.31\.11.*'
      action: drop

image-20220319213622108

3.4:映射标签

映射标签类似于copy + rename,保留原标签和值

  - job_name: "from cmdb"
    metrics_path: '/metrics'
    http_sd_configs:
    - url: http://192.168.31.89:80/api/hosts
      refresh_interval: 10s
    relabel_configs:
    - regex: '(job)'
      replacement: '${1}_name'
      action: labelmap

image-20220319204517743

3.5:重命名标签

重命名就是映射后删除


  - job_name: "from cmdb"
    metrics_path: '/metrics'
    http_sd_configs:
    - url: http://192.168.31.89:80/api/hosts
      refresh_interval: 10s
    relabel_configs:
    - regex: '(job)'
      replacement: '${1}_name'
      action: labelmap
    - regex: '(job)'
      action: labeldrop

image-20220319204743844

04:管理metric指标

我们也可以在prometheus配置文件中,对某一个job中的target获取到的metric指标进行增加、修改、删除、映射

官网文档:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#metric_relabel_configs

配置示例如下:

scrape_configs:
  - job_name: "nodes"
    metrics_path: '/metrics'
    static_configs:
      - targets: 
        - '192.168.31.11:9100'
    metric_relabel_configs:
    - source_labels: [<label_name> [,...]]  # 原有指标名,使用完整名称表达
      regex: '<regex> | default = (.*)'     # 存在source_labes时,表示匹配对应的值,不存在时,表示匹配source_labes的名字
      replacement: '<string> | default = $1' # 替换指标值时,指标对应的值
      separator: ""  													# replacement操作时,指标值的连接符
      target_label: '<label_name>'          # 替换后的指标名
      action: '<relabel_action> | default = replace' # 标签管理的动作

relabel_config 和 metric_relabel_configs的区别

  • relabel_config
    • 用于发现目标前的标签设置,针对于target对象,最后是显示在target中
  • metric_relabel_configs
    • 用于target获取完成后,针对于metric中的监控数据,最后是作用于metric中
    • 它是在Prometheus保存数据前,对标签进行重新编辑

常见的action动作有:

  • replace,默认,通过regex匹配source_label的值,使用replacement来进行修改值,或target_label修改名称
  • keep,删除所有与regex不匹配的source_labels的target
  • drop,删除与regex匹配的source_labels的target
  • labelkeep,删除regex不匹配的标签
  • labeldrop,删除regex匹配的标签
  • labelmap,将regex匹配的所有标签名中的replacement放置在指定的标签名中,类似与copy + rename

4.1:了解metric组成

这部分有关于PromQL,详细的可以去看PromQL的文章,这里只简单介绍一下在管理metric时用到的知识点。

Metric Name的表示方式有2种:

  • 指标名称可以使用字母、数字、下划线,且必须能匹配RE2规范的正则表达式
  • 以"__"为前缀的名称为Prometheus系统预留使用

正常表示

#|<- Metric Name  ->|    |<-        Labels        ->|  
http_requests_total      {status="200",method="GET"}

Prometheus内部表示

#|<- Metric Name  ->|    |<-        Labels        ->|  
__name__="http_requests_total"      {status="200",method="GET"}

所以在匹配prometheus指标名称时,有2种方式

# 1
- source_labels:
  - __name__
  regex: 'http_requests_total'
# 意思是匹配__name__='http_requests_total' 的指标
  
# 2
- source_labels:
  - http_requests_total
  regex: '.*'
# 意思是匹配http_requests_total指标名,且指标的值为任意值

4.2:删除metric指标

删除指标通常用于简化metrics,避免额外的开销

  - job_name: "from cmdb"
    metrics_path: '/metrics'
    http_sd_configs:
    - url: http://192.168.31.89:80/api/hosts
      refresh_interval: 10s
    metric_relabel_configs:
    - source_labels:
      - __name__    # 标识指标名称的预留标签
      regex: 'node_network_receive_bytes_total'
      action: drop

!> 这里的效果可能会因为时间关系延迟生效,可以等待2-3分钟后观察

image-20220319230744328

4.3:替换metric指标

重命名的原因可能是,不同的业务类型中,对于指标的名称可能不同,但指标的意义却相同,比如

A:
  pod_xxxxx
B:
  container_xxx
这时我们就可以规范这些名称,统一为pod,方便使用query进行查询统计
  metric_relabel_configs:
  - source_labels: 
    - pod
    separator: ;
    regex: (.+)
    target_label: pod
    replacement: $1
    action: replace
  - source_labels: 
    - container
    separator: ;
    regex: (.+)
    target_label: pod
    replacement: $1
    action: replace

05:自定义metric指标

对于一些业务需要兼容prometheus时,我们可以对业务中开放metric接口,此时就需要我们自定义这个metric了。

以python的prometheus client为例演示

官方文档:https://github.com/prometheus/client_python

from prometheus_client import start_http_server, Counter, Summary, Gauge
# start_http_server 启动http服务,并在/metrics uri中提供自定义指标的访问

# 自定义指标,('指标名称', '指标描述')返回一个可以对指标值进行操作的对象
REQUEST_TIME = Summary('linux98_processing_seconds', 'Time spect')
g = Gauge('linux98_gauge', 'description')
counter_TIME = Counter('linux98_request_count', 'Time spect')

start_http_server(9100)

while True:
  # 由于start_http_server是不阻塞的,运行起来就掉了,这里使用while来阻塞
    counter_TIME.inc()  # 对counter单调递增
    g.set('123456')    # 对gauge设置值

其他的客户端操作请看prometheus的github主页

https://github.com/prometheus/?q=client&type=all&language=&sort=

0

评论区