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

Add net/http/pprof profiling endpoint to http port #913

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
// core. This means copying IP objects, slices, de-referencing pointers and taking the actual value, etc

type Control struct {
f *Interface
l *logrus.Logger
cancel context.CancelFunc
sshStart func()
statsStart func()
dnsStart func()
f *Interface
l *logrus.Logger
cancel context.CancelFunc
sshStart func()
httpStart func()
dnsStart func()
}

type ControlHostInfo struct {
Expand All @@ -48,8 +48,8 @@ func (c *Control) Start() {
if c.sshStart != nil {
go c.sshStart()
}
if c.statsStart != nil {
go c.statsStart()
if c.httpStart != nil {
go c.httpStart()
}
if c.dnsStart != nil {
go c.dnsStart()
Expand Down
4 changes: 3 additions & 1 deletion examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ logging:
# As an example, to log as RFC3339 with millisecond precision, set to:
#timestamp_format: "2006-01-02T15:04:05.000Z07:00"

#http:
#listen: 127.0.0.1:8080

#stats:
#type: graphite
#prefix: nebula
Expand All @@ -252,7 +255,6 @@ logging:
#interval: 10s

#type: prometheus
#listen: 127.0.0.1:8080
#path: /metrics
#namespace: prometheusns
#subsystem: nebula
Expand Down
34 changes: 34 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package nebula

import (
"fmt"
"net/http"
_ "net/http/pprof"

"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/config"
)

// startHttp returns a function to start an http server with pprof support and optionally a provided stats
// http handler.
func startHttp(l *logrus.Logger, c *config.C, listen string, statsHandler statsHandlerFunc) (func(), error) {
if listen == "" {
return nil, nil
}

var statsPath string
if statsHandler != nil {
statsPath = c.GetString("stats.path", "")
if statsPath == "" {
return nil, fmt.Errorf("stats.path should not be empty")
}
}

return func() {
l.Infof("Go pprof handler listening on %s at /debug/pprof", listen)
if statsHandler != nil {
http.Handle(statsPath, statsHandler(listen, statsPath))
}
l.Fatal(http.ListenAndServe(listen, nil))
}, nil
}
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,25 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg
go lightHouse.LhUpdateWorker(ctx, ifce)
}

httpListen := c.GetString("http.listen", "")
if httpListen == "" {
if httpListen = c.GetString("stats.listen", ""); httpListen != "" {
l.Warn("http.listen is undef, falling back to stats.listen. stats.listen will be deprecated in a future release.")
}
}

// TODO - stats third-party modules start uncancellable goroutines. Update those libs to accept
// a context so that they can exit when the context is Done.
statsStart, err := startStats(l, c, buildVersion, configTest)

statsHTTPHandler, err := startStats(l, c, httpListen, buildVersion, configTest)
if err != nil {
return nil, util.NewContextualError("Failed to start stats emitter", nil, err)
}

httpStart, err := startHttp(l, c, httpListen, statsHTTPHandler)
if err != nil {
return nil, util.NewContextualError("Failed to start http server", nil, err)
}

if configTest {
return nil, nil
}
Expand All @@ -348,5 +359,5 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg
dnsStart = dnsMain(l, hostMap, c)
}

return &Control{ifce, l, cancel, sshStart, statsStart, dnsStart}, nil
return &Control{ifce, l, cancel, sshStart, httpStart, dnsStart}, nil
}
34 changes: 13 additions & 21 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nebula
import (
"errors"
"fmt"
"log"
"net"
"net/http"
"runtime"
Expand All @@ -19,10 +18,12 @@ import (
"github.com/slackhq/nebula/config"
)

type statsHandlerFunc func(listen, path string) http.Handler

// startStats initializes stats from config. On success, if any further work
// is needed to serve stats, it returns a func to handle that work. If no
// work is needed, it'll return nil. On failure, it returns nil, error.
func startStats(l *logrus.Logger, c *config.C, buildVersion string, configTest bool) (func(), error) {
// is needed to serve stats, it returns a statsHandlerFunc for that work. If
// no work is needed, it'll return nil. On failure, it returns nil, error.
func startStats(l *logrus.Logger, c *config.C, listen, buildVersion string, configTest bool) (f statsHandlerFunc, err error) {
mType := c.GetString("stats.type", "")
if mType == "" || mType == "none" {
return nil, nil
Expand All @@ -33,16 +34,14 @@ func startStats(l *logrus.Logger, c *config.C, buildVersion string, configTest b
return nil, fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
}

var startFn func()
switch mType {
case "graphite":
err := startGraphiteStats(l, interval, c, configTest)
if err != nil {
return nil, err
}
case "prometheus":
var err error
startFn, err = startPrometheusStats(l, interval, c, buildVersion, configTest)
f, err = startPrometheusStats(l, interval, c, listen, buildVersion, configTest)
if err != nil {
return nil, err
}
Expand All @@ -56,7 +55,7 @@ func startStats(l *logrus.Logger, c *config.C, buildVersion string, configTest b
go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval)
go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval)

return startFn, nil
return f, nil
}

func startGraphiteStats(l *logrus.Logger, i time.Duration, c *config.C, configTest bool) error {
Expand All @@ -79,18 +78,12 @@ func startGraphiteStats(l *logrus.Logger, i time.Duration, c *config.C, configTe
return nil
}

func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, buildVersion string, configTest bool) (func(), error) {
func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, listen, buildVersion string, configTest bool) (statsHandlerFunc, error) {
namespace := c.GetString("stats.namespace", "")
subsystem := c.GetString("stats.subsystem", "")

listen := c.GetString("stats.listen", "")
if listen == "" {
return nil, fmt.Errorf("stats.listen should not be empty")
}

path := c.GetString("stats.path", "")
if path == "" {
return nil, fmt.Errorf("stats.path should not be empty")
return nil, fmt.Errorf("http.listen or stats.listen must be defined to use Prometheus stats")
}

pr := prometheus.NewRegistry()
Expand All @@ -114,14 +107,13 @@ func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, buildV
pr.MustRegister(g)
g.Set(1)

var startFn func()
var startHandler statsHandlerFunc
if !configTest {
startFn = func() {
startHandler = func(listen, path string) http.Handler {
l.Infof("Prometheus stats listening on %s at %s", listen, path)
http.Handle(path, promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}))
log.Fatal(http.ListenAndServe(listen, nil))
return promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l})
}
}

return startFn, nil
return startHandler, nil
}