Skip to content

Commit

Permalink
Default to 30s duration like net/http/pprof
Browse files Browse the repository at this point in the history
GH-Issue: #3
  • Loading branch information
felixge committed Jul 11, 2020
1 parent d6b8e30 commit 7b3aa0f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 7b3aa0f

Please sign in to comment.