Skip to content

proposal: net/http/pprof: function to register all profiles to a mux #71213

Open
@DerekTBrown

Description

@DerekTBrown

User Story

  • I have an application that has mux instances.
  • I want to enable net/http/pprof on a specific mux instance.
  • net/http/pprof does not provide an ergonomic interface for attaching the handlers to a specific mux.

Current Options / Alternatives Considered

The net/http/pprof package init function is the recommended path for enabling the pprof handler.1 This method uses the DefaultServeMux:

func init() {
prefix := ""
if godebug.New("httpmuxgo121").Value() != "1" {
prefix = "GET "
}
http.HandleFunc(prefix+"/debug/pprof/", Index)
http.HandleFunc(prefix+"/debug/pprof/cmdline", Cmdline)
http.HandleFunc(prefix+"/debug/pprof/profile", Profile)
http.HandleFunc(prefix+"/debug/pprof/symbol", Symbol)
http.HandleFunc(prefix+"/debug/pprof/trace", Trace)
}

If the user wants to mount the pprof handlers using a non-default mux, they must do this by manually enumerating all of the available profilers2. For example:

	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)

Proposal

This experience could be made better for users by moving the logic in the init function into a separate method (with the mux as an argument), then invoking this within the default package init function.

Footnotes

  1. https://pkg.go.dev/net/http/pprof#:~:text=To%20use%20pprof%2C%20link%20this%20package%20into%20your%20program%3A

  2. https://pkg.go.dev/net/http/pprof#:~:text=If%20you%20are%20not%20using%20DefaultServeMux%2C%20you%20will%20have%20to%20register%20handlers%20with%20the%20mux%20you%20are%20using.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions