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