Skip to content

Commit

Permalink
Make pprof the default format, remove guessing
Browse files Browse the repository at this point in the history
@swatson314159 reported that the current behavior is confusing in GH issue #1,
and I agree. Hopefully the new behavior is less surprising.
  • Loading branch information
felixge committed Jun 30, 2020
1 parent 517eaf9 commit 90cb49f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ Additionally fgprof supports the plain text format used by Brendan Gregg's [Flam
```
git clone https://github.com/brendangregg/FlameGraph
cd FlameGraph
curl -s 'localhost:6060/debug/fgprof?seconds=3' > fgprof.fold
./flamegraph.pl fgprof.fold > fgprof.svg
curl -s 'localhost:6060/debug/fgprof?seconds=3&format=folded' > fgprof.folded
./flamegraph.pl fgprof.folded > fgprof.svg
```

![](./assets/fgprof_gregg.png)
Expand Down
20 changes: 3 additions & 17 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package fgprof
import (
"fmt"
"net/http"
"strings"
"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 Brendan Gregg's "folded" stack
// format, or Google's "pprof" format. If no "format" is given, the handler
// tries to guess the best format based on the http headers.
// 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
Expand All @@ -22,23 +20,11 @@ func Handler() http.Handler {

format := Format(r.URL.Query().Get("format"))
if format == "" {
format = guessFormat(r)
format = FormatPprof
}

stop := Start(w, format)
defer stop()
time.Sleep(time.Duration(seconds) * time.Second)
})
}

// guessFormat returns FormatPprof if it looks like pprof sent r, otherwise
// FormatFolded. It's not meant to be a perfect heuristic, but a nice
// convenience for users that don't want to specify the format explicitly.
func guessFormat(r *http.Request) Format {
for _, format := range r.Header["Accept-Encoding"] {
if strings.ToLower(format) == "gzip" {
return FormatPprof
}
}
return FormatFolded
}

0 comments on commit 90cb49f

Please sign in to comment.