forked from tinode/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_pprof.go
53 lines (41 loc) · 1.4 KB
/
http_pprof.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
// Debug tooling. Dumps named profile in response to HTTP request at
// http(s)://<host-name>/<configured-path>/<profile-name>
// See godoc for the list of possible profile names: https://golang.org/pkg/runtime/pprof/#Profile
package main
import (
"fmt"
"net/http"
"path"
"runtime/pprof"
"strings"
"github.com/tinode/chat/server/logs"
)
var pprofHttpRoot string
// Expose debug profiling at the given URL path.
func servePprof(mux *http.ServeMux, serveAt string) {
if serveAt == "" || serveAt == "-" {
return
}
pprofHttpRoot = path.Clean("/"+serveAt) + "/"
mux.HandleFunc(pprofHttpRoot, profileHandler)
logs.Info.Printf("pprof: profiling info exposed at '%s'", pprofHttpRoot)
}
func profileHandler(wrt http.ResponseWriter, req *http.Request) {
wrt.Header().Set("X-Content-Type-Options", "nosniff")
wrt.Header().Set("Content-Type", "text/plain; charset=utf-8")
profileName := strings.TrimPrefix(req.URL.Path, pprofHttpRoot)
profile := pprof.Lookup(profileName)
if profile == nil {
servePprofError(wrt, http.StatusNotFound, "Unknown profile '"+profileName+"'")
return
}
// Respond with the requested profile.
profile.WriteTo(wrt, 2)
}
func servePprofError(wrt http.ResponseWriter, status int, txt string) {
wrt.Header().Set("Content-Type", "text/plain; charset=utf-8")
wrt.Header().Set("X-Go-Pprof", "1")
wrt.Header().Del("Content-Disposition")
wrt.WriteHeader(status)
fmt.Fprintln(wrt, txt)
}