Skip to content

Commit

Permalink
fix(cmd): add flag debug to enable pprof (#1512)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad authored Sep 29, 2024
1 parent 1a609e1 commit 5525298
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,16 @@ func CreateNode(numValidators int, chain genesis.ChainType, workingDir string,
// The passwordFetcher will be used to fetch the password for the default_wallet if it is encrypted.
// It returns an error if the genesis doc or default_wallet can't be found inside the working directory.
// TODO: write test for me.
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool)) (
func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string, bool), enableDebugger bool) (
*node.Node, *wallet.Wallet, error,
) {
conf, gen, err := MakeConfig(workingDir)
if err != nil {
return nil, nil, err
}

conf.HTTP.EnableDebugger = enableDebugger

defaultWalletPath := PactusDefaultWalletPath(workingDir)
walletInstance, err := wallet.Open(defaultWalletPath, true,
wallet.WithCustomServers([]string{conf.GRPC.Listen}))
Expand Down
4 changes: 3 additions & 1 deletion cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func buildStartCmd(parentCmd *cobra.Command) {
passwordOpt := startCmd.Flags().StringP("password", "p", "",
"the wallet password")

debuggerOpt := startCmd.Flags().BoolP("debug", "d", false, "enable pprof debugger")

startCmd.Run = func(_ *cobra.Command, _ []string) {
workingDir, _ := filepath.Abs(*workingDirOpt)
// change working directory
Expand Down Expand Up @@ -58,7 +60,7 @@ func buildStartCmd(parentCmd *cobra.Command) {
return password, true
}
node, _, err := cmd.StartNode(
workingDir, passwordFetcher)
workingDir, passwordFetcher, *debuggerOpt)
cmd.FatalErrorCheck(err)

cmd.TrapSignal(func() {
Expand Down
4 changes: 3 additions & 1 deletion cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const appID = "com.github.pactus-project.pactus.pactus-gui"

var (
workingDirOpt *string
debuggerOpt *bool
passwordOpt *string
testnetOpt *bool
)
Expand All @@ -33,6 +34,7 @@ func init() {
workingDirOpt = flag.String("working-dir", cmd.PactusDefaultHomeDir(), "working directory path")
passwordOpt = flag.String("password", "", "wallet password")
testnetOpt = flag.Bool("testnet", false, "initializing for the testnet")
debuggerOpt = flag.Bool("debug", false, "enable pprof debugger")
version.NodeAgent.AppType = "gui"
// the gtk on macos should run on main thread.
if runtime.GOOS == "darwin" {
Expand Down Expand Up @@ -155,7 +157,7 @@ func newNode(workingDir string) (*node.Node, *wallet.Wallet, error) {

return getWalletPassword(wlt)
}
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher, *debuggerOpt)
if err != nil {
return nil, nil, err
}
Expand Down
10 changes: 6 additions & 4 deletions www/http/config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package http

type Config struct {
Enable bool `toml:"enable"`
Listen string `toml:"listen"`
Enable bool `toml:"enable"`
Listen string `toml:"listen"`
EnableDebugger bool `toml:"-"` // EnableDebugger is private configs
}

func DefaultConfig() *Config {
return &Config{
Enable: false,
Listen: "",
Enable: false,
Listen: "",
EnableDebugger: false,
}
}

Expand Down
7 changes: 4 additions & 3 deletions www/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package http
import (
"bytes"
"context"
"expvar"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -87,8 +86,10 @@ func (s *Server) StartServer(grpcServer string) error {
s.router.HandleFunc("/validator/address/{address}", s.GetValidatorHandler)
s.router.HandleFunc("/validator/number/{number}", s.GetValidatorByNumberHandler)
s.router.HandleFunc("/metrics/prometheus", promhttp.Handler().ServeHTTP)
s.router.HandleFunc("/debug/pprof/", pprof.Index)
s.router.Handle("/debug/vars", expvar.Handler())

if s.config.EnableDebugger {
s.router.HandleFunc("/debug/pprof/", pprof.Index)
}

if s.enableAuth {
http.Handle("/", handlers.RecoveryHandler()(basicAuth(s.router)))
Expand Down

0 comments on commit 5525298

Please sign in to comment.