Skip to content

Commit

Permalink
Enable profiling at cmd level
Browse files Browse the repository at this point in the history
  • Loading branch information
iknite committed Mar 7, 2019
1 parent f7a99ea commit fc66637
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

type cmdContext struct {
apiKey, logLevel, configFile, path string
disableConfig bool
disableConfig, profiling bool
}

type clientContext struct {
Expand Down
12 changes: 12 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package cmd

import (
"net/http"
_ "net/http/pprof" // this will enable the default profiling capabilities

"github.com/bbva/qed/log"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand All @@ -33,7 +36,14 @@ func NewRootCommand(args []string) *cobra.Command {
Short: "QED is a client for the verifiable log server",
// TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if ctx.profiling {
go func() {
if err := http.ListenAndServe("127.0.0.1:6060", nil); err != http.ErrServerClosed {
log.Errorf("Can't start profiling HTTP server: %s", err)
}
}()

}
if ctx.configFile != "" {
v.SetConfigFile(ctx.configFile)
} else {
Expand Down Expand Up @@ -74,11 +84,13 @@ func NewRootCommand(args []string) *cobra.Command {
f.StringVarP(&ctx.logLevel, "log", "l", "error", "Choose between log levels: silent, error, info and debug")
f.StringVarP(&ctx.apiKey, "apikey", "k", "", "Server api key")
f.StringVarP(&ctx.path, "path", "p", "/var/tmp/qed", "Qed root path for storage configuration and credentials")
f.BoolVarP(&ctx.profiling, "profiling", "f", false, "Allow a pprof url (localhost:6060) for profiling purposes")

// Lookups
v.BindPFlag("log", f.Lookup("log"))
v.BindPFlag("api_key", f.Lookup("apikey"))
v.BindPFlag("path", f.Lookup("path"))
v.BindPFlag("profiling", f.Lookup("profiling"))

cmd.AddCommand(
newStartCommand(ctx),
Expand Down
3 changes: 0 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func newStartCommand(ctx *cmdContext) *cobra.Command {
// Bindings
conf.APIKey = ctx.apiKey
conf.NodeID = v.GetString("server.node-id")
conf.EnableProfiling = v.GetBool("server.profiling")
conf.PrivateKeyPath, _ = homedir.Expand(v.GetString("server.key"))
conf.SSLCertificate, _ = homedir.Expand(v.GetString("server.tls.certificate"))
conf.SSLCertificateKey, _ = homedir.Expand(v.GetString("server.tls.certificate_key"))
Expand Down Expand Up @@ -84,7 +83,6 @@ func newStartCommand(ctx *cmdContext) *cobra.Command {
f := cmd.Flags()
hostname, _ := os.Hostname()
f.StringVar(&conf.NodeID, "node-id", hostname, "Unique name for node. If not set, fallback to hostname")
f.BoolVarP(&conf.EnableProfiling, "profiling", "f", false, "Allow a pprof url (localhost:6060) for profiling purposes")
f.StringVar(&conf.PrivateKeyPath, "keypath", fmt.Sprintf("%s/%s", ctx.path, "id_ed25519"), "Server Singning private key file path")
f.StringVar(&conf.SSLCertificate, "certificate", fmt.Sprintf("%s/%s", ctx.path, "server.crt"), "Server crt file")
f.StringVar(&conf.SSLCertificateKey, "certificate-key", fmt.Sprintf("%s/%s", ctx.path, "server.key"), "Server key file")
Expand All @@ -104,7 +102,6 @@ func newStartCommand(ctx *cmdContext) *cobra.Command {

// Lookups
v.BindPFlag("server.node-id", f.Lookup("node-id"))
v.BindPFlag("server.profiling", f.Lookup("profiling"))
v.BindPFlag("server.key", f.Lookup("keypath"))
v.BindPFlag("server.tls.certificate", f.Lookup("certificate"))
v.BindPFlag("server.tls.certificate_key", f.Lookup("certificate-key"))
Expand Down
2 changes: 1 addition & 1 deletion config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
log: error # Choose between log levels: silent, error, info and debug.
api_key: "my-key" # The application namespace used in all the nodes.
path: "/var/tmp/qed/" # Set root path used for dbpath, raftpath, private key and certificates.
profiling: false # Allow a pprof url (localhost:6060) for profiling purposes.

###############################################################################
# Server Configuration (where it collect and processes events).
###############################################################################
server:
node_id: "hostname" # Unique name for node. If not set, fallback to hostname.
profiling: false # Allow a pprof url (localhost:6060) for profiling purposes.
metrics: false # Allow metrics
key: "/var/tmp/qed/id_ed25519" # Path to the ed25519 key file.
tls:
Expand Down
4 changes: 0 additions & 4 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ type Config struct {
// Path to the private key file used to sign snapshots.
PrivateKeyPath string

// Enables profiling endpoint.
EnableProfiling bool

// Enables tampering endpoint.
EnableTampering bool

Expand Down Expand Up @@ -94,7 +91,6 @@ func DefaultConfig() *Config {
GossipJoinAddr: []string{},
DBPath: currentDir + "/db",
RaftPath: currentDir + "/wal",
EnableProfiling: false,
EnableTampering: false,
EnableTLS: false,
SSLCertificate: "",
Expand Down
23 changes: 0 additions & 23 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"io"
"io/ioutil"
"net/http"
_ "net/http/pprof" // this will enable the default profiling capabilities
"os"
"strconv"

Expand Down Expand Up @@ -59,7 +58,6 @@ type Server struct {
mgmtServer *http.Server
raftBalloon *raftwal.RaftBalloon
tamperingServer *http.Server
profilingServer *http.Server
metricsServer *http.Server
prometheusRegistry *prometheus.Registry
signer sign.Signer
Expand Down Expand Up @@ -173,9 +171,6 @@ func NewServer(conf *Config) (*Server, error) {
tamperMux := tampering.NewTamperingApi(store, hashing.NewSha256Hasher())
server.tamperingServer = newHTTPServer(fmt.Sprintf("localhost:1880%d", id), tamperMux)
}
if conf.EnableProfiling {
server.profilingServer = newHTTPServer(fmt.Sprintf("localhost:606%d", id), nil)
}

r := prometheus.NewRegistry()
metrics.Register(r)
Expand Down Expand Up @@ -227,15 +222,6 @@ func (s *Server) Start() error {
}
}()

if s.profilingServer != nil {
go func() {
log.Debugf(" * Starting profiling HTTP server in addr: localhost:6060")
if err := s.profilingServer.ListenAndServe(); err != http.ErrServerClosed {
log.Errorf("Can't start profiling HTTP server: %s", err)
}
}()
}

if s.tamperingServer != nil {
log.Info(">>>>>>>>>>>>>>>>>>> FIXME: Tampering is enabled! Do not run this in production!")
go func() {
Expand Down Expand Up @@ -318,15 +304,6 @@ func (s *Server) Stop() error {
log.Debugf("Done.\n")
}

if s.profilingServer != nil {
log.Debugf("Profiling enabled: stopping server...")
if err := s.profilingServer.Shutdown(context.Background()); err != nil { // TODO include timeout instead nil
log.Error(err)
return err
}
log.Debugf("Done.\n")
}

log.Debugf("Stopping MGMT server...")
if err := s.mgmtServer.Shutdown(context.Background()); err != nil { // TODO include timeout instead nil
log.Error(err)
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ func setupServer(id int, joinAddr string, tls bool, t *testing.T) (scope.TestF,
conf.SSLCertificate = fmt.Sprintf("%s/.ssh/server.crt", usr.HomeDir)
conf.SSLCertificateKey = fmt.Sprintf("%s/.ssh/server.key", usr.HomeDir)
}
conf.EnableProfiling = true
conf.EnableTampering = true
conf.EnableTLS = tls

Expand Down

0 comments on commit fc66637

Please sign in to comment.