0
点赞
收藏
分享

微信扫一扫

Prometheus监控之检查工具Promtool Debug

一、概述

属于这个类别的子命令允许从运行的Prometheus服务器提取调试数据,以便对其进行分析。接下来我们将演示如何使用它们。
Promtool 在 debug 方面一个有 3 个子命令,
分别用来获取profiling debug信息、获取metric debug信息、获取所有debug信息、对规则文件进行单元测试

二、Debug

1、获取profiling debug信息

使用 这个命令来获取 Prometheus 的 profiling 性能数据。命令的参数如下: 
promtool debug pprof <server>

使用这个命令来创建一个文件看一下。
./promtool debug pprof 'http://127.0.0.1:9090'
collecting: http://127.0.0.1:9090/debug/pprof/threadcreate
collecting: http://127.0.0.1:9090/debug/pprof/profile?seconds=30
collecting: http://127.0.0.1:9090/debug/pprof/block
collecting: http://127.0.0.1:9090/debug/pprof/goroutine
collecting: http://127.0.0.1:9090/debug/pprof/heap
collecting: http://127.0.0.1:9090/debug/pprof/mutex
collecting: http://127.0.0.1:9090/debug/pprof/trace?seconds=30
Compiling debug information complete, all files written in "debug.tar.gz".

tar -zxf debug.tar.gz
[root@Erdong-Test ~]# ll -h
-rw-rw-rw- 1 root root 177 1月 1 1970 block.pb
-rw-rw-rw- 1 root root 5.9K 1月 1 1970 cpu.pb
-rw-rw-rw- 1 root root 7.4K 1月 1 1970 goroutine.pb
-rw-rw-rw- 1 root root 223K 1月 1 1970 heap.pb
-rw-rw-rw- 1 root root 177 1月 1 1970 mutex.pb
-rw-rw-rw- 1 root root 493 1月 1 1970 threadcreate.pb
-rw-rw-rw- 1 root root 73K 1月 1 1970 trace.pb
创建好的文件名是 debug.tar.gz ,
使用tar命令解压以后就会得到 block.pb 、cpu.pb、goroutine.pb、heap.pb、mutex.pb、threadcreate.pb、trace.pb 一共7个文件。
使用pprof,我们可以生成转储的镜像,在下一个代码片段中我们可以观察到这一点。
pprof -svg heap.pb > /vagrant/cache/heap.svg

2、获取metric debug信息

Promtool 的 debug metrics 子命令是下载 Prometheus 提供的 Metric 指标并且进行压缩。
这个命令平时很少使用,因为从 Prometheus 的端口中就可以直接获取这些 Metric 数据。
这个调试命令存在的意义是,当自己无法解决问题的时候,可以把 Metric 导出提供给其他分析人员进行分析调试,这个命令的参数结构如下:
promtool debug metrics <server>

promtool debug metrics 'http://prometheus:9090'
collecting: http://192.168.1.121:9090/metrics
Compiling debug information complete, all files written in "debug.tar.gz".

tar xzvf debug.tar.gz
metrics.txt

tail -n 5 metrics.txt
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 3786
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

可以看到这个命令生成了一个 debug.tar.gz 的压缩文件,这个文件解压后是 metrics.txt ,里面写满了 metric 。
将这个文件发给其他人,就可以知道当前 Prometheus 的状态。
这个命令和如下这个命令的效果一致。
curl 127.0.0.1:9090/metrics > metrics.txt

3、获取所有debug信息

Promtool 的 debug all 子命令就是将前边的两个子命令的结果合在一起,然后进行打包,这个子命令的使用方法如下:
promtool debug all <server>

promtool debug all 'http://prometheus:9090'
collecting: http://192.168.1.121:9090/debug/pprof/mutex
collecting: http://192.168.1.121:9090/debug/pprof/threadcreate
collecting: http://192.168.1.121:9090/debug/pprof/profile?seconds=30
collecting: http://192.168.1.121:9090/debug/pprof/block
collecting: http://192.168.1.121:9090/debug/pprof/goroutine
collecting: http://192.168.1.121:9090/debug/pprof/heap
collecting: http://192.168.1.121:9090/debug/pprof/trace?seconds=30
collecting: http://192.168.1.121:9090/metrics
Compiling debug information complete, all files written in "debug.tar.gz".

4、对规则文件进行单元测试

Promtool 还可以对规则文件进行单元测试,命令用法如下:
promtool test rules <test-rule-file>...

./promtool test rules promtsdb.rules.yaml
Unit Testing: promtsdb.rules.yaml
FAILED:
yaml: unmarshal errors:
line 1: field groups not found in type main.unitTestFile
出现了报错。这个时候我们就需要去查找错误的原因了。
前边我们遇到过 Promtool 的 check rules 子命令,这两个命令都是对规则文件进行检查的,
不同点是,check rules 只校验文件的语法是否存在错误,而 test rules 则是对规则文件进行单元测试。

举报

相关推荐

0 条评论