Skip to content

Commit

Permalink
Merge pull request #4 from felixge/gh-3
Browse files Browse the repository at this point in the history
Default to 30s duration like net/http/pprof
  • Loading branch information
felixge authored Jul 12, 2020
2 parents c14b6f4 + 7b3aa0f commit 66ab7d9
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 66ab7d9

Please sign in to comment.