...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package chaosdaemon
15
16 import (
17 "net/http"
18 "net/http/pprof"
19
20 "github.com/prometheus/client_golang/prometheus"
21 "github.com/prometheus/client_golang/prometheus/promhttp"
22 )
23
24 type httpServerBuilder struct {
25 mux *http.ServeMux
26 addr string
27 profiling bool
28 reg prometheus.Gatherer
29 }
30
31 func newHTTPServerBuilder() *httpServerBuilder {
32 return &httpServerBuilder{
33 mux: http.NewServeMux(),
34 }
35 }
36
37
38 func (b *httpServerBuilder) Addr(addr string) *httpServerBuilder {
39 b.addr = addr
40
41 return b
42 }
43
44
45 func (b *httpServerBuilder) Metrics(reg prometheus.Gatherer) *httpServerBuilder {
46 b.reg = reg
47
48 return b
49 }
50
51
52 func (b *httpServerBuilder) Profiling(profiling bool) *httpServerBuilder {
53 b.profiling = profiling
54
55 return b
56 }
57
58
59 func (b *httpServerBuilder) Build() *http.Server {
60 registerMetrics(b.mux, b.reg)
61
62 if b.profiling {
63 registerProfiler(b.mux)
64 }
65
66 return &http.Server{
67 Addr: b.addr,
68 Handler: b.mux,
69 }
70 }
71
72 func registerMetrics(mux *http.ServeMux, g prometheus.Gatherer) {
73 if g != nil {
74 mux.Handle("/metrics", promhttp.HandlerFor(g, promhttp.HandlerOpts{}))
75 }
76 }
77
78 func registerProfiler(mux *http.ServeMux) {
79 mux.HandleFunc("/debug/pprof/", pprof.Index)
80 mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
81 mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
82 mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
83 mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
84 }
85