刘孝喜 發表於 2019-11-9 23:14:00

Go服务监控

<p>使用Golang可以开发出高性能的HTTP、GRPC服务。一般项目运行后,我们也需要监控服务的性能或者进行调试。除了打日志,还有没有其他可视化的方案呢?答案是有的。</p>
<p>本文将会介绍几种常用的监控方案。</p>
<h2 id="pprof">pprof</h2>
<p>这个是go语言自带的。启用很简单:</p>
<pre><code class="language-go">_ "net/http/pprof"
</code></pre>
<p>仅需显式的在 main 包的 import 里增加上面一行即可。完整使用示例:</p>
<pre><code class="language-go">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)
}
</code></pre>
<p>运行之后,在浏览器打开 <code>http://127.0.0.1:10108/debug/pprof/</code>就能看到监控的一些信息了:</p>
<p><img src="https://img2018.cnblogs.com/blog/663847/201911/663847-20191109231138364-633001314.png" alt="" loading="lazy"></p>
<blockquote>
<p>注:生产环境一般不会按上面那么写,都是开个协程:</p>
</blockquote>
<pre><code class="language-go">go http.ListenAndServe(":10108", nil)
</code></pre>
<p>如何启动 PProf 可视化界面?</p>
<blockquote>
<p>需要<code>graphviz</code>支持,可以到 http://www.graphviz.org/download/ 下载,并把bin加入到环境变量。Mac可以使用brew安装。</p>
</blockquote>
<p>下面以<code>heap</code>为例:</p>
<p>方法一:</p>
<pre><code>go tool pprof -http=:8081 http://localhost:10108/debug/pprof/heap
</code></pre>
<p>方法二:</p>
<pre><code>go tool pprof http://localhost:10108/debug/pprof/heap
</code></pre>
<p>然后在交互式命令行输入<code>web</code>即可跳转到默认浏览器:</p>
<p><img src="https://img2018.cnblogs.com/blog/663847/201911/663847-20191109231211647-1240640016.png" alt="" loading="lazy"></p>
<p>查看协程信息:</p>
<pre><code class="language-bash">go tool pprof -http=:8081 http://localhost:10108/debug/pprof/goroutine
</code></pre>
<h2 id="debugcharts">debugcharts</h2>
<p>一个可以实时查看golang程序内存、CPU、GC、协程等变化情况的可视化工具。</p>
<blockquote>
<p>debugcharts 监控占用内存大,生产环境不建议开启。</p>
</blockquote>
<p>跟pprof一样, import引入, 然后开端口监听就行了:</p>
<pre><code class="language-go">_ "github.com/mkevac/debugcharts"
</code></pre>
<pre><code>//省略其它代码...
http.ListenAndServe(":10108", nil)
</code></pre>
<p>运行后,浏览器打开 http://localhost:10108/debug/charts/ 就能看到了:<br>
<img src="https://img2018.cnblogs.com/blog/663847/201911/663847-20191109231239780-383614762.png" alt="" loading="lazy"></p>
<h2 id="prometheus">prometheus</h2>
<p>prometheus是grafana的插件,支持go监控的可视化。</p>
<p>首先需要代码里引入包:</p>
<pre><code class="language-go">"github.com/prometheus/client_golang/prometheus/promhttp"
</code></pre>
<p>然后增加路由:</p>
<pre><code class="language-go">//prometheus
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":10108", nil)
</code></pre>
<p>配置grafana后,效果图:</p>
<p><img src="https://img2018.cnblogs.com/blog/663847/201911/663847-20191109231323653-1640901497.png" alt="" loading="lazy"></p>
<h2 id="一个端口开启-pprofchartsprometheus">一个端口开启 pprof+charts+prometheus</h2>
<p>如果每一个监控都开一个端口就有点浪费端口了。可以在一个端口里开启 pprof+charts+prometheus 。</p>
<p>1、入口文件增加代码:</p>
<pre><code>//监控
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)
}()
</code></pre>
<p>2、import增加</p>
<pre><code class="language-go">_ "github.com/mkevac/debugcharts"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
_ "net/http/pprof"
</code></pre>
<h2 id="参考">参考</h2>
<p>1、Golang pprof详解<br>
https://studygolang.com/articles/14519<br>
2、mkevac/debugcharts: Very simple charts with some debug data for Go programs<br>
https://github.com/mkevac/debugcharts<br>
3、prometheus/client_golang: Prometheus instrumentation library for Go applications<br>
https://github.com/prometheus/client_golang/</p>


</div>
<div id="MySignature" role="contentinfo">
    (本文完)
<br><br>


欢迎关注公众号"飞鸿影记(fhyblog)",探寻物件背后的逻辑,记录生活真实的影子。
<div style="text-align: center"><span style="color: #808000"><img src="https://images2015.cnblogs.com/blog/663847/201703/663847-20170331215930477-1406562582.jpg" alt="" width="250" height="250"></span></div>


<div>
<p><strong>作者:飞鸿影</strong></p>
<p><strong>出处:http://52fhy.cnblogs.com/</strong></p>
</div>

<hr>

<p style="border: silver 1px dashed; padding: 8px 5px; font-weight: bold">版权申明:没有标明转载或特殊申明均为作者原创。本文采用以下协议进行授权,自由转载 - 非商用 - 非衍生 - 保持署名 | Creative Commons BY-NC-ND 3.0,转载请注明作者及出处。</p>

<hr>


<div style="text-align: center;display:none"><span style="color: #808000"><img src="https://images2018.cnblogs.com/blog/663847/201805/663847-20180515232241445-706573186.png" alt="" width="250" height="250"></span></div><br><br>
来源:https://www.cnblogs.com/52fhy/p/11828448.html
頁: [1]
查看完整版本: Go服务监控