首页 > 其他分享 >解决方案(14) golang pprof接入业务同端口

解决方案(14) golang pprof接入业务同端口

时间:2022-11-22 23:38:10浏览次数:52  
标签:return 14 pprof golang adapt func debug gin


前言

声明: 本文不对pprof基础做讲解,详情可以查阅 https://studygolang.com/pkgdoc 找到net/http/pprof

golang pprof官方介绍里,是采用了默认6060端口,单独开辟了应用进程的pprof服务。它在生产中有以下问题:

  • 需要额外维护不同应用组的pprof端口。(实际上,它可以和业务绑定在同一个路由端口里,减少端口冲突维护)。
  • 在二次开发上,可能想接入鉴权,限频的拦截,官方提供的方式做不到。
  • 官方的路由是f(w http.Writer, r *http.Request),而项目里,可能用到了gin,难以接入。

解决

实现

从gin项目来演示如何让pprof和业务绑定同端口。

本代码片段,为可复制片段,按照项目鉴权等自行适配

func profRouter(r gin.IRoutes) {

// 主页面
r.GET("/debug/pprof/", adapt(pprof.Index))

r.GET("/debug/pprof/profile", profile())

r.GET("/debug/pprof/heap", heap())

r.GET("/debug/pprof/block", block())

r.GET("/debug/pprof/goroutine", goroutine())

r.GET("/debug/pprof/allocs", allocs())
r.GET("/debug/pprof/cmdline", cmdline())
r.GET("/debug/pprof/threadcreate", threadcreate())
r.GET("/debug/pprof/mutex", mutex())
r.GET("/debug/pprof/trace", trace())


}

func adapt(f func(w http.ResponseWriter, r *http.Request)) func(c *gin.Context) {
return func(c *gin.Context) {
f(c.Writer, c.Request)
}
}

func profile() gin.HandlerFunc {
return adapt(pprof.Profile)
//return adapt(pprof.Handler("profile").ServeHTTP)
}

func heap() gin.HandlerFunc {
return adapt(pprof.Handler("heap").ServeHTTP)
}

func block() gin.HandlerFunc {
return adapt(pprof.Handler("block").ServeHTTP)
}

func goroutine() gin.HandlerFunc {
return adapt(pprof.Handler("goroutine").ServeHTTP)
}

func allocs() gin.HandlerFunc {
return adapt(pprof.Handler("allocs").ServeHTTP)
}

func cmdline() gin.HandlerFunc {
return adapt(pprof.Cmdline)
}

func threadcreate() gin.HandlerFunc {
return adapt(pprof.Handler("threadcreate").ServeHTTP)
}

func mutex() gin.HandlerFunc {
return adapt(pprof.Handler("mutex").ServeHTTP)
}

func trace() gin.HandlerFunc {
return adapt(pprof.Trace)
}
func main(){
r := gin.Default()
// pprof路由
profRouter(r)

// 业务路由
xxx(r)
r.Run(":8080")
}

代码中,使用了进行转换,使官方的路由可以适配进来

func adapt(f func(w http.ResponseWriter, r *http.Request)) func(c *gin.Context) {
return func(c *gin.Context) {
f(c.Writer, c.Request)
}
}

效果和官方保持一致
​​​ http://localhost:8080/debug/pprof/​解决方案(14) golang pprof接入业务同端口_代码片段

另外,如果pprof的视图很乱,想要了解两个服务增量上的差异,可以使用
go tool pprof -base file1 file2 来比较增量差异。

其中file1和file2是通过类似以下命令拉取的分析文件

curl http://localhost:9112/local/api/debug/pprof/goroutine > file2

仓库

该实现,已经开源到https://github.com/fwhezfwhez/ginprof.git

import "github.com/fwhezfwhez/ginprof"
func main() {
router := gin.New()
ginprof.ProfRouter(router)
...
}


标签:return,14,pprof,golang,adapt,func,debug,gin
From: https://blog.51cto.com/u_11553781/5878730

相关文章

  • golang 微信支付计算签名
    原文:1、签名算法(签名校验工具)签名生成的通用步骤如下:第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用URL键......
  • golang算法—— 使用两个栈实现一个队列
    前言阅读本文,假定已经了解了基本数据结构概念。队列:先入先出。栈:先进后出。分析使用两个栈串联,可以实现先进先出。但是,得注意以下两点:队列在入列时,stack2必须为空,stac......
  • golang grpc使用示例
    疑问写前面grpc有内部对心跳的处理吗,还是说,双工需要自己作心跳管理,有懂的留言一下。SEO优化grpc如何双工通信?grpc如何从服务端推送消息给客户端?gprc环境如何搭建?grpc......
  • BZOJ3670-[Noi2014]动物园
    3670:[Noi2014]动物园TimeLimit: 10Sec  MemoryLimit: 512MBSubmit: 3465  Solved: 1882[​​Submit​​][​​Status​​][​​Discuss​​]D......
  • go /golang 下生成protobuf *.pb.go文件 记录
      如果出现这个状况解决办法记录一下:在指定目录  gitclonehttps://github.com/golang/protobuf 下载源码   进入到以下目录  分别执行 以下图片......
  • hudi with flink1.14验证
    hudiwithflinkdatastreambyjava版本flink1.14hudi0.12.1pom依赖<dependencies><dependency><groupId>org.apache.hudi</groupId>......
  • [译]Golang中JSON和结构体的组合使用
     原文地址:http://attilaolah.eu/2014/09/10/json-and-struct-composition-in-go/ 假设你正在把一个JSON对象解码为Go的结构体。该JSON来自不受你控制的服......
  • k8s1.14.1 集群更新证书过期时间
    适用场景部分组件证书检查只有1年时间(即不止kubelet证书),但当前未过期部分老包可能会出现一些master组件证书,或者kubelet证书只有1年的时间,如果检查发现若干证书都是1年,需......
  • leetcode814. 二叉树剪枝。如果想到使用递归还是很简单的
    814.二叉树剪枝有一点疑问,为什么不能先     if(!root->left&&!root->right&&root->val==0)returnnullptr;   ?classSolution{public:TreeNode......
  • golang
    golang  TRANSLATEwithxEnglishArabicHebrewPolishBulgarianHindiPortugueseCatalanHmongDawRomanianChineseSimplifiedHungarian......