pprof是golang的提供的性能分析工具
直接通过引用github.com/gin-contrib/pprof
即可调用
项目代码节选展示:
//性能调优监视 TODO Gin自主隐藏,待优化
authStr := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(config.ProducerUsername+":"+config.ProducerPassword)))
pprofGroup := r.Group("/producer", func(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
if auth != authStr {
c.Header("www-Authenticate", "Basic")
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.Next()
})
pprof.RouteRegister(pprofGroup, "flowerxxxxx_pprof")
Gin框架所携带的pprof直接调用即可,但是在生产环境中会引发暴露的问题,导致/debug/pprof接口暴露到公网。
我们将接口注册到新的路由组里,并且将(账号+冒号+密码)Base64后并添加Basic标识,即可触发访问也要输入账号和密码的保护机制。