debug.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package debug // import "github.com/docker/docker/api/server/router/debug"
  2. import (
  3. "context"
  4. "expvar"
  5. "net/http"
  6. "net/http/pprof"
  7. "github.com/docker/docker/api/server/httputils"
  8. "github.com/docker/docker/api/server/router"
  9. )
  10. // NewRouter creates a new debug router
  11. // The debug router holds endpoints for debug the daemon, such as those for pprof.
  12. func NewRouter() router.Router {
  13. r := &debugRouter{}
  14. r.initRoutes()
  15. return r
  16. }
  17. type debugRouter struct {
  18. routes []router.Route
  19. }
  20. func (r *debugRouter) initRoutes() {
  21. r.routes = []router.Route{
  22. router.NewGetRoute("/vars", frameworkAdaptHandler(expvar.Handler())),
  23. router.NewGetRoute("/pprof/", frameworkAdaptHandlerFunc(pprof.Index)),
  24. router.NewGetRoute("/pprof/cmdline", frameworkAdaptHandlerFunc(pprof.Cmdline)),
  25. router.NewGetRoute("/pprof/profile", frameworkAdaptHandlerFunc(pprof.Profile)),
  26. router.NewGetRoute("/pprof/symbol", frameworkAdaptHandlerFunc(pprof.Symbol)),
  27. router.NewGetRoute("/pprof/trace", frameworkAdaptHandlerFunc(pprof.Trace)),
  28. router.NewGetRoute("/pprof/{name}", handlePprof),
  29. }
  30. }
  31. func (r *debugRouter) Routes() []router.Route {
  32. return r.routes
  33. }
  34. func frameworkAdaptHandler(handler http.Handler) httputils.APIFunc {
  35. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  36. handler.ServeHTTP(w, r)
  37. return nil
  38. }
  39. }
  40. func frameworkAdaptHandlerFunc(handler http.HandlerFunc) httputils.APIFunc {
  41. return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  42. handler(w, r)
  43. return nil
  44. }
  45. }