go可视化监控工具

pprof+charts+prometheus

https://www.toutiao.com/i6757508509522199048/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1&timestamp=1595292837&app=news_article&utm_source=weixin&utm_medium=toutiao_android&use_new_style=1&req_id=20200721085357010014047025265C1CC2&group_id=6757508509522199048

https://www.u-ya.net/blog/post/3977.html

debugcharts

https://blog.csdn.net/hello_ufo/article/details/93035052

https://studygolang.com/articles/28023

pprof

https://segmentfault.com/a/1190000019222661

expvar–非嵌入式

https://blog.csdn.net/jeffrey11223/article/details/78886923/

Go服务监控

使用Golang可以开发出高性能的HTTP、GRPC服务。一般项目运行后,我们也需要监控服务的性能或者进行调试。除了打日志,还有没有其他可视化的方案呢?答案是有的。

本文将会介绍几种常用的监控方案。

pprof

这个是go语言自带的。启用很简单:

_ "net/http/pprof"
仅需显式的在 main 包的 import 里增加上面一行即可。完整使用示例:

package main

import (
 "net/http"
 _ "net/http/pprof"
)

func main(){
 //提供给负载均衡探活以及pprof调试
 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 w.Write([]byte("ok"))
 })

 http.ListenAndServe(":10108", nil)
}

运行之后,在浏览器打开 http://127.0.0.1:10108/debug/pprof/就能看到监控的一些信息了:
在这里插入图片描述

注:生产环境一般开个协程:

go http.ListenAndServe(":10108", nil)

如何启动 PProf 可视化界面?

需要graphviz支持,可以到 http://www.graphviz.org/download/ 下载,并把bin加入到环境变量。Mac可以使用brew安装。

下面以heap为例:

方法一:

go tool pprof -http=:8081 http://localhost:10108/debug/pprof/heap
方法二:

go tool pprof http://localhost:10108/debug/pprof/heap
然后在交互式命令行输入web即可跳转到默认浏览器:

在这里插入图片描述

查看协程信息:
终端输入

go tool pprof -http=:8081 http://localhost:10108/debug/pprof/goroutine

自动跳转 浏览器显示
在这里插入图片描述
在这里插入图片描述

debugcharts

一个可以实时查看golang程序内存、CPU、GC、协程等变化情况的可视化工具。

跟pprof一样, import引入, 然后开端口监听就行了:

_ "github.com/mkevac/debugcharts"
http.ListenAndServe(":10108", nil)
//省略其它代码...

运行后,浏览器打开 http://localhost:10108/debug/charts/ 就能看到了:
实时监控
在这里插入图片描述

prometheus

prometheus是grafana的插件,支持go监控的可视化。

首先需要代码里引入包:

"github.com/prometheus/client_golang/prometheus/promhttp"
然后增加路由:

//prometheus
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":10108", nil)

配置grafana后,效果图:
在这里插入图片描述

一个端口开启 pprof+charts+prometheus

如果每一个监控都开一个端口就有点浪费端口了。可以在一个端口里开启 pprof+charts+prometheus 。

1、入口文件增加代码:

//监控
go func() {
 //提供给负载均衡探活
 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 w.Write([]byte("ok"))

 })

 //prometheus
 http.Handle("/metrics", promhttp.Handler())

 //pprof, go tool pprof -http=:8081 http://$host:$port/debug/pprof/heap
 http.ListenAndServe(":10108", nil)
}()

2、import增加

_ "github.com/mkevac/debugcharts"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
_ "net/http/pprof"

参考
1、Golang pprof详解

https://studygolang.com/articles/14519

2、mkevac/debugcharts: Very simple charts with some debug data for Go programs

https://github.com/mkevac/debugcharts

3、prometheus/client_golang: Prometheus instrumentation library for Go applications

https://github.com/prometheus/client_golang/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a...Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值