Skip to content

Commit

Permalink
feat: restores cli flags to set addresses for debug and metrics servers
Browse files Browse the repository at this point in the history
* restores address flags
* renames `--debug-addr` to `--debug-listen-addr` to be consistent with config file setting `debug-listen-addr:`
* renames `--metrics-addr` to `--metrics-listen-addr` to be consistent with config file setting `metrics-listen-addr:`
* updates docs
  • Loading branch information
AntiTyping committed Sep 19, 2024
1 parent ec7bb41 commit 6cb53dc
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Additional information on how IBC works can be found [here](https://ibc.cosmos.n
$ rly start
```

>When running multiple instances of `rly start`, you will need to use the `--debug-addr` flag and provide an address:port. You can also pass an empty string `''` to turn off this feature or pass `localhost:0` to randomly select a port.
>When running multiple instances of `rly start`, you will need to use the `--debug-listen-addr` flag and provide an address:port. You can also pass an empty string `''` to turn off this feature or pass `localhost:0` to randomly select a port.

---
[[TROUBLESHOOTING](docs/troubleshooting.md)]
Expand Down
24 changes: 24 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const (
flagOrder = "order"
flagVersion = "version"
flagEnableDebugServer = "enable-debug-server"
flagDebugListenAddr = "debug-listen-addr"
flagEnableMetricsServer = "enable-metrics-server"
flagMetricsListenAddr = "metrics-listen-addr"
flagOverwriteConfig = "overwrite"
flagLimit = "limit"
flagHeight = "height"
Expand Down Expand Up @@ -419,6 +421,17 @@ func dstPortFlag(v *viper.Viper, cmd *cobra.Command) *cobra.Command {
}

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

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

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

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

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

cmd.Flags().Bool(
flagEnableMetricsServer,
false,
Expand Down
42 changes: 30 additions & 12 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,38 @@ $ %s start demo-path2 --max-tx-size 10`, appName, appName, appName, appName)),
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
metricsListenAddr := a.config.Global.MetricsListenPort

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

if metricsListenAddrFlag != "" {
metricsListenAddr = metricsListenAddrFlag
}

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

if flagEnableMetricsServer == false || metricsAddr == "" {
if flagEnableMetricsServer == false || metricsListenAddr == "" {
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)
ln, err := net.Listen("tcp", metricsListenAddr)
if err != nil {
a.log.Error(
"Failed to listen on metrics address. If you have another relayer process open, use --" +
metricsAddr +
metricsListenAddr +
" to pick a different address or port.",
)

return nil, fmt.Errorf("failed to listen on metrics address %q: %w", metricsAddr, err)
return nil, fmt.Errorf("failed to listen on metrics address %q: %w", metricsListenAddr, err)
}
log := a.log.With(zap.String("sys", "metricshttp"))
log.Info("Metrics server listening", zap.String("addr", metricsAddr))
log.Info("Metrics server listening", zap.String("addr", metricsListenAddr))
prometheusMetrics = processor.NewPrometheusMetrics()
relayermetrics.StartMetricsServer(cmd.Context(), log, ln, prometheusMetrics.Registry)
for _, chain := range chains {
Expand All @@ -210,29 +219,38 @@ func setupMetricsServer(cmd *cobra.Command, a *appState, err error, chains map[s
}

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

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

if debugListenAddrFlag != "" {
debugListenAddr = debugListenAddrFlag
}

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

if flagEnableDebugServer == false || debugAddr == "" {
if flagEnableDebugServer == false || debugListenAddr == "" {
a.log.Warn("Disabled debug server due to missing debug-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)
ln, err := net.Listen("tcp", debugListenAddr)
if err != nil {
a.log.Error(
"Failed to listen on debug address. If you have another relayer process open, use --" +
debugAddr +
debugListenAddr +
" to pick a different address or port.",
)

return fmt.Errorf("failed to listen on debug address %q: %w", debugAddr, err)
return fmt.Errorf("failed to listen on debug address %q: %w", debugListenAddr, err)
}
log := a.log.With(zap.String("sys", "debughttp"))
log.Info("Debug server listening", zap.String("addr", debugAddr))
log.Info("Debug server listening", zap.String("addr", debugListenAddr))
relaydebug.StartDebugServer(cmd.Context(), log, ln)
}
return nil
Expand Down
30 changes: 26 additions & 4 deletions cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ func TestMetricsServerFlag(t *testing.T) {

tests := []struct {
args []string
wantedPort int
wantedRunning bool
}{
{
[]string{"start"},
0,
false,
},
{
[]string{"start", "--enable-metrics-server"},
relayermetrics.MetricsServerPort,
true,
},
{
[]string{"start", "--enable-metrics-server", "--metrics-listen-addr", "127.0.0.1:7778"},
7778,
true,
},
}
Expand All @@ -43,9 +51,9 @@ func TestMetricsServerFlag(t *testing.T) {
sys.MustRunWithLogger(t, logger, tt.args...)

if tt.wantedRunning == true {
requireRunningMetricsServer(t, logs, relayermetrics.MetricsServerPort)
requireRunningMetricsServer(t, logs, tt.wantedPort)
} else {
requireDisabledMetricsServer(t, logs, relayermetrics.MetricsServerPort)
requireDisabledMetricsServer(t, logs, tt.wantedPort)
}
})
}
Expand All @@ -72,6 +80,12 @@ func TestMetricsServerConfig(t *testing.T) {
6184,
true,
},
{
[]string{"start", "--enable-metrics-server", "--metrics-listen-addr", "127.0.0.1:7184"},
"",
7184,
true,
},
}

for _, tt := range tests {
Expand All @@ -98,14 +112,22 @@ func TestDebugServerFlag(t *testing.T) {

tests := []struct {
args []string
wantedPort int
wantedRunning bool
}{
{
[]string{"start"},
0,
false,
},
{
[]string{"start", "--enable-debug-server"},
relaydebug.DebugServerPort,
true,
},
{
[]string{"start", "--enable-debug-server", "--debug-listen-addr", "127.0.0.1:7777"},
7777,
true,
},
}
Expand All @@ -118,9 +140,9 @@ func TestDebugServerFlag(t *testing.T) {
sys.MustRunWithLogger(t, logger, tt.args...)

if tt.wantedRunning == true {
requireRunningDebugServer(t, logs, relaydebug.DebugServerPort)
requireRunningDebugServer(t, logs, tt.wantedPort)
} else {
requireDisabledDebugServer(t, logs, relaydebug.DebugServerPort)
requireDisabledDebugServer(t, logs, tt.wantedPort)
}
})
}
Expand Down
6 changes: 4 additions & 2 deletions docs/advanced_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

**Prometheus exporter**

If you started `rly` with the default `--debug-addr` argument,
you can use `http://$IP:5183/relayer/metrics` as a target for your prometheus scraper.
If you started `rly` with `--enable-metrics-server` argument,
you can use `http://127.0.0.1:5184/relayer/metrics` as a target for your prometheus scraper.

You can use `--metrics-listen-addr $IP:7777` to customize the address and port where the metrics server.


Exported metrics:
Expand Down
6 changes: 3 additions & 3 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ $ rly query clients-expiration <PATH-NAME>

## Inspect Go runtime debug data

If you started `rly` with the default `--debug-addr` argument,
you can open `http://localhost:7597` in your browser to explore details from the Go runtime.
If you started `rly` with `--enable-debug-server` argument,
you can open `http://127.0.0.1:5183` in your browser to explore details from the Go runtime.

If you need active assistance from the Relayer development team regarding an unresponsive Relayer instance,
it will be helpful to provide the output from `http://localhost:7597/debug/pprof/goroutine?debug=2` at a minimum.
it will be helpful to provide the output from `http://127.0.0.1:7597/debug/pprof/goroutine?debug=2` at a minimum.

<br>

Expand Down
2 changes: 1 addition & 1 deletion interchaintest/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (r *Relayer) start(ctx context.Context, remainingArgs ...string) {
// Start the debug server on a random port.
// It won't be reachable without introspecting the output,
// but this will allow catching any possible data races around the debug server.
args := append([]string{"start", "--debug-addr", "localhost:0"}, remainingArgs...)
args := append([]string{"start", "--enable-debug-server", "--debug-listen-addr", "localhost:0", "--enable-metrics-server", "--metrics-listen-addr", "localhost:0"}, remainingArgs...)
res := r.Sys().RunC(ctx, r.log(), args...)
if res.Err != nil {
r.errCh <- res.Err
Expand Down

0 comments on commit 6cb53dc

Please sign in to comment.