/
diagnostics.go
64 lines (53 loc) · 1.61 KB
/
diagnostics.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
package service
import (
"net/http"
"net/http/pprof"
"github.com/devdammit/shekel/pkg/encoding/json"
"github.com/devdammit/shekel/pkg/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
serviceInfo map[string]string
)
func SetInfo(info map[string]string) {
serviceInfo = info
}
type DiagnosticServerConfig struct {
DocsDir string
ConfigClientHandler http.HandlerFunc
StateHandler http.HandlerFunc
}
func StartDiagnosticsServer(addr string) {
startDiagnosticsServer(addr, "", nil, nil)
}
func StartDiagnosticsServerWithConfig(addr string, cfg DiagnosticServerConfig) {
if addr != "" {
startDiagnosticsServer(addr, cfg.DocsDir, cfg.ConfigClientHandler, cfg.StateHandler)
}
}
func startDiagnosticsServer(addr, docs string, configsHandler http.HandlerFunc, stateHandler http.HandlerFunc) {
jsonInfo, err := json.Marshal(serviceInfo)
if err != nil {
panic(err)
}
mux := http.NewServeMux()
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)
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/info", func(w http.ResponseWriter, _ *http.Request) {
if _, err := w.Write(jsonInfo); err != nil {
log.With(log.Err(err)).Error("failed to write http response")
}
})
// docs
if docs != "" {
mux.Handle("/docs/", http.FileServer(http.Dir(docs)))
}
if stateHandler != nil {
mux.Handle("/state/", stateHandler)
}
go NewHTTPServer(addr, 0, mux).Start()
}