Skip to content

Commit a5d1e4b

Browse files
committed
feat(profiling): add pprof handler registration function for mux servers
1 parent 6bf64d0 commit a5d1e4b

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

pkg/lib/profile/profile.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package profile
2+
3+
import (
4+
"net/http"
5+
"net/http/pprof"
6+
)
7+
8+
type profileConfig struct {
9+
pprof bool
10+
cmdline bool
11+
profile bool
12+
symbol bool
13+
trace bool
14+
}
15+
16+
// Option applies a configuration option to the given config.
17+
type Option func(p *profileConfig)
18+
19+
func (p *profileConfig) apply(options []Option) {
20+
if len(options) == 0 {
21+
// If no options are given, default to all
22+
p.pprof = true
23+
p.cmdline = true
24+
p.profile = true
25+
p.symbol = true
26+
p.trace = true
27+
28+
return
29+
}
30+
31+
for _, o := range options {
32+
o(p)
33+
}
34+
}
35+
36+
func defaultProfileConfig() *profileConfig {
37+
// Initialize config
38+
return &profileConfig{}
39+
}
40+
41+
// RegisterHandlers registers profile Handlers with the given ServeMux.
42+
//
43+
// The Handlers registered are determined by the given options.
44+
// If no options are given, all available handlers are registered by default.
45+
func RegisterHandlers(mux *http.ServeMux, options ...Option) {
46+
config := defaultProfileConfig()
47+
config.apply(options)
48+
49+
if config.pprof {
50+
mux.HandleFunc("/debug/pprof/", pprof.Index)
51+
}
52+
if config.cmdline {
53+
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
54+
}
55+
if config.profile {
56+
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
57+
}
58+
if config.symbol {
59+
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
60+
}
61+
if config.trace {
62+
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
63+
}
64+
}

0 commit comments

Comments
 (0)