以下是在CentOS上监控Golang应用性能的常用方法:
pprof(内置工具)
net/http/pprof,启动HTTP服务(如http.ListenAndServe(":6060", nil)),通过go tool pprof命令或浏览器访问http://localhost:6060/debug/pprof/生成分析报告,支持CPU、内存、阻塞等维度。import _ "net/http/pprof"
func main() {
go func() { log.Println(http.ListenAndServe(":6060", nil)) }()
// 业务代码
}
Prometheus + Grafana(指标监控)
yum安装,编辑prometheus.yml添加Golang应用监控目标(如localhost:8080/metrics)。prometheus/client_golang库暴露指标(如http_requests_total、request_latency)。系统级工具
yum install htop)。第三方工具
选择建议:
pprof分析CPU/内存。