-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
55 lines (44 loc) · 1.2 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
package god
import (
"net/http"
"net/http/pprof"
"runtime"
"strconv"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Debug will run a god compatible debug endpoint.
func Debug(alternativePort ...int) {
// prepare port
port := 6060
// check alternative port
if len(alternativePort) > 0 {
port = alternativePort[0]
}
// enable mutex profiling
runtime.SetMutexProfileFraction(100)
// enable block profiling
runtime.SetBlockProfileRate(100)
// enable debugging
go func() {
// create mux
mux := http.NewServeMux()
// add pprof endpoints
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
// add prometheus endpoint
mux.Handle("/metrics", promhttp.Handler())
// add status endpoint
mux.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
// launch debug server
err := http.ListenAndServe("0.0.0.0:"+strconv.Itoa(port), mux)
if err != nil {
println(err.Error())
}
}()
}