-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.go
70 lines (63 loc) · 1.76 KB
/
server.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
package httpdebug
import (
"net/http"
"net/http/pprof"
"time"
"github.com/signalfx/golib/v2/explorable"
"github.com/signalfx/golib/v2/expvar2"
"github.com/signalfx/golib/v2/log"
"github.com/signalfx/golib/v2/pointer"
)
// Server exposes private debugging information
type Server struct {
http.Server
Exp2 *expvar2.Handler
Mux *http.ServeMux
}
// Config controls optional parameters for the debug server
type Config struct {
Logger log.Logger
ReadTimeout *time.Duration
WriteTimeout *time.Duration
ExplorableObj interface{}
}
// DefaultConfig is used by default for unset config parameters
var DefaultConfig = &Config{
Logger: log.DefaultLogger.CreateChild(),
ReadTimeout: pointer.Duration(time.Duration(0)),
WriteTimeout: pointer.Duration(time.Duration(0)),
}
var (
// LogKeyHTTPClass is appended as a key to subloggers of the debug server
LogKeyHTTPClass = log.Key("http_class")
)
// New creates a new debug server
func New(conf *Config) *Server {
conf = pointer.FillDefaultFrom(conf, DefaultConfig).(*Config)
m := http.NewServeMux()
s := &Server{
Server: http.Server{
// TODO: Also put logger in http.Server
Handler: m,
ReadTimeout: *conf.ReadTimeout,
WriteTimeout: *conf.WriteTimeout,
},
Exp2: expvar2.New(),
Mux: m,
}
m.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
m.HandleFunc("/debug/pprof/profile", pprof.Profile)
m.HandleFunc("/debug/pprof/", pprof.Index)
m.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
setupTrace(m)
if conf.ExplorableObj != nil {
e := &explorable.Handler{
Val: conf.ExplorableObj,
BasePath: "/debug/explorer/",
Logger: log.NewContext(conf.Logger).With(LogKeyHTTPClass, "explorable"),
}
m.Handle("/debug/explorer/", e)
}
m.Handle("/debug/vars", s.Exp2)
return s
}