【问题标题】:Can't use go tool pprof with an existing server无法在现有服务器上使用 go tool pprof
【发布时间】:2015-08-14 03:59:49
【问题描述】:

我有一个现有的 http 服务器,我想对其进行分析。我已将 _ "net/http/pprof" 包含在我的导入中,并且我已经运行了 http 服务器:

router := createRouter()
server := &http.Server {
    Addr:           ":8080",
    Handler:        router,
    ReadTimeout:    15*time.Second,
    WriteTimeout:   15*time.Second,
//  MaxHeaderBytes: 4096,
}

log.Fatal(server.ListenAndServe())

当我尝试访问http://localhost:8080/debug/pprof/ 时,我得到404 page not found

这就是我在本地机器上使用go tool pprof 时得到的结果:

userver@userver:~/Desktop/gotest$ go tool pprof http://192.168.0.27:8080/
Use of uninitialized value $prefix in concatenation (.) or string at /usr/lib/go/pkg/tool/linux_amd64/pprof line 3019.
Read http://192.168.0.27:8080/pprof/symbol
Failed to get the number of symbols from http://192.168.0.27:8080/pprof/symbol

userver@userver:~/Desktop/gotest$ go tool pprof http://localhost:8080/debug/pprof/profile
Read http://localhost:8080/debug/pprof/symbol
Failed to get the number of symbols from http://localhost:8080/debug/pprof/symbol

对于远程客户端也是如此:

MacBookAir:~ apple$ go tool pprof http://192.168.0.27:8080/
Use of uninitialized value $prefix in concatenation (.) or string at /usr/local/Cellar/go/1.3.2/libexec/pkg/tool/darwin_amd64/pprof line 3027.
Read http://192.168.0.27:8080/pprof/symbol
Failed to get the number of symbols from http://192.168.0.27:8080/pprof/symbol

【问题讨论】:

    标签: go gorilla pprof


    【解决方案1】:

    看起来问题出在github.com/gorilla/mux 中使用的*mux.Router 中,我在http.Server 实例中将其用作Handler

    解决方案:只为pprof 启动另外一个服务器:

    server := &http.Server {
        Addr:           ":8080",
        Handler:        router,
        ReadTimeout:    15*time.Second,
        WriteTimeout:   15*time.Second,
    }
    go func() {
        log.Println(http.ListenAndServe(":6060", nil))
    }()
    log.Fatal(server.ListenAndServe())
    

    【讨论】:

      【解决方案2】:

      文档中没有明确提及,但net/http/pprof 仅将其处理程序注册到http.DefaultServeMux

      来自source

      func init() {
              http.Handle("/debug/pprof/", http.HandlerFunc(Index))
              http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
              http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
              http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
              http.Handle("/debug/pprof/trace", http.HandlerFunc(Trace))
      }
      

      如果你没有使用默认的多路复用器,你只需要使用你正在使用的任何多路复用器注册任何/所有你想要的,例如mymux.HandleFunc("…", pprof.Index) 之类的东西。

      或者,您可以使用默认多路复用器 you've shown 侦听单独的端口(如果需要,也可能仅绑定到本地主机)。

      【讨论】:

      • 我有一个问题:一旦转到/debug/pprof/,就会有几个链接指向 404 页。特别是heapallocs 404。正如您在上面显示的,我没有看到net/http/pprof/pprof.goinit() 函数中提供的那些路径的路线。我如何使这些可访问?你会碰巧知道我怎样才能得到那些@Dave C 吗?
      • 如果您使用router.PathPrefix("/debug/").Handler(http.DefaultServeMux),则会添加堆端点。如果您应用 init 处理程序,则不是这样。不知道为什么...
      • 请注意,当使用 Gorilla mux 时,有一个警告 - 请参阅下面的 my answer 了解 404 错误的解决方法。
      【解决方案3】:

      如果您使用的是github.com/gorilla/mux.Router,您可以简单地将任何以/debug/ 为前缀的请求交给http.DefaultServeMux

      import _ "net/http/debug"
      router := mux.NewRouter()
      router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
      

      【讨论】:

        【解决方案4】:

        以下是 pprof 与 Gorilla mux 结合使用的方法。为了避免 HTTP 404 错误,我们需要明确指定 /debug/pprof/{cmd} 的路由,因为 Gorilla 不做前缀路由:

        router := mux.NewRouter()
        router.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
        router.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
        router.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
        router.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
        router.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
        router.Handle("/debug/pprof/{cmd}", http.HandlerFunc(pprof.Index)) // special handling for Gorilla mux
        
        err := http.ListenAndServe("127.0.0.1:9999", router)
        log.Errorf("pprof server listen failed: %v", err) // note: http.ListenAndServe never returns nil
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-15
          • 2015-09-25
          • 2019-05-30
          • 1970-01-01
          • 2013-11-30
          • 2014-04-07
          • 1970-01-01
          • 2023-02-20
          相关资源
          最近更新 更多