Skip to content

Commit

Permalink
refactor: removes address flags for debug and metrics server
Browse files Browse the repository at this point in the history
* removes address flags so config file is the source of truth for addresses
* adds tests for flags to enable debug and metrics servers
* adds tests for config file settings for addresses for debug and metrics servers
* adjusts log messages
  • Loading branch information
AntiTyping committed Sep 19, 2024
1 parent ae03f4e commit 7c169a8
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 95 deletions.
24 changes: 0 additions & 24 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ const (
flagOrder = "order"
flagVersion = "version"
flagEnableDebugServer = "enable-debug-server"
flagDebugAddr = "debug-addr"
flagEnableMetricsServer = "enable-metrics-server"
flagMetricsAddr = "metrics-addr"
flagOverwriteConfig = "overwrite"
flagLimit = "limit"
flagHeight = "height"
Expand Down Expand Up @@ -421,17 +419,6 @@ func dstPortFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
}

func debugServerFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(
flagDebugAddr,
"",
"address to use for debug and metrics server. By default, "+
"will be the api-listen-addr parameter in the global config.",
)

if err := v.BindPFlag(flagDebugAddr, cmd.Flags().Lookup(flagDebugAddr)); err != nil {
panic(err)
}

cmd.Flags().Bool(
flagEnableDebugServer,
false,
Expand All @@ -446,17 +433,6 @@ func debugServerFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
}

func metricsServerFlags(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
cmd.Flags().String(
flagMetricsAddr,
"",
"address to use for metrics server. By default, "+
"will be the metrics-listen-addr parameter in the global config.",
)

if err := v.BindPFlag(flagMetricsAddr, cmd.Flags().Lookup(flagMetricsAddr)); err != nil {
panic(err)
}

cmd.Flags().Bool(
flagEnableMetricsServer,
false,
Expand Down
139 changes: 68 additions & 71 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,85 +93,16 @@ $ %s start demo-path2 --max-tx-size 10`, appName, appName, appName, appName)),
return err
}

debugAddr := a.config.Global.APIListenPort

// debug server
debugAddrFlag, err := cmd.Flags().GetString(flagDebugAddr)
err = setupDebugServer(cmd, a, err)
if err != nil {
return err
}

if debugAddrFlag != "" {
debugAddr = debugAddrFlag
}

flagEnableDebugServer, err := cmd.Flags().GetBool(flagEnableDebugServer)
prometheusMetrics, err := setupMetricsServer(cmd, a, err, chains)
if err != nil {
return err
}

if flagEnableDebugServer == false || debugAddr == "" {
a.log.Info("Skipping debug server due to empty debug address flag")
} else {
a.log.Warn("SECURITY WARNING! Debug server is enabled. It should only be used for non-production deployments.")
ln, err := net.Listen("tcp", debugAddr)
if err != nil {
a.log.Error(
"Failed to listen on debug address. If you have another relayer process open, use --" +
flagDebugAddr +
" to pick a different address.",
)

return fmt.Errorf("failed to listen on debug address %q: %w", debugAddr, err)
}
log := a.log.With(zap.String("sys", "debughttp"))
log.Info("Debug server listening", zap.String("addr", debugAddr))
relaydebug.StartDebugServer(cmd.Context(), log, ln)
}

// metrics server
var prometheusMetrics *processor.PrometheusMetrics

metricsAddr := a.config.Global.MetricsListenPort

metricsAddrFlag, err := cmd.Flags().GetString(flagMetricsAddr)
if err != nil {
return err
}

if metricsAddrFlag != "" {
metricsAddr = metricsAddrFlag
}

flagEnableMetricsServer, err := cmd.Flags().GetBool(flagEnableMetricsServer)
if err != nil {
return err
}

if flagEnableMetricsServer == false || metricsAddr == "" {
a.log.Info("Skipping metrics server due to empty metrics address flag")
} else {
ln, err := net.Listen("tcp", metricsAddr)
if err != nil {
a.log.Error(
"Failed to listen on metrics address. If you have another relayer process open, use --" +
flagMetricsAddr +
" to pick a different address.",
)

return fmt.Errorf("failed to listen on metrics address %q: %w", debugAddr, err)
}
log := a.log.With(zap.String("sys", "metricshttp"))
log.Info("Metrics server listening", zap.String("addr", metricsAddr))
prometheusMetrics = processor.NewPrometheusMetrics()
relayermetrics.StartMetricsServer(cmd.Context(), log, ln, prometheusMetrics.Registry)
for _, chain := range chains {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok {
ccp.SetMetrics(prometheusMetrics)
}
}
}

processorType, err := cmd.Flags().GetString(flagProcessor)
if err != nil {
return err
Expand Down Expand Up @@ -240,3 +171,69 @@ $ %s start demo-path2 --max-tx-size 10`, appName, appName, appName, appName)),
cmd = stuckPacketFlags(a.viper, cmd)
return cmd
}

func setupMetricsServer(cmd *cobra.Command, a *appState, err error, chains map[string]*relayer.Chain) (*processor.PrometheusMetrics, error) {
var prometheusMetrics *processor.PrometheusMetrics

metricsAddr := a.config.Global.MetricsListenPort

flagEnableMetricsServer, err := cmd.Flags().GetBool(flagEnableMetricsServer)
if err != nil {
return nil, err
}

if flagEnableMetricsServer == false || metricsAddr == "" {
a.log.Warn("Disabled metrics server due to missing metrics-listen-addr setting in config file.")
} else {
a.log.Info("Metrics server is enabled.")
ln, err := net.Listen("tcp", metricsAddr)
if err != nil {
a.log.Error(
"Failed to listen on metrics address. If you have another relayer process open, use --" +
metricsAddr +
" to pick a different address or port.",
)

return nil, fmt.Errorf("failed to listen on metrics address %q: %w", metricsAddr, err)
}
log := a.log.With(zap.String("sys", "metricshttp"))
log.Info("Metrics server listening", zap.String("addr", metricsAddr))
prometheusMetrics = processor.NewPrometheusMetrics()
relayermetrics.StartMetricsServer(cmd.Context(), log, ln, prometheusMetrics.Registry)
for _, chain := range chains {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok {
ccp.SetMetrics(prometheusMetrics)
}
}
}
return prometheusMetrics, nil
}

func setupDebugServer(cmd *cobra.Command, a *appState, err error) error {
debugAddr := a.config.Global.APIListenPort

flagEnableDebugServer, err := cmd.Flags().GetBool(flagEnableDebugServer)
if err != nil {
return err
}

if flagEnableDebugServer == false || debugAddr == "" {
a.log.Warn("Disabled debug server due to missing api-listen-addr setting in config file.")
} else {
a.log.Warn("SECURITY WARNING! Debug server is enabled. It should only be used with caution and proper security.")
ln, err := net.Listen("tcp", debugAddr)
if err != nil {
a.log.Error(
"Failed to listen on debug address. If you have another relayer process open, use --" +
debugAddr +
" to pick a different address or port.",
)

return fmt.Errorf("failed to listen on debug address %q: %w", debugAddr, err)
}
log := a.log.With(zap.String("sys", "debughttp"))
log.Info("Debug server listening", zap.String("addr", debugAddr))
relaydebug.StartDebugServer(cmd.Context(), log, ln)
}
return nil
}
Loading

0 comments on commit 7c169a8

Please sign in to comment.