-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
框架没有接入pprof 进行性能剖析吗? #806
Comments
木有,hc也需要手动 |
同求! |
Same request! |
go-zero 如何接入 pprof 工具,我这简单总结了下: |
How go-zero accesses the pprof tool, I briefly summarize it: |
有内置的,看 |
There are built-in ones, see |
好的,我看看,谢谢 |
ok, i'll take a look, thanks |
|
|
这个就不需要了,删掉 |
This is not needed, delete it |
//我根据博主文档整理一份完整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 |
我是这样实现的,创建 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,
},
}...,
),
)
} |
框架没有接入pprof 进行性能剖析,需要自己进行接入
The text was updated successfully, but these errors were encountered: