-
Notifications
You must be signed in to change notification settings - Fork 26
/
debug.go
87 lines (76 loc) · 2.26 KB
/
debug.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
75
76
77
78
79
80
81
82
83
84
85
86
87
package debug
import (
"context"
"net/http"
"net/http/pprof"
"time"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
)
type Config struct {
Enabled bool `json:"enabled"`
ListenAddress string `json:"listen_address"`
ShutdownTimeout int `json:"shutdown_timeout"`
}
type Server struct {
server *http.Server
logger *zerolog.Logger
cfg *Config
errGroup *errgroup.Group
}
func NewServer(cfg *Config, log *zerolog.Logger, errGroup *errgroup.Group) *Server {
if cfg.Enabled {
pprofMux := http.NewServeMux()
pprofMux.Handle("/debug/allocs", pprof.Handler("allocs"))
pprofMux.Handle("/debug/block", pprof.Handler("block"))
pprofMux.Handle("/debug/goroutine", pprof.Handler("goroutine"))
pprofMux.Handle("/debug/heap", pprof.Handler("heap"))
pprofMux.Handle("/debug/mutex", pprof.Handler("mutex"))
pprofMux.Handle("/debug/threadcreate", pprof.Handler("threadcreate"))
pprofMux.Handle("/debug/profile", http.HandlerFunc(pprof.Profile))
pprofMux.Handle("/debug/symbol", http.HandlerFunc(pprof.Symbol))
pprofMux.Handle("/debug/trace", http.HandlerFunc(pprof.Trace))
srv := &http.Server{
Addr: cfg.ListenAddress,
Handler: pprofMux,
ReadTimeout: 5 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 30 * time.Second,
}
debugLogger := log.With().Str("component", "debug").Logger()
return &Server{
server: srv,
logger: &debugLogger,
cfg: cfg,
errGroup: errGroup,
}
}
return nil
}
func (srv *Server) Start() {
if srv != nil {
srv.errGroup.Go(func() error {
err := srv.server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
srv.logger.Error().Err(err).Str("address", srv.cfg.ListenAddress).Msg("Profiling endpoint failed to listen")
}
return nil
})
}
}
func (srv *Server) Stop() {
if srv != nil {
var shutdown context.CancelFunc
ctx := context.Background()
if srv.cfg.ShutdownTimeout > 0 {
shutdownTimeout := time.Duration(srv.cfg.ShutdownTimeout) * time.Second
ctx, shutdown = context.WithTimeout(ctx, shutdownTimeout)
defer shutdown()
}
err := srv.server.Shutdown(ctx)
if err != nil {
srv.logger.Info().Err(err).Msg("error shutting down debug server")
}
}
}