监控
本章介绍 Go-Sail 的监控功能。
前言
对于正在运行的应用服务而言,服务监控至关重要。它能够让应用的运行状态更加透明与清晰,同时对于性能优化以及在微服务环境下的弹性扩缩容都起到重要作用。
目前,Go-Sail 主要通过 Prometheus 和 PProf 两种方式实现服务的观测与遥测。
指标采集
Go-Sail 通过 Prometheus 实现对多项服务关键指标的监控。你可以通过暴露的指标端点收集这些数据,进行后续分析。目前采集的指标包括:
- HTTP 请求延迟
- HTTP 状态码计数
- CPU 使用率
- 内存使用率
- 磁盘使用率
- 网络流量
配置
如需启用指标监控功能,需要在配置文件中做好相应设置。以下为需要调整的配置片段:
config.example.yaml
http_conf:
debug: false
addr: ""
swagger_conf:
enable: false
redoc_ui_path: ""
json_path: ""
favicon_path: ""
prometheus_conf:
enable: true
addr: ""
access_path: ""
disable_system_sample: false
disk_path: ""
sample_interval: ""
指标采集方式
当配置完成后,Go-Sail 会自动启用 Prometheus 监控。但请注意,此监控模式不会主动推送数据,而是以暴露 access_path 的方式提供外部访问,你可以利用 Grafana 等工具进行数据采集与展示。
PProf
配置
PProf 的配置与 HTTP 服务设置相绑定。你只需在 HTTP 配置内将 debug 参数设为 true,即可自动开放 PProf 访问,访问路径为 /debug/pprof。
config.example.yaml
http_conf:
debug: true
addr: ""
swagger_conf:
enable: false
redoc_ui_path: ""
json_path: ""
favicon_path: ""
prometheus_conf:
enable: false
addr: ""
access_path: ""
disable_system_sample: false
disk_path: ""
sample_interval: ""
使用方式
PProf 支持以下内容的查看:
-
内存使用
/debug/pprof/heap -
CPU 分析
/debug/pprof/profile -
goroutine 状态
/debug/pprof/goroutine -
阻塞分析
/debug/pprof/block -
锁争用
/debug/pprof/mutex
你可以通过 go tool 工具做详细分析。例如:
go tool pprof http://localhost:8080/debug/pprof/heap