multi: add pprof

This commit adds a config option that can be set inorder to spin up a
pprof profile server on the given port.
This commit is contained in:
Elle Mouton
2022-04-26 14:29:10 +02:00
parent 4113bf3f20
commit cbc8a414f7
3 changed files with 31 additions and 0 deletions

View File

@@ -37,6 +37,9 @@ import (
"google.golang.org/grpc/keepalive" "google.golang.org/grpc/keepalive"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
// Blank import to set up profiling HTTP handlers.
_ "net/http/pprof"
) )
const ( const (
@@ -185,6 +188,27 @@ func (a *Aperture) Start(errChan chan error) error {
"exporter: %v", err) "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. // Initialize our etcd client.
a.etcdClient, err = clientv3.New(clientv3.Config{ a.etcdClient, err = clientv3.New(clientv3.Config{
Endpoints: []string{a.cfg.Etcd.Host}, Endpoints: []string{a.cfg.Etcd.Host},

View File

@@ -125,6 +125,9 @@ type Config struct {
// BaseDir is a custom directory to store all aperture flies. // BaseDir is a custom directory to store all aperture flies.
BaseDir string `long:"basedir" description:"Directory to place all of aperture's files in."` 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 { func (c *Config) validate() error {

View File

@@ -19,6 +19,10 @@ debuglevel: "debug"
autocert: false autocert: false
servername: aperture.example.com 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 # Settings for the lnd node used to generate payment requests. All of these
# options are required. # options are required.
authenticator: authenticator: