-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmonitor.go
More file actions
39 lines (35 loc) · 1.03 KB
/
Copy pathmonitor.go
File metadata and controls
39 lines (35 loc) · 1.03 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
package http
import (
"net/http"
nprof "net/http/pprof"
"runtime/pprof"
"github.com/Aize-Public/forego/ctx"
"github.com/Aize-Public/forego/ctx/log"
"github.com/Aize-Public/forego/utils/prom"
)
func (this *Server) SetupPrometheus(c ctx.C, path string) {
if path == "" {
path = "/metrics"
}
this.mux.Handle(path, prom.Handler())
}
// register /pprof/ and /goroutines under the given prefix
func (this *Server) SetupMonitoring(c ctx.C, prefix string) {
this.mux.HandleFunc(prefix+"/pprof/", nprof.Index)
this.mux.HandleFunc(prefix+"/pprof/cmdline", nprof.Cmdline)
this.mux.HandleFunc(prefix+"/pprof/profile", nprof.Profile)
this.mux.HandleFunc(prefix+"/pprof/symbol", nprof.Symbol)
this.mux.HandleFunc(prefix+"/pprof/trace", nprof.Trace)
this.mux.HandleFunc(prefix+"/goroutines", func(w http.ResponseWriter, r *http.Request) {
c := r.Context()
p := pprof.Lookup("goroutine")
if p == nil {
w.WriteHeader(204)
return
}
err := p.WriteTo(w, 1)
if err != nil {
log.Errorf(c, "can't write profile: %v", err)
}
})
}