/
options.go
53 lines (45 loc) · 1.49 KB
/
options.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
package metrics
import (
"fmt"
"net/http"
netPProf "net/http/pprof"
"runtime"
runtimePProf "runtime/pprof"
)
const (
pprofIndexPath = "/debug/pprof/"
pprofCmdLinePath = "/debug/pprof/cmdline"
pprofProfilePath = "/debug/pprof/profile"
pprofSymbolPath = "/debug/pprof/symbol"
pprofTracePath = "/debug/pprof/trace"
pprofHeapPath = "/debug/pprof/heap"
)
type ServerOption func(mux *http.ServeMux) *http.ServeMux
func WithProfilingHooks() ServerOption {
return func(mux *http.ServeMux) *http.ServeMux {
mux.HandleFunc(pprofIndexPath, netPProf.Index)
mux.HandleFunc(pprofCmdLinePath, netPProf.Cmdline)
mux.HandleFunc(pprofProfilePath, netPProf.Profile)
mux.HandleFunc(pprofSymbolPath, netPProf.Symbol)
mux.HandleFunc(pprofTracePath, netPProf.Trace)
mux.HandleFunc(pprofHeapPath, memProfileHandler)
return mux
}
}
func memProfileHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", `attachment; filename="profile"`)
runtime.GC()
if err := runtimePProf.WriteHeapProfile(w); err != nil {
serveError(w, http.StatusInternalServerError,
fmt.Sprintf("Could not write heap profile: %s", err))
}
}
func serveError(w http.ResponseWriter, status int, txt string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.Header().Del("Content-Disposition")
w.WriteHeader(status)
fmt.Fprintln(w, txt)
}