在CentOS上对Golang应用进行性能监控,可采用以下方式:
pprof内置工具
net/http/pprof包,启动HTTP服务(如http.ListenAndServe(":6060", nil)),通过go tool pprof命令或浏览器访问http://localhost:6060/debug/pprof/获取性能数据,支持生成火焰图、内存分析等。import _ "net/http/pprof"
func main() {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
// 业务代码
}
Prometheus+Grafana
yum安装Prometheus和Grafana,编辑prometheus.yml添加Go应用监控目标(如targets: ['localhost:8080'])。prometheus/client_golang库,暴露/metrics端点。系统级工具
htop需通过yum install htop安装。yum install sysstat安装。第三方工具
选择建议: