-
Notifications
You must be signed in to change notification settings - Fork 99
/
handler.go
35 lines (31 loc) · 951 Bytes
/
handler.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
package fgprof
import (
"fmt"
"net/http"
"strconv"
"time"
)
// Handler returns an http handler that takes an optional "seconds" query
// argument that defaults to "30" and produces a profile over this duration.
// The optional "format" parameter controls if the output is written in
// Google's "pprof" format (default) or Brendan Gregg's "folded" stack format.
func Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var seconds int
var err error
if s := r.URL.Query().Get("seconds"); s == "" {
seconds = 30
} else if seconds, err = strconv.Atoi(s); err != nil || seconds <= 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "bad seconds: %d: %s\n", seconds, err)
return
}
format := Format(r.URL.Query().Get("format"))
if format == "" {
format = FormatPprof
}
stop := Start(w, format)
defer stop()
time.Sleep(time.Duration(seconds) * time.Second)
})
}