Skip to content

Commit

Permalink
fixed start diag server (#10236)
Browse files Browse the repository at this point in the history
fixed start diag server if metrics address is different from pprof
address

---------

Co-authored-by: taratorio <[email protected]>
  • Loading branch information
dvovk and taratorio authored May 7, 2024
1 parent 40d1327 commit 764706d
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/caplin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func runCaplinNode(cliCtx *cli.Context) error {
log.Error("[Phase1] Could not initialize caplin", "err", err)
return err
}
if _, _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
if _, _, _, err := debug.Setup(cliCtx, true /* root logger */); err != nil {
return err
}
rcfg := beacon_router_configuration.RouterConfiguration{
Expand Down
5 changes: 3 additions & 2 deletions cmd/devnet/devnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (n *devnetNode) run(ctx *cli.Context) error {
var logger log.Logger
var err error
var metricsMux *http.ServeMux
var pprofMux *http.ServeMux

defer n.done()
defer func() {
Expand All @@ -152,7 +153,7 @@ func (n *devnetNode) run(ctx *cli.Context) error {
n.Unlock()
}()

if logger, metricsMux, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
if logger, metricsMux, pprofMux, err = debug.Setup(ctx, false /* rootLogger */); err != nil {
return err
}

Expand Down Expand Up @@ -184,7 +185,7 @@ func (n *devnetNode) run(ctx *cli.Context) error {

n.ethNode, err = enode.New(ctx.Context, n.nodeCfg, n.ethCfg, logger)

diagnostics.Setup(ctx, n.ethNode, metricsMux)
diagnostics.Setup(ctx, n.ethNode, metricsMux, pprofMux)

n.Lock()
if n.startErr != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/erigon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func runErigon(cliCtx *cli.Context) error {
var logger log.Logger
var err error
var metricsMux *http.ServeMux
var pprofMux *http.ServeMux

if logger, metricsMux, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
if logger, metricsMux, pprofMux, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
return err
}

Expand All @@ -68,7 +69,7 @@ func runErigon(cliCtx *cli.Context) error {
return err
}

diagnostics.Setup(cliCtx, ethNode, metricsMux)
diagnostics.Setup(cliCtx, ethNode, metricsMux, pprofMux)

err = ethNode.Serve()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/silkworm_api/snapshot_idx.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func FindIf(segments []snaptype.FileInfo, predicate func(snaptype.FileInfo) bool
}

func buildIndex(cliCtx *cli.Context, dataDir string, snapshotPaths []string, minBlock uint64) error {
logger, _, err := debug.Setup(cliCtx, true /* rootLogger */)
logger, _, _, err := debug.Setup(cliCtx, true /* rootLogger */)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion diagnostics/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ var (
diagnosticsPortFlag = "diagnostics.endpoint.port"
metricsHTTPFlag = "metrics.addr"
metricsPortFlag = "metrics.port"
pprofPortFlag = "pprof.port"
pprofAddrFlag = "pprof.addr"
)

func Setup(ctx *cli.Context, node *node.ErigonNode, metricsMux *http.ServeMux) {
func Setup(ctx *cli.Context, node *node.ErigonNode, metricsMux *http.ServeMux, pprofMux *http.ServeMux) {
if ctx.Bool(diagnosticsDisabledFlag) {
return
}
Expand All @@ -34,9 +36,14 @@ func Setup(ctx *cli.Context, node *node.ErigonNode, metricsMux *http.ServeMux) {
metricsHost := ctx.String(metricsHTTPFlag)
metricsPort := ctx.Int(metricsPortFlag)
metricsAddress := fmt.Sprintf("%s:%d", metricsHost, metricsPort)
pprofHost := ctx.String(pprofAddrFlag)
pprofPort := ctx.Int(pprofPortFlag)
pprofAddress := fmt.Sprintf("%s:%d", pprofHost, pprofPort)

if diagAddress == metricsAddress {
diagMux = SetupDiagnosticsEndpoint(metricsMux, diagAddress)
} else if diagAddress == pprofAddress && pprofMux != nil {
diagMux = SetupDiagnosticsEndpoint(pprofMux, diagAddress)
} else {
diagMux = SetupDiagnosticsEndpoint(nil, diagAddress)
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/app/backup_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ CloudDrives (and ssd) have bad-latency and good-parallel-throughput - then havin
)

func doBackup(cliCtx *cli.Context) error {
logger, _, err := debug.Setup(cliCtx, true /* rootLogger */)
logger, _, _, err := debug.Setup(cliCtx, true /* rootLogger */)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/app/import_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func importChain(cliCtx *cli.Context) error {
if cliCtx.NArg() < 1 {
utils.Fatalf("This command requires an argument.")
}
logger, _, err := debug.Setup(cliCtx, true /* rootLogger */)
logger, _, _, err := debug.Setup(cliCtx, true /* rootLogger */)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion turbo/app/init_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ It expects the genesis file as argument.`,
func initGenesis(cliCtx *cli.Context) error {
var logger log.Logger
var err error
if logger, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
if logger, _, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
return err
}
// Make sure we have a valid genesis JSON
Expand Down
21 changes: 11 additions & 10 deletions turbo/app/snapshots_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var snapshotCommand = cli.Command{
Name: "snapshots",
Usage: `Managing snapshots (historical data partitions)`,
Before: func(context *cli.Context) error {
_, _, err := debug.Setup(context, true /* rootLogger */)
_, _, _, err := debug.Setup(context, true /* rootLogger */)
if err != nil {
return err
}
Expand Down Expand Up @@ -197,7 +197,7 @@ var (
)

func doIntegrity(cliCtx *cli.Context) error {
logger, _, err := debug.Setup(cliCtx, true /* root logger */)
logger, _, _, err := debug.Setup(cliCtx, true /* root logger */)
if err != nil {
return err
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func doDiff(cliCtx *cli.Context) error {
}

func doDecompressSpeed(cliCtx *cli.Context) error {
logger, _, err := debug.Setup(cliCtx, true /* rootLogger */)
logger, _, _, err := debug.Setup(cliCtx, true /* rootLogger */)
if err != nil {
return err
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func doDecompressSpeed(cliCtx *cli.Context) error {
func doRam(cliCtx *cli.Context) error {
var logger log.Logger
var err error
if logger, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
if logger, _, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
return err
}
defer logger.Info("Done")
Expand All @@ -334,7 +334,7 @@ func doRam(cliCtx *cli.Context) error {
}

func doIndicesCommand(cliCtx *cli.Context) error {
logger, _, err := debug.Setup(cliCtx, true /* rootLogger */)
logger, _, _, err := debug.Setup(cliCtx, true /* rootLogger */)
if err != nil {
return err
}
Expand Down Expand Up @@ -433,7 +433,7 @@ func openSnaps(ctx context.Context, cfg ethconfig.BlocksFreezing, dirs datadir.D
func doUncompress(cliCtx *cli.Context) error {
var logger log.Logger
var err error
if logger, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
if logger, _, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
return err
}
ctx := cliCtx.Context
Expand Down Expand Up @@ -486,7 +486,7 @@ func doUncompress(cliCtx *cli.Context) error {
func doCompress(cliCtx *cli.Context) error {
var err error
var logger log.Logger
if logger, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
if logger, _, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
return err
}
ctx := cliCtx.Context
Expand Down Expand Up @@ -536,7 +536,7 @@ func doCompress(cliCtx *cli.Context) error {
func doRetireCommand(cliCtx *cli.Context) error {
var logger log.Logger
var err error
if logger, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
if logger, _, _, err = debug.Setup(cliCtx, true /* rootLogger */); err != nil {
return err
}
defer logger.Info("Done")
Expand Down Expand Up @@ -685,8 +685,9 @@ func doUploaderCommand(cliCtx *cli.Context) error {
var logger log.Logger
var err error
var metricsMux *http.ServeMux
var pprofMux *http.ServeMux

if logger, metricsMux, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
if logger, metricsMux, pprofMux, err = debug.Setup(cliCtx, true /* root logger */); err != nil {
return err
}

Expand All @@ -709,7 +710,7 @@ func doUploaderCommand(cliCtx *cli.Context) error {
return err
}

diagnostics.Setup(cliCtx, ethNode, metricsMux)
diagnostics.Setup(cliCtx, ethNode, metricsMux, pprofMux)

err = ethNode.Serve()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion turbo/app/support_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var supportCommand = cli.Command{
Usage: "Connect Erigon instance to a diagnostics system for support",
ArgsUsage: "--diagnostics.addr <URL for the diagnostics system> --ids <diagnostic session ids allowed to connect> --metrics.urls <http://erigon_host:metrics_port>",
Before: func(cliCtx *cli.Context) error {
_, _, err := debug.Setup(cliCtx, true /* rootLogger */)
_, _, _, err := debug.Setup(cliCtx, true /* rootLogger */)
if err != nil {
return err
}
Expand Down
20 changes: 11 additions & 9 deletions turbo/debug/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ var (
Name: "metrics",
}
metricsAddrFlag = cli.StringFlag{
Name: "metrics.addr",
Name: "metrics.addr",
Usage: "Prometheus HTTP server listening interface",
Value: "0.0.0.0",
}
metricsPortFlag = cli.UintFlag{
Name: "metrics.port",
Expand Down Expand Up @@ -182,7 +184,7 @@ func SetupCobra(cmd *cobra.Command, filePrefix string) log.Logger {

// Setup initializes profiling and logging based on the CLI flags.
// It should be called as early as possible in the program.
func Setup(ctx *cli.Context, rootLogger bool) (log.Logger, *http.ServeMux, error) {
func Setup(ctx *cli.Context, rootLogger bool) (log.Logger, *http.ServeMux, *http.ServeMux, error) {
// ensure we've read in config file details before setting up metrics etc.
if err := SetFlagsFromConfigFile(ctx); err != nil {
log.Warn("failed setting config flags from yaml/toml file", "err", err)
Expand All @@ -194,13 +196,13 @@ func Setup(ctx *cli.Context, rootLogger bool) (log.Logger, *http.ServeMux, error

if traceFile := ctx.String(traceFlag.Name); traceFile != "" {
if err := Handler.StartGoTrace(traceFile); err != nil {
return logger, nil, err
return logger, nil, nil, err
}
}

if cpuFile := ctx.String(cpuprofileFlag.Name); cpuFile != "" {
if err := Handler.StartCPUProfile(cpuFile); err != nil {
return logger, nil, err
return logger, nil, nil, err
}
}
pprofEnabled := ctx.Bool(pprofFlag.Name)
Expand All @@ -210,25 +212,25 @@ func Setup(ctx *cli.Context, rootLogger bool) (log.Logger, *http.ServeMux, error
var metricsMux *http.ServeMux
var metricsAddress string

if metricsEnabled && (!pprofEnabled || metricsAddr != "") {
if metricsEnabled {
metricsPort := ctx.Int(metricsPortFlag.Name)
metricsAddress = fmt.Sprintf("%s:%d", metricsAddr, metricsPort)
metricsMux = metrics.Setup(metricsAddress, logger)
}

// pprof server
if pprofEnabled {
pprofHost := ctx.String(pprofAddrFlag.Name)
pprofPort := ctx.Int(pprofPortFlag.Name)
address := fmt.Sprintf("%s:%d", pprofHost, pprofPort)
if address == metricsAddress {
if (address == metricsAddress) && metricsEnabled {
metricsMux = StartPProf(address, metricsMux)
} else {
metricsMux = StartPProf(address, nil)
pprofMux := StartPProf(address, nil)
return logger, metricsMux, pprofMux, nil
}
}

return logger, metricsMux, nil
return logger, metricsMux, nil, nil
}

func StartPProf(address string, metricsMux *http.ServeMux) *http.ServeMux {
Expand Down

0 comments on commit 764706d

Please sign in to comment.