diff --git a/aperture.go b/aperture.go index e885936..3da60ba 100644 --- a/aperture.go +++ b/aperture.go @@ -37,6 +37,9 @@ import ( "google.golang.org/grpc/keepalive" "google.golang.org/protobuf/encoding/protojson" "gopkg.in/yaml.v2" + + // Blank import to set up profiling HTTP handlers. + _ "net/http/pprof" ) const ( @@ -185,6 +188,27 @@ func (a *Aperture) Start(errChan chan error) error { "exporter: %v", err) } + // Enable http profiling and validate profile port number if requested. + if a.cfg.ProfilePort != 0 { + if a.cfg.ProfilePort < 1024 || a.cfg.ProfilePort > 65535 { + return fmt.Errorf("the profile port must be between " + + "1024 and 65535") + } + + go func() { + http.Handle("/", http.RedirectHandler( + "/debug/pprof", http.StatusSeeOther, + )) + + listenAddr := fmt.Sprintf( + "localhost:%d", a.cfg.ProfilePort, + ) + + log.Infof("Starting profile server at %s", listenAddr) + fmt.Println(http.ListenAndServe(listenAddr, nil)) + }() + } + // Initialize our etcd client. a.etcdClient, err = clientv3.New(clientv3.Config{ Endpoints: []string{a.cfg.Etcd.Host}, diff --git a/config.go b/config.go index ed1c87b..e95b6e6 100644 --- a/config.go +++ b/config.go @@ -125,6 +125,9 @@ type Config struct { // BaseDir is a custom directory to store all aperture flies. BaseDir string `long:"basedir" description:"Directory to place all of aperture's files in."` + + // ProfilePort is the port on which the pprof profile will be served. + ProfilePort uint16 `long:"profile" description:"Enable HTTP profiling on given port -- NOTE port must be between 1024 and 65535"` } func (c *Config) validate() error { diff --git a/sample-conf.yaml b/sample-conf.yaml index 6493d5a..057e5cd 100644 --- a/sample-conf.yaml +++ b/sample-conf.yaml @@ -19,6 +19,10 @@ debuglevel: "debug" autocert: false servername: aperture.example.com +# The port on which the pprof profile will be served. If no port is provided, +# the profile will not be served. +profile: 9999 + # Settings for the lnd node used to generate payment requests. All of these # options are required. authenticator: