Go语言中文网
分享
  1. 首页
  2. 文章

golang在自定义的https服务器中启用pprof接口

sheepbao.github.io · · 4192 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

以下所有观点都是个人愚见,有不同建议或补充的的欢迎emial, aboutme
原文章地址

pprof的简介

pprof是golang标准库里面的其中一个库,它通过其HTTP服务器得到运行时的分析数据,从而给pprof可视化工具提供数据分析来源。它可以用来分析性能消耗,分析内存泄漏,死锁等。
具体使用可以了解官方包pprof,那我如何在http中使用pprof?如何在已有http或者https服务上使用pprof呢? 这些答案在标准库找不到,随在此记录一下。

如何启动pprof

在官方包中已经给出了例子:

package main
import "net/http"
import _ "net/http/pprof" // 初始化pprof
func main() {
 // do something
 ...
 go func() {
	 log.Println(http.ListenAndServe("localhost:6060", nil)) //启动http服务器
 }()
}

启动完后,就可以使用go自动的工具go tool pprof
如:

go tool pprof http://localhost:6060/debug/pprof/heap // 获取堆的相关数据
go tool pprof http://localhost:6060/debug/pprof/profile // 获取30s内cpu的相关数据
go tool pprof http://localhost:6060/debug/pprof/block // 在你程序调用 runtime.SetBlockProfileRate ,查看goroutine阻塞的相关数据
go tool pprof http://localhost:6060/debug/pprof/mutex // 在你程序调用 runtime.SetMutexProfileFraction,查看谁占用mutex

为什么我自定义mux的http服务不能用?

启动自定义mux的http服务器

package main
import (
	"net/http"
	_ "net/http/pprof"
)
func main() {
	// 启动一个自定义mux的http服务器
	mux := http.NewServeMux()
	mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello"))
	})
	http.ListenAndServe(":6060", mux)
}

得到结果: 404 Not Found

go tool pprof http://localhost:6060/debug/pprof/heap
Fetching profile over HTTP from http://localhost:6060/debug/pprof/heap
http://localhost:6060/debug/pprof/heap: server response: 404 Not Found
failed to fetch any source profiles

为什么程序导入了_ "net/http/pprof"还是会404呢?因为导入pprof的时侯只是调用了pprof包的init函数,看看init里面的内容,pprof.init(),可以得知,这里注册的所有路由都是在DefaultServeMux下的, 所以当然我们自定义的mux是没有用的,要使它有用也很简单,我们自己手动注册路由与相应的处理函数。

package main
import (
	"net/http"
	"net/http/pprof"
)
func main() {
	// 启动一个自定义mux的http服务器
	mux := http.NewServeMux()
	mux.HandleFunc("/debug/pprof/", pprof.Index)
	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
	mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello"))
	})
	http.ListenAndServe(":6066", mux)
}

这样,线上的服务出现问题时,就可以用pprof工具看看,是否有异常。

pprof如何在https中使用?

go tool pprof https+insecure://localhost:8001/debug/pprof/heap将原来的http替换成https+insecure即可。

注意:go的版本要>=go1.8,具体查看go1.8的release信息https://tip.golang.org/doc/go1.8#tool_pprof

Please enable JavaScript to view the comments powered by Disqus. comments powered by Disqus

有疑问加站长微信联系(非本文作者)

本文来自:sheepbao.github.io

感谢作者:sheepbao.github.io

查看原文:golang在自定义的https服务器中启用pprof接口

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
4192 次点击 ∙ 2 赞
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏

X
登录和大家一起探讨吧

AltStyle によって変換されたページ (->オリジナル) /