diff --git a/handler.go b/handler.go index 93d9663..a25cdc6 100644 --- a/handler.go +++ b/handler.go @@ -6,14 +6,16 @@ import ( "time" ) -// Handler returns an http handler that requires a "seconds" query argument -// 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. +// 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 - if _, err := fmt.Sscanf(r.URL.Query().Get("seconds"), "%d", &seconds); err != nil { + if s := r.URL.Query().Get("seconds"); s == "" { + seconds = 30 + } else if _, err := fmt.Sscanf(s, "%d", &seconds); err != nil || seconds <= 0 { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "bad seconds: %d: %s\n", seconds, err) }