diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ab699600..9f90157f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ be deprecated eventually. - [#2339](https://github.com/influxdata/telegraf/pull/2339): Increment gather_errors for all errors emitted by inputs. - [#2071](https://github.com/influxdata/telegraf/issues/2071): Use official docker SDK. - [#1678](https://github.com/influxdata/telegraf/pull/1678): Add AMQP consumer input plugin +- [#2512](https://github.com/influxdata/telegraf/pull/2512): Added pprof tool. - [#2501](https://github.com/influxdata/telegraf/pull/2501): Support DEAD(X) state in system input plugin. - [#2522](https://github.com/influxdata/telegraf/pull/2522): Add support for mongodb client certificates. - [#1948](https://github.com/influxdata/telegraf/pull/1948): Support adding SNMP table indexes as tags. diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 16f7845d0..40e90a1ec 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -4,6 +4,8 @@ import ( "flag" "fmt" "log" + "net/http" + _ "net/http/pprof" // Comment this line to disable pprof endpoint. "os" "os/signal" "runtime" @@ -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") @@ -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: @@ -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{} @@ -267,6 +275,23 @@ func main() { processorFilters = strings.Split(":"+strings.TrimSpace(*fProcessorFilters)+":", ":") } + if *pprofAddr != "" { + go func() { + pprofHostPort := *pprofAddr + parts := strings.Split(pprofHostPort, ":") + if len(parts) == 2 && parts[0] == "" { + pprofHostPort = fmt.Sprintf("localhost:%s", parts[1]) + } + pprofHostPort = "http://" + pprofHostPort + "/debug/pprof" + + log.Printf("I! Starting pprof HTTP server at: %s", pprofHostPort) + + if err := http.ListenAndServe(*pprofAddr, nil); err != nil { + log.Fatal("E! " + err.Error()) + } + }() + } + if len(args) > 0 { switch args[0] { case "version": diff --git a/docs/PROFILING.md b/docs/PROFILING.md new file mode 100644 index 000000000..a0851c8f1 --- /dev/null +++ b/docs/PROFILING.md @@ -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. +