-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprof.go
74 lines (58 loc) · 1.35 KB
/
pprof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package pprof
import (
"context"
"net/http"
"net/http/pprof"
"github.com/boxgo/box/pkg/config"
"github.com/boxgo/box/pkg/config/field"
"github.com/boxgo/box/pkg/dummybox"
)
type (
PProf struct {
dummybox.DummyBox
cfg config.SubConfigurator
addr *field.Field
}
Options struct {
cfg config.SubConfigurator
}
OptionFunc func(*Options)
)
var (
Default = New()
)
func WithConfigurator(cfg config.SubConfigurator) OptionFunc {
return func(opts *Options) {
opts.cfg = cfg
}
}
func New(optionFunc ...OptionFunc) *PProf {
opts := &Options{}
for _, fn := range optionFunc {
fn(opts)
}
if opts.cfg == nil {
opts.cfg = config.Default
}
pp := &PProf{
cfg: opts.cfg,
addr: field.New(false, "pprof", "addr", "pprof http server listen addr", ":9091"),
}
pp.cfg.Mount(pp.addr)
return pp
}
func (pp *PProf) Name() string {
return "pprof"
}
func (pp *PProf) Serve(ctx context.Context) error {
serveMux := http.NewServeMux()
serveMux.HandleFunc("/debug/pprof/", pprof.Index)
serveMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
serveMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
serveMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
serveMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
return http.ListenAndServe(pp.cfg.GetString(pp.addr), serveMux)
}
func (pp *PProf) Shutdown(ctx context.Context) error {
return nil
}