Golang enabling the Pprof interface in a custom HTTPS server

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

All of the following views are personal humble opinion, with different suggestions or additions to the Welcome emial, Aboutme
Original article Address

Pprof's introduction

Pprof is one of the libraries in the Golang standard library that provides data analysis sources for Pprof visualizers by using its HTTP server to obtain runtime profiling data. It can be used to analyze performance consumption, analyze memory leaks, deadlock, and so on.
Specific use can understand the official package pprof, then how do I use Pprof in HTTP? How do I use pprof on an existing HTTP or HTTPS service? These answers are not found in the standard library and are documented here.

How to start Pprof

Examples have been given in the official package:

package mainimport  "net/http"import _ "net/http/pprof"  //  初始化pproffunc main() {    // do something    ...    go func() {    log.Println(http.ListenAndServe("localhost:6060", nil)) //启动http服务器    }()}

When you're done, you can use the Go Auto toolgo tool pprof
Such as:

go tool pprof http://localhost:6060/debug/pprof/heap    // 获取堆的相关数据go tool pprof http://localhost:6060/debug/pprof/profile // 获取30s内cpu的相关数据go tool pprof http://localhost:6060/debug/pprof/block   // 在你程序调用 runtime.SetBlockProfileRate ,查看goroutine阻塞的相关数据go tool pprof http://localhost:6060/debug/pprof/mutex   // 在你程序调用 runtime.SetMutexProfileFraction,查看谁占用mutex

Why is my custom MUX HTTP service not available?

Start the HTTP server for the custom MUX

package mainimport ("net/http"_ "net/http/pprof")func main() {// 启动一个自定义mux的http服务器mux := http.NewServeMux()mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("hello"))})http.ListenAndServe(":6060", mux)}

Get results:404 Not Found

go tool pprof http://localhost:6060/debug/pprof/heapFetching profile over HTTP from http://localhost:6060/debug/pprof/heaphttp://localhost:6060/debug/pprof/heap: server response: 404 Not Foundfailed to fetch any source profiles

Why is the program imported _ "net/http/pprof" or will it be 404? Because the import pprof just call the Pprof package init function, look at the contents of the Init, Pprof.init (), you can know that all the routes registered here are DefaultServeMux under, so of course, our custom mux is useless, to make it useful and simple , we manually register the route with the corresponding handler function.

package mainimport ("net/http""net/http/pprof")func main() {// 启动一个自定义mux的http服务器mux := http.NewServeMux()mux.HandleFunc("/debug/pprof/", pprof.Index)mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)mux.HandleFunc("/debug/pprof/profile", pprof.Profile)mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)mux.HandleFunc("/debug/pprof/trace", pprof.Trace)mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("hello"))})http.ListenAndServe(":6066", mux)}

This way, when there is a problem with the service on the line, you can use the Pprof tool to see if there is an exception.

How is pprof used in HTTPS?

go tool pprof https+insecure://localhost:8001/debug/pprof/heapReplace the original with a http https+insecure can.

Note: Go version to >=go1.8, the specific view go1.8 release information https://tip.golang.org/doc/go1.8#tool_pprof

    • ←previous Post
Please enable JavaScript to view the comments powered by Disqus. Comments Powered by Disqus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.