Prometheus 运维工具 Promtool (三) Debug 功能

Promtool 在 debug 方面一个有 3 个子命令,分别用来获取 profiling debug 信息、获取 metric debug 信息、获取所有 debug 信息、对规则文件进行单元测试,接下来我们依次看一下。

获取 profiling debug 信息

使用 这个命令来获取 Prometheus 的 profiling 性能数据。命令的参数如下:

promtool debug pprof <server>

使用这个命令来创建一个文件看一下。

[root@Erdong-Test ~]# ./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".

[root@Erdong-Test ~]# 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 工具来进行分析。

获取 metric debug 信息

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

promtool debug metrics <server>

我们来使用一下这个命令

[root@Erdong-Test ~]# ./promtool debug metrics 'http://127.0.0.1:9090'
collecting: http://127.0.0.1:9090/metrics
Compiling debug information complete, all files written in "debug.tar.gz".
[root@Erdong-Test ~]# tar -zxf debug.tar.gz
tar: metrics.txt:不可信的旧时间戳 1970-01-01 08:00:00
[root@Erdong-Test ~]# tail metrics.txt
# TYPE prometheus_web_federation_warnings_total counter
prometheus_web_federation_warnings_total 0
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# 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"} 519347
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

获取所有 debug 信息

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

promtool debug all <server>

尝试执行一下:

[root@Erdong-Test ~]# ./promtool debug all 'http://127.0.0.1:9090'
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/threadcreate
collecting: http://127.0.0.1:9090/debug/pprof/trace?seconds=30
collecting: http://127.0.0.1:9090/metrics
Compiling debug information complete, all files written in "debug.tar.gz".

[root@Erdong-Test ~]#  tar -zxf debug.tar.gz
tar: cpu.pb:不可信的旧时间戳 1970-01-01 08:00:00
tar: block.pb:不可信的旧时间戳 1970-01-01 08:00:00
tar: goroutine.pb:不可信的旧时间戳 1970-01-01 08:00:00
tar: heap.pb:不可信的旧时间戳 1970-01-01 08:00:00
tar: mutex.pb:不可信的旧时间戳 1970-01-01 08:00:00
tar: threadcreate.pb:不可信的旧时间戳 1970-01-01 08:00:00
tar: trace.pb:不可信的旧时间戳 1970-01-01 08:00:00
tar: metrics.txt:不可信的旧时间戳 1970-01-01 08:00:00
[root@Erdong-Test ~]# ls
block.pb  debug.tar.gz  heap.pb      mutex.pb         trace.pb
cpu.pb    goroutine.pb  metrics.txt  threadcreate.pb

可以看到和前边的执行问你家是一样的。

测试规则文件

Promtool 还可以对规则文件进行单元测试,命令用法如下:

promtool test rules <test-rule-file>...

我们随便找一个告警规则文件执行看一下。

[root@Erdong-Test ~]# ./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 则是对规则文件进行单元测试。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Prometheus 是一个流行的开源监控系统,广泛应用于云原生环境中。以下是一些常用的 Prometheus 运维操作指南: 1. 安装和配置 Prometheus:下载 Prometheus 的二进制文件,创建配置文件 prometheus.yml,配置监控目标和告警规则。 2. 监控目标管理:Prometheus 可以监控多种目标,包括 HTTP、TCP、UDP 等服务。在 prometheus.yml 中添加新的目标,并通过 Prometheus 的 Web 界面查看监控指标。 3. 查询和可视化监控指标:Prometheus 提供了强大的查询语言 PromQL,可以用于查询和聚合监控指标。Grafana 是一个流行的监控可视化工具,可以与 Prometheus 集成,提供丰富的可视化功能。 4. 告警设置和管理:Prometheus 可以基于监控指标的阈值设置告警规则,并通过 Alertmanager 发送告警通知。在 prometheus.yml 中配置告警规则,并设置 Alertmanager 的通知方式。 5. 数据备份和恢复:Prometheus 的数据存储在本地硬盘上,默认情况下使用本地文件系统作为存储后端。为了防止数据丢失,需要定期备份数据文件并进行恢复测试。 6. 性能调优和容量规划:Prometheus 的性能与存储容量密切相关,需要对系统进行性能调优和容量规划。例如,可以调整采样频率、增加存储容量、使用分布式存储等方式提高系统性能和可扩展性。 以上是一些常用的 Prometheus 运维操作指南,但是还有很多细节需要注意,例如安全性、高可用性、自动化部署等方面。建议运维人员在使用 Prometheus 之前,对其进行深入学习和实践。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值