Skip to content

Commit 1016c68

Browse files
Allow pprof profiling HTTP API (#132)
* Allow pprof profiling HTTP endpoints
1 parent e8e00d9 commit 1016c68

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

cmd/freno/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func main() {
4444
verbose := flag.Bool("verbose", false, "verbose")
4545
debug := flag.Bool("debug", false, "debug mode (very verbose)")
4646
stack := flag.Bool("stack", false, "add stack trace upon error")
47+
enableProfiling := flag.Bool("enable-profiling", false, "enable pprof profiling http api")
4748

4849
help := flag.Bool("help", false, "show the help")
4950
flag.Parse()
@@ -89,6 +90,9 @@ func main() {
8990
if *httpPort > 0 {
9091
config.Settings().ListenPort = *httpPort
9192
}
93+
if *enableProfiling {
94+
config.Settings().EnableProfiling = *enableProfiling
95+
}
9296

9397
switch {
9498
case *http:

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type ConfigurationSettings struct {
108108
BackendMySQLPassword string
109109
MemcacheServers []string // if given, freno will report to aggregated values to given memcache
110110
MemcachePath string // use as prefix to metric path in memcache key, e.g. if `MemcachePath` is "myprefix" the key would be "myprefix/mysql/maincluster". Default: "freno"
111+
EnableProfiling bool // enable pprof profiling http api
111112
Stores StoresSettings
112113
}
113114

pkg/http/api.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"net/http/pprof"
78
"os"
89
"strconv"
910
"strings"
@@ -370,6 +371,18 @@ func ConfigureRoutes(api API) *httprouter.Router {
370371
register(router, "/debug/vars", metricsHandle)
371372
register(router, "/debug/metrics", metricsHandle)
372373

374+
if config.Settings().EnableProfiling {
375+
router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
376+
router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)
377+
router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
378+
router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
379+
router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
380+
router.Handler(http.MethodGet, "/debug/pprof/goroutine", pprof.Handler("goroutine"))
381+
router.Handler(http.MethodGet, "/debug/pprof/heap", pprof.Handler("heap"))
382+
router.Handler(http.MethodGet, "/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
383+
router.Handler(http.MethodGet, "/debug/pprof/block", pprof.Handler("block"))
384+
}
385+
373386
register(router, "/help", api.Help)
374387

375388
router.GET("/config/memcache", api.MemcacheConfig)

0 commit comments

Comments
 (0)