Skip to content

Commit 06bcd7d

Browse files
committed
Enable http pprof connections when pprof-address is set.
Add flag to specify the address where those connections listen. Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent a06dec8 commit 06bcd7d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

api/http/pprof/pprof.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package pprof
2+
3+
import (
4+
"expvar"
5+
"fmt"
6+
"net/http"
7+
"net/http/pprof"
8+
9+
"github.com/Sirupsen/logrus"
10+
)
11+
12+
func Enable(address string) {
13+
http.Handle("/", http.RedirectHandler("/debug/pprof", http.StatusMovedPermanently))
14+
15+
http.Handle("/debug/vars", http.HandlerFunc(expVars))
16+
http.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
17+
http.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
18+
http.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
19+
http.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
20+
http.Handle("/debug/pprof/block", pprof.Handler("block"))
21+
http.Handle("/debug/pprof/heap", pprof.Handler("heap"))
22+
http.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
23+
http.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
24+
25+
go http.ListenAndServe(address, nil)
26+
logrus.Debug("pprof listening in address %s", address)
27+
}
28+
29+
// Replicated from expvar.go as not public.
30+
func expVars(w http.ResponseWriter, r *http.Request) {
31+
first := true
32+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
33+
fmt.Fprintf(w, "{\n")
34+
expvar.Do(func(kv expvar.KeyValue) {
35+
if !first {
36+
fmt.Fprintf(w, ",\n")
37+
}
38+
first = false
39+
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
40+
})
41+
fmt.Fprintf(w, "\n}\n")
42+
}

containerd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ var daemonFlags = []cli.Flag{
5555
Value: &cli.StringSlice{},
5656
Usage: "specify additional runtime args",
5757
},
58+
cli.StringFlag{
59+
Name: "pprof-address",
60+
Usage: "http address to listen for pprof events",
61+
},
5862
}
5963

6064
func main() {

containerd/main_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/cloudfoundry/gosigar"
1313
"github.com/codegangsta/cli"
1414
"github.com/cyberdelia/go-metrics-graphite"
15+
"github.com/docker/containerd/api/http/pprof"
1516
"github.com/docker/containerd/osutils"
1617
"github.com/docker/containerd/supervisor"
1718
"github.com/rcrowley/go-metrics"
@@ -37,6 +38,10 @@ func setAppBefore(app *cli.App) {
3738
if err := debugMetrics(context.GlobalDuration("metrics-interval"), context.GlobalString("graphite-address")); err != nil {
3839
return err
3940
}
41+
42+
}
43+
if p := context.GlobalString("pprof-address"); len(p) > 0 {
44+
pprof.Enable(p)
4045
}
4146
if err := checkLimits(); err != nil {
4247
return err

0 commit comments

Comments
 (0)