Skip to content

Commit

Permalink
Refactor pprof endpoints in line with go tool expectations (#313)
Browse files Browse the repository at this point in the history
* Refactor pprof endpoints in line with go tool expectations:
https://golang.org/pkg/net/http/pprof/

* remove unneed profiler struct (use the underlying pprof package)

* added /debug/pprof/trace and support for multiple request methods on the
same route

* update HTTP readme + CHANGELOG
  • Loading branch information
adampetrovic authored Jun 25, 2020
1 parent 5612072 commit ef54071
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
22.0.0
------
- Refactor pprof endpoints to sit under `/debug/pprof`

21.0.0
------
- Breaks out expiry interval in to per metric type expiry intervals. See [README.md](README.md) for details.
Expand Down
8 changes: 4 additions & 4 deletions HTTP.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## HTTP endpoints

### `prof` endpoints
- `/trace`, runs a trace profile for 30 seconds
- `/pprof`, runs a pprof profile for 30 seconds
- `/memprof`, runs a `heap` memory profile for 30 seconds
### `pprof` endpoints
- `/debug/pprof/trace`, runs a trace profile for 30 seconds
- `/debug/pprof/profile`, runs a pprof profile for 30 seconds
- `/debug/pprof/heap`, runs a `heap` memory profile for 30 seconds

Only one prof will be allowed to run at any point, and requesting multiple will block until the previous has completed.

Expand Down
41 changes: 0 additions & 41 deletions pkg/web/http_profilers.go

This file was deleted.

24 changes: 13 additions & 11 deletions pkg/web/httpservers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"expvar"
"fmt"
"net/http"
"net/http/pprof"
"strings"
"time"

Expand All @@ -27,7 +28,7 @@ type httpServer struct {
type route struct {
path string
handler http.HandlerFunc
method string
methods []string
name string
}

Expand Down Expand Up @@ -88,33 +89,34 @@ func NewHttpServer(
}

if enableProf {
profiler := &traceProfiler{}
routes = append(routes,
route{path: "/memprof", handler: profiler.MemProf, method: "POST", name: "profmem_post"},
route{path: "/pprof", handler: profiler.PProf, method: "POST", name: "profpprof_post"},
route{path: "/trace", handler: profiler.Trace, method: "POST", name: "proftrace_post"},
route{path: "/debug/pprof/", handler: pprof.Index, methods: []string{"GET"}, name: "pprof_index"},
route{path: "/debug/pprof/cmdline", handler: pprof.Cmdline, methods: []string{"GET"}, name: "pprof_cmdline"},
route{path: "/debug/pprof/profile", handler: pprof.Profile, methods: []string{"GET"}, name: "pprof_profile"},
route{path: "/debug/pprof/symbol", handler: pprof.Symbol, methods: []string{"GET"}, name: "pprof_symbol"},
route{path: "/debug/pprof/trace", handler: pprof.Trace, methods: []string{"GET", "POST"}, name: "pprof_trace"},
)
}

if enableExpVar {
routes = append(routes,
route{path: "/expvar", handler: expvar.Handler().ServeHTTP, method: "GET", name: "expvar_get"},
route{path: "/expvar", handler: expvar.Handler().ServeHTTP, methods: []string{"GET"}, name: "expvar_get"},
)
}

if enableIngestion {
server.rawMetricsV2 = newRawHttpHandlerV2(logger, serverName, handler)
routes = append(routes,
route{path: "/v2/raw", handler: server.rawMetricsV2.MetricHandler, method: "POST", name: "metricsv2_post"},
route{path: "/v2/event", handler: server.rawMetricsV2.EventHandler, method: "POST", name: "eventsv2_post"},
route{path: "/v2/raw", handler: server.rawMetricsV2.MetricHandler, methods: []string{"POST"}, name: "metricsv2_post"},
route{path: "/v2/event", handler: server.rawMetricsV2.EventHandler, methods: []string{"POST"}, name: "eventsv2_post"},
)
}

if enableHealthcheck {
hc := &healthChecker{logger}
routes = append(routes,
route{path: "/healthcheck", handler: hc.healthCheck, method: "GET", name: "healthcheck_get"},
route{path: "/deepcheck", handler: hc.deepCheck, method: "GET", name: "deepcheck_get"},
route{path: "/healthcheck", handler: hc.healthCheck, methods: []string{"GET"}, name: "healthcheck_get"},
route{path: "/deepcheck", handler: hc.deepCheck, methods: []string{"GET"}, name: "deepcheck_get"},
)
}

Expand Down Expand Up @@ -150,7 +152,7 @@ func createRoutes(routes []route) (*mux.Router, error) {
router := mux.NewRouter()

for _, route := range routes {
r := router.HandleFunc(route.path, route.handler).Methods(route.method).Name(route.name)
r := router.HandleFunc(route.path, route.handler).Methods(route.methods...).Name(route.name)
if err := r.GetError(); err != nil {
return nil, fmt.Errorf("error creating route %s: %v", route.name, err)
}
Expand Down

0 comments on commit ef54071

Please sign in to comment.