/
threaddump.go
36 lines (32 loc) · 1.05 KB
/
threaddump.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
package threaddump
import (
"bytes"
"fmt"
"net/http"
"runtime/pprof"
)
var lookup = pprof.Lookup
// HandleThreaddump - Prints out a plaintext snapshot of the process's
// current-running thread stats
// https://forum.golangbridge.org/t/how-to-take-thread-dump-in-golang/11417/4
func HandleThreaddump(writer http.ResponseWriter, request *http.Request) {
var buff bytes.Buffer
profile := lookup("goroutine")
if profile == nil {
writer.Header().Add("Content-Type", "application/json")
writer.WriteHeader(http.StatusInternalServerError)
errorMsg := `{"error": { "message": "unable to read profile" } }`
_, _ = writer.Write([]byte(errorMsg))
return
}
if err := profile.WriteTo(&buff, 1); err != nil {
writer.Header().Add("Content-Type", "application/json")
writer.WriteHeader(http.StatusInternalServerError)
errorMsg := fmt.Sprintf(`{"error": { "message": "unable to read profile: %v" } }`, err)
_, _ = writer.Write([]byte(errorMsg))
return
}
payload := buff.Bytes()
writer.Header().Add("Content-Type", "text/plain")
_, _ = writer.Write(payload)
}