From 640a0ef956e2b27f64122075d89a9dfbcf8ce6c0 Mon Sep 17 00:00:00 2001 From: Craig Peterson Date: Fri, 18 Mar 2016 10:39:29 -0600 Subject: [PATCH] Adding pprof middleware for debugging purposes --- caddy/directives.go | 1 + caddy/setup/pprof.go | 26 ++++++++++++++++++++++++++ caddy/setup/pprof_test.go | 28 ++++++++++++++++++++++++++++ middleware/pprof/pprof.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 caddy/setup/pprof.go create mode 100644 caddy/setup/pprof_test.go create mode 100644 middleware/pprof/pprof.go diff --git a/caddy/directives.go b/caddy/directives.go index fd5245085..f9da35d2f 100644 --- a/caddy/directives.go +++ b/caddy/directives.go @@ -61,6 +61,7 @@ var directiveOrder = []directive{ {"mime", setup.Mime}, {"basicauth", setup.BasicAuth}, {"internal", setup.Internal}, + {"pprof", setup.PProf}, {"proxy", setup.Proxy}, {"fastcgi", setup.FastCGI}, {"websocket", setup.WebSocket}, diff --git a/caddy/setup/pprof.go b/caddy/setup/pprof.go new file mode 100644 index 000000000..269439455 --- /dev/null +++ b/caddy/setup/pprof.go @@ -0,0 +1,26 @@ +package setup + +import ( + "github.com/mholt/caddy/middleware" + "github.com/mholt/caddy/middleware/pprof" +) + +//PProf returns a new instance of a pprof handler. It accepts no arguments or options. +func PProf(c *Controller) (middleware.Middleware, error) { + found := false + for c.Next() { + if found { + return nil, c.Err("pprof can only be specified once") + } + if len(c.RemainingArgs()) != 0 { + return nil, c.ArgErr() + } + if c.NextBlock() { + return nil, c.ArgErr() + } + found = true + } + return func(next middleware.Handler) middleware.Handler { + return pprof.New(next) + }, nil +} diff --git a/caddy/setup/pprof_test.go b/caddy/setup/pprof_test.go new file mode 100644 index 000000000..ac9375af7 --- /dev/null +++ b/caddy/setup/pprof_test.go @@ -0,0 +1,28 @@ +package setup + +import "testing" + +func TestPProf(t *testing.T) { + tests := []struct { + input string + shouldErr bool + }{ + {`pprof`, false}, + {`pprof {}`, true}, + {`pprof /foo`, true}, + {`pprof { + a b + }`, true}, + {`pprof + pprof`, true}, + } + for i, test := range tests { + c := NewTestController(test.input) + _, err := PProf(c) + if test.shouldErr && err == nil { + t.Errorf("Test %v: Expected error but found nil", i) + } else if !test.shouldErr && err != nil { + t.Errorf("Test %v: Expected no error but found error: %v", i, err) + } + } +} diff --git a/middleware/pprof/pprof.go b/middleware/pprof/pprof.go new file mode 100644 index 000000000..d78d34bab --- /dev/null +++ b/middleware/pprof/pprof.go @@ -0,0 +1,34 @@ +package pprof + +import ( + "net/http" + pp "net/http/pprof" + + "github.com/mholt/caddy/middleware" +) + +//Handler is a simple struct whose ServeHTTP will delegate relevant pprof endpoints to net/http/pprof +type handler struct { + mux *http.ServeMux +} + +//New creates a new pprof middleware +func New(next middleware.Handler) middleware.Handler { + //pretty much copying what pprof does on init: https://golang.org/src/net/http/pprof/pprof.go#L67 + mux := http.NewServeMux() + mux.HandleFunc("/debug/pprof/", pp.Index) + mux.HandleFunc("/debug/pprof/cmdline", pp.Cmdline) + mux.HandleFunc("/debug/pprof/profile", pp.Profile) + mux.HandleFunc("/debug/pprof/symbol", pp.Symbol) + mux.HandleFunc("/debug/pprof/trace", pp.Trace) + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + next.ServeHTTP(w, r) + }) + return &handler{mux} +} + +func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { + rec := middleware.NewResponseRecorder(w) + h.mux.ServeHTTP(rec, r) + return rec.Status(), nil +}