-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/relay: bind debug and monitoring address separately
Same principle as #2808. Refactor the binding functions to be usable across multiple commands.
- Loading branch information
Showing
5 changed files
with
157 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// bindDebugMonitoringFlags binds Prometheus monitoring and debug address CLI flags. | ||
func bindDebugMonitoringFlags(cmd *cobra.Command, monitorAddr, debugAddr *string, defaultMonitorAddr string) { | ||
cmd.Flags().StringVar(monitorAddr, "monitoring-address", defaultMonitorAddr, "Listening address (ip and port) for the monitoring API (prometheus).") | ||
cmd.Flags().StringVar(debugAddr, "debug-address", "", "Listening address (ip and port) for the pprof and QBFT debug API.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/obolnetwork/charon/app" | ||
) | ||
|
||
func genTestCmd(t *testing.T, f func(config app.Config)) *cobra.Command { | ||
t.Helper() | ||
|
||
var conf app.Config | ||
|
||
cmd := &cobra.Command{ | ||
Use: "test", | ||
Short: "test", | ||
} | ||
|
||
cmd.Run = func(cmd *cobra.Command, args []string) { | ||
f(conf) | ||
} | ||
|
||
bindDebugMonitoringFlags(cmd, &conf.MonitoringAddr, &conf.DebugAddr, "") | ||
|
||
return cmd | ||
} | ||
|
||
func Test_bindDebugMonitoringFlags(t *testing.T) { | ||
cmd := &cobra.Command{ | ||
Use: "testcmd", | ||
} | ||
|
||
t.Run("both present", func(t *testing.T) { | ||
var ( | ||
mAddr = "127.0.0.1:9999" | ||
dAddr = "127.0.0.1:8888" | ||
) | ||
|
||
cmd.ResetCommands() | ||
|
||
testCmd := genTestCmd(t, func(config app.Config) { | ||
require.Equal(t, mAddr, config.MonitoringAddr) | ||
require.Equal(t, dAddr, config.DebugAddr) | ||
}) | ||
|
||
cmd.AddCommand(testCmd) | ||
|
||
cmd.SetArgs([]string{ | ||
"test", | ||
"--monitoring-address", | ||
mAddr, | ||
"--debug-address", | ||
dAddr, | ||
}) | ||
|
||
require.NoError(t, cmd.Execute()) | ||
}) | ||
|
||
t.Run("only monitor", func(t *testing.T) { | ||
var ( | ||
mAddr = "127.0.0.1:9999" | ||
dAddr = "" | ||
) | ||
cmd.ResetCommands() | ||
|
||
testCmd := genTestCmd(t, func(config app.Config) { | ||
require.Equal(t, mAddr, config.MonitoringAddr) | ||
require.Equal(t, dAddr, config.DebugAddr) | ||
}) | ||
|
||
cmd.AddCommand(testCmd) | ||
|
||
cmd.SetArgs([]string{ | ||
"test", | ||
"--monitoring-address", | ||
mAddr, | ||
}) | ||
|
||
require.NoError(t, cmd.Execute()) | ||
}) | ||
|
||
t.Run("only debug", func(t *testing.T) { | ||
var ( | ||
mAddr = "" | ||
dAddr = "127.0.0.1:8888" | ||
) | ||
|
||
cmd.ResetCommands() | ||
|
||
testCmd := genTestCmd(t, func(config app.Config) { | ||
require.Equal(t, mAddr, config.MonitoringAddr) | ||
require.Equal(t, dAddr, config.DebugAddr) | ||
}) | ||
|
||
cmd.AddCommand(testCmd) | ||
|
||
cmd.SetArgs([]string{ | ||
"test", | ||
"--debug-address", | ||
dAddr, | ||
}) | ||
|
||
require.NoError(t, cmd.Execute()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters