Skip to content

Commit

Permalink
Enable pprof HTTP endpoints with CLI flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Aug 29, 2024
1 parent 6992208 commit 2d96435
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/rosetta/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ VERSION:
Required: false,
Value: math.MaxUint32,
}

cliFlagShouldEnablePprofEndpoints = cli.BoolFlag{
Name: "pprof",
Usage: "Whether to enable pprof HTTP endpoints.",
}
)

func getAllCliFlags() []cli.Flag {
Expand Down Expand Up @@ -212,6 +217,7 @@ func getAllCliFlags() []cli.Flag {
cliFlagConfigFileCustomCurrencies,
cliFlagActivationEpochSirius,
cliFlagActivationEpochSpica,
cliFlagShouldEnablePprofEndpoints,
}
}

Expand Down Expand Up @@ -243,6 +249,7 @@ type parsedCliFlags struct {
configFileCustomCurrencies string
activationEpochSirius uint32
activationEpochSpica uint32
shouldEnablePprofEndpoints bool
}

func getParsedCliFlags(ctx *cli.Context) parsedCliFlags {
Expand Down Expand Up @@ -274,5 +281,6 @@ func getParsedCliFlags(ctx *cli.Context) parsedCliFlags {
configFileCustomCurrencies: ctx.GlobalString(cliFlagConfigFileCustomCurrencies.Name),
activationEpochSirius: uint32(ctx.GlobalUint(cliFlagActivationEpochSirius.Name)),
activationEpochSpica: uint32(ctx.GlobalUint(cliFlagActivationEpochSpica.Name)),
shouldEnablePprofEndpoints: ctx.GlobalBool(cliFlagShouldEnablePprofEndpoints.Name),
}
}
4 changes: 4 additions & 0 deletions cmd/rosetta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func startRosetta(ctx *cli.Context) error {
return err
}

if cliFlags.shouldEnablePprofEndpoints {
controllers = append(controllers, newPprofController())
}

httpServer, err := createHttpServer(cliFlags.port, controllers...)
if err != nil {
return err
Expand Down
58 changes: 58 additions & 0 deletions cmd/rosetta/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"net/http"
"net/http/pprof"

"github.com/coinbase/rosetta-sdk-go/server"
)

type pprofController struct {
routes []server.Route
}

// See:
// - https://stackoverflow.com/a/71032595/1475331
// - https://pkg.go.dev/net/http/pprof
func newPprofController() *pprofController {
routes := []server.Route{
{
Method: "GET",
Pattern: "/debug/pprof/",
HandlerFunc: http.HandlerFunc(pprof.Index),
},
{
Method: "GET",
Pattern: "/debug/pprof/cmdline",
HandlerFunc: http.HandlerFunc(pprof.Cmdline),
},
{
Method: "GET",
Pattern: "/debug/pprof/profile",
HandlerFunc: http.HandlerFunc(pprof.Profile),
},
{
Method: "GET",
Pattern: "/debug/pprof/symbol",
HandlerFunc: http.HandlerFunc(pprof.Symbol),
},
{
Method: "GET",
Pattern: "/debug/pprof/trace",
HandlerFunc: http.HandlerFunc(pprof.Trace),
},
{
Method: "GET",
Pattern: "/debug/pprof/{cmd}",
HandlerFunc: http.HandlerFunc(pprof.Index),
},
}

return &pprofController{
routes: routes,
}
}

func (r *pprofController) Routes() server.Routes {
return r.routes
}
3 changes: 2 additions & 1 deletion systemtests/check_with_mesh_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def run_rosetta(configuration: Configuration):
f"--observer-actual-shard={configuration.network_shard}",
f"--network-id={configuration.network_id}",
f"--network-name={configuration.network_name}",
f"--handle-contracts",
f"--native-currency={configuration.native_currency}",
f"--config-custom-currencies={configuration.config_file_custom_currencies}",
f"--first-historical-epoch={current_epoch}",
f"--num-historical-epochs={configuration.num_historical_epochs}",
f"--activation-epoch-sirius={configuration.activation_epoch_sirius}",
f"--activation-epoch-spica={configuration.activation_epoch_spica}",
"--handle-contracts",
"--pprof"
]

return subprocess.Popen(command)
Expand Down

0 comments on commit 2d96435

Please sign in to comment.