Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pprof tool #2512

Merged
merged 6 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof" // Comment this line to disable pprof endpoint.
"os"
"os/signal"
"runtime"
Expand All @@ -24,6 +26,8 @@ import (

var fDebug = flag.Bool("debug", false,
"turn on debug logging")
var pprofAddr = flag.String("pprof-addr", "",
"pprof address to listen on, not activate pprof if empty")
var fQuiet = flag.Bool("quiet", false,
"run in quiet mode")
var fTest = flag.Bool("test", false, "gather metrics, print them out, and exit")
Expand Down Expand Up @@ -87,6 +91,7 @@ The commands & flags are:
--output-filter filter the output plugins to enable, separator is :
--usage print usage for a plugin, ie, 'telegraf --usage mysql'
--debug print metrics as they're generated to stdout
--pprof-addr pprof address to listen on, format: localhost:6060 or :6060
--quiet run in quiet mode

Examples:
Expand All @@ -105,6 +110,9 @@ Examples:

# run telegraf, enabling the cpu & memory input, and influxdb output plugins
telegraf --config telegraf.conf --input-filter cpu:mem --output-filter influxdb

# run telegraf with pprof
telegraf --config telegraf.conf --pprof-addr localhost:6060
`

var stop chan struct{}
Expand Down Expand Up @@ -267,6 +275,18 @@ func main() {
processorFilters = strings.Split(":"+strings.TrimSpace(*fProcessorFilters)+":", ":")
}

if *pprofAddr != "" {
go func() {
log.Printf(
"I! Starting pprof on %s. Open profiling tools page in browser: /debug/pprof",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have all the information we need to print the full url to this page, let have it write something like:
I! Starting pprof HTTP server at http://localhost:6060/debug/pprof This way I can click on it or copy paste it more easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielnelson, thanks for review. Changes done.

Could you also move forward our contributions:
#2387
#2594

We hope to see them in 1.3 too

*pprofAddr,
)
if err := http.ListenAndServe(*pprofAddr, nil); err != nil {
log.Fatal("E! " + err.Error())
}
}()
}

if len(args) > 0 {
switch args[0] {
case "version":
Expand Down
24 changes: 24 additions & 0 deletions docs/PROFILING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Telegraf profiling

Telegraf uses the standard package `net/http/pprof`. This package serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.

By default, the profiling is turned off.

To enable profiling you need to specify address to config parameter `pprof-addr`, for example:

```
telegraf --config telegraf.conf --pprof-addr localhost:6060
```

There are several paths to get different profiling information:

To look at the heap profile:

`go tool pprof http://localhost:6060/debug/pprof/heap`

or to look at a 30-second CPU profile:

`go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30`

To view all available profiles, open `http://localhost:6060/debug/pprof/` in your browser.