6
框架没有接入pprof 进行性能剖析,需要自己进行接入
框架没有接入pprof 进行性能剖析,需要自己进行接入
go-zero 如何接入 pprof 工具,我这简单总结了下: https://laomiao.site/go-zero-%E5%A6%82%E4%BD%95%E6%8E%A5%E5%85%A5-pprof
有内置的,看 core/prof
包
好的,我看看,谢谢
这个就不需要了,删掉
//我根据博主文档整理一份完整demo,goctl1.3.5生成的API
package main
import (
"flag"
"fmt"
"github.com/zeromicro/go-zero/core/service"
"log"
"net/http"
"zero-api/service/gateway/web/internal/config"
"zero-api/service/gateway/web/internal/handler"
"zero-api/service/gateway/web/internal/svc"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
var configFile = flag.String("f", "etc/gateway-api.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
//新增代码开始***********//
svcGroup := service.NewServiceGroup()
defer svcGroup.Stop()
//新增代码结束***********//
ctx := svc.NewServiceContext(c)
server := rest.MustNewServer(c.RestConf)
//新增代码开始***********//
svcGroup.Add(server)
//新增代码结束***********//
//注释原有代码
//defer server.Stop()
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
//注释原有代码
//server.Start()
//新增代码开始***********//
svcGroup.Add(pprofServer{})
svcGroup.Start()
//新增代码结束***********//
}
//新增代码开始***********//
type pprofServer struct{}
func (pprofServer) Start() {
addr := "127.0.0.1:39599"
fmt.Printf("Start pprof server, listen addr %s\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal(err)
}
}
func (pprofServer) Stop() {
fmt.Printf("Stop pprof server\n")
}
//新增代码结束***********//
//goctl1.3.5官方模版生成的代码
func main02() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
server := rest.MustNewServer(c.RestConf)
defer server.Stop()
handler.RegisterHandlers(server, ctx)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}
你好,使用kill -usr2 后 打不开trace.pprof文件,其他文件可以打开
go tool pprof core-1-trace-1201111253.pprof core-1-trace-1201111253.pprof: parsing profile: unrecognized profile format failed to fetch any source profiles
我是这样实现的,创建/ProjectName/api/internal/handler/monitor.go
文件,然后在里面增加pprof相关的Handler,最后在main里面handler.RegisterMonitorHandler(server, ctx)
导入就好啦。
package handler
import (
"github.com/zeromicro/go-zero/rest"
"net/http"
"net/http/pprof"
"ticket_saas/api/internal/svc"
)
func RegisterMonitorHandler(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/debug/pprof",
Handler: pprof.Index,
},
{
Method: http.MethodGet,
Path: "/debug/allocs",
Handler: pprof.Handler("allocs").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/block",
Handler: pprof.Handler("block").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/cmdline",
Handler: pprof.Handler("cmdline").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/goroutine",
Handler: pprof.Handler("goroutine").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/heap",
Handler: pprof.Handler("heap").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/mutex",
Handler: pprof.Handler("mutex").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/profile",
Handler: pprof.Handler("profile").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/threadcreate",
Handler: pprof.Handler("threadcreate").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/trace",
Handler: pprof.Handler("trace").ServeHTTP,
},
{
Method: http.MethodGet,
Path: "/debug/symbol",
Handler: pprof.Symbol,
},
{
Method: http.MethodGet,
Path: "/debug/vars",
Handler: http.DefaultServeMux.ServeHTTP,
},
}...,
),
)
}