Repository navigation
Expand file tree
/
Copy pathadmin.go
More file actions
65 lines (57 loc) · 1.35 KB
/
Copy pathadmin.go
File metadata and controls
65 lines (57 loc) · 1.35 KB
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
package admin
import (
"fmt"
"net/http"
"net/http/pprof"
"strings"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type handler struct {
promHandler http.Handler
enablePprof bool
}
// NewServer returns an initialized `http.Server`, configured to listen on an address.
func NewServer(addr string, enablePprof bool) *http.Server {
h := &handler{
promHandler: promhttp.Handler(),
enablePprof: enablePprof,
}
return &http.Server{
Addr: addr,
Handler: h,
}
}
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
debugPathPrefix := "/debug/pprof/"
if h.enablePprof && strings.HasPrefix(req.URL.Path, debugPathPrefix) {
switch req.URL.Path {
case fmt.Sprintf("%scmdline", debugPathPrefix):
pprof.Cmdline(w, req)
case fmt.Sprintf("%sprofile", debugPathPrefix):
pprof.Profile(w, req)
case fmt.Sprintf("%strace", debugPathPrefix):
pprof.Trace(w, req)
case fmt.Sprintf("%ssymbol", debugPathPrefix):
pprof.Symbol(w, req)
default:
pprof.Index(w, req)
}
return
}
switch req.URL.Path {
case "/metrics":
h.promHandler.ServeHTTP(w, req)
case "/ping":
h.servePing(w)
case "/ready":
h.serveReady(w)
default:
http.NotFound(w, req)
}
}
func (h *handler) servePing(w http.ResponseWriter) {
w.Write([]byte("pong\n"))
}
func (h *handler) serveReady(w http.ResponseWriter) {
w.Write([]byte("ok\n"))
}