• Matthew Slipper's avatar
    op-batcher op-proposer op-service: Introduce op-service (#3202) · 8ed8d4ec
    Matthew Slipper authored
    * op-batcher op-proposer op-service: Introduce op-service
    
    op-service is a utilities library that automates much of the boilerplate setup we've been duplicating across different daemons. This PR adds the following to op-batcher:
    
    - Common setup utilities for logging, metrics, pprof, and RPC.
    - For each of the above functions, a set of CLI flags and configs that can be used to parse + validate CLI configuration for that module.
    - A base RPC server that implementers can extend.
    - HTTP metrics utilities.
    
    * update dockerfiles
    
    * code review updates
    
    * op-service: temporary replace, until op-service go module is canonical and can be resolved
    Co-authored-by: default avatarprotolambda <proto@protolambda.com>
    8ed8d4ec
cli.go 1.25 KB
package pprof

import (
	"errors"
	"math"

	opservice "github.com/ethereum-optimism/optimism/op-service"
	"github.com/urfave/cli"
)

const (
	EnabledFlagName    = "pprof.enabled"
	ListenAddrFlagName = "pprof.addr"
	PortFlagName       = "pprof.port"
)

func CLIFlags(envPrefix string) []cli.Flag {
	return []cli.Flag{
		cli.BoolFlag{
			Name:   EnabledFlagName,
			Usage:  "Enable the pprof server",
			EnvVar: opservice.PrefixEnvVar(envPrefix, "PPROF_ENABLED"),
		},
		cli.StringFlag{
			Name:   ListenAddrFlagName,
			Usage:  "pprof listening address",
			Value:  "0.0.0.0",
			EnvVar: opservice.PrefixEnvVar(envPrefix, "PPROF_ADDR"),
		},
		cli.IntFlag{
			Name:   PortFlagName,
			Usage:  "pprof listening port",
			Value:  6060,
			EnvVar: opservice.PrefixEnvVar(envPrefix, "PPROF_PORT"),
		},
	}
}

type CLIConfig struct {
	Enabled    bool
	ListenAddr string
	ListenPort int
}

func (m CLIConfig) Check() error {
	if !m.Enabled {
		return nil
	}

	if m.ListenPort < 0 || m.ListenPort > math.MaxUint16 {
		return errors.New("invalid pprof port")
	}

	return nil
}

func ReadCLIConfig(ctx *cli.Context) CLIConfig {
	return CLIConfig{
		Enabled:    ctx.GlobalBool(EnabledFlagName),
		ListenAddr: ctx.GlobalString(ListenAddrFlagName),
		ListenPort: ctx.GlobalInt(PortFlagName),
	}
}