From cc9f435303bcfce8382550697099859397a2bc0d Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Fri, 13 Nov 2020 22:30:32 +0000 Subject: [PATCH] [prometheus] add prometheus ip configuration flag Signed-off-by: Leo Chen --- api/service/prometheus/service.go | 2 +- cmd/harmony/config.go | 1 + cmd/harmony/config_test.go | 7 ++++--- cmd/harmony/default.go | 1 + cmd/harmony/flags.go | 11 +++++++++++ cmd/harmony/flags_test.go | 20 ++++++++++++++++++++ cmd/harmony/main.go | 2 +- 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/api/service/prometheus/service.go b/api/service/prometheus/service.go index 529509f50..65aae8502 100644 --- a/api/service/prometheus/service.go +++ b/api/service/prometheus/service.go @@ -49,7 +49,7 @@ func NewService(config nodeconfig.PrometheusServerConfig, additionalHandlers ... mux.HandleFunc(h.Path, h.Handler) } - utils.Logger().Info().Int("port", config.HTTPPort). + utils.Logger().Debug().Int("port", config.HTTPPort). Str("ip", config.HTTPIp). Msg("Starting Prometheus server") endpoint := fmt.Sprintf("%s:%d", config.HTTPIp, config.HTTPPort) diff --git a/cmd/harmony/config.go b/cmd/harmony/config.go index 298532d70..43a1d0073 100644 --- a/cmd/harmony/config.go +++ b/cmd/harmony/config.go @@ -112,6 +112,7 @@ type httpConfig struct { RosettaEnabled bool RosettaPort int PrometheusEnabled bool + PrometheusIP string PrometheusPort int } diff --git a/cmd/harmony/config_test.go b/cmd/harmony/config_test.go index 1e86b8624..a70f5a195 100644 --- a/cmd/harmony/config_test.go +++ b/cmd/harmony/config_test.go @@ -30,7 +30,7 @@ func init() { } func TestV1_0_0Config(t *testing.T) { - testConfig := `Version = "1.0.0" + testConfig := `Version = "1.0.3" [BLSKeys] KMSConfigFile = "" @@ -56,6 +56,7 @@ func TestV1_0_0Config(t *testing.T) { IP = "127.0.0.1" Port = 9500 PrometheusEnabled = true + PrometheusIP = "0.0.0.0" PrometheusPort = 9900 [Log] @@ -107,8 +108,8 @@ func TestV1_0_0Config(t *testing.T) { if config.P2P.IP != defaultConfig.P2P.IP { t.Errorf("Expect default p2p IP if old config is provided") } - if config.Version != "1.0.0" { - t.Errorf("Expected config version: 1.0.0, not %v", config.Version) + if config.Version != "1.0.3" { + t.Errorf("Expected config version: 1.0.3, not %v", config.Version) } config.Version = defaultConfig.Version // Shortcut for testing, value checked above if !reflect.DeepEqual(config, defaultConfig) { diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index 09d2545c9..a7b2838a5 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -31,6 +31,7 @@ var defaultConfig = harmonyConfig{ Port: nodeconfig.DefaultRPCPort, RosettaPort: nodeconfig.DefaultRosettaPort, PrometheusEnabled: true, + PrometheusIP: "0.0.0.0", PrometheusPort: nodeconfig.DefaultPrometheusPort, }, WS: wsConfig{ diff --git a/cmd/harmony/flags.go b/cmd/harmony/flags.go index 1001c8fc4..330ad6bae 100644 --- a/cmd/harmony/flags.go +++ b/cmd/harmony/flags.go @@ -52,6 +52,7 @@ var ( httpPortFlag, httpRosettaPortFlag, httpPrometheusEnabledFlag, + httpPrometheusIPFlag, httpPrometheusPortFlag, } @@ -452,6 +453,11 @@ var ( Usage: "enable HTTP / Prometheus requests", DefValue: defaultConfig.HTTP.PrometheusEnabled, } + httpPrometheusIPFlag = cli.StringFlag{ + Name: "http.prometheus.ip", + Usage: "ip address to listen for prometheus service", + DefValue: defaultConfig.HTTP.PrometheusIP, + } httpPrometheusPortFlag = cli.IntFlag{ Name: "http.prometheus.port", Usage: "prometheus port to listen for HTTP requests", @@ -477,6 +483,11 @@ func applyHTTPFlags(cmd *cobra.Command, config *harmonyConfig) { isRosettaSpecified = true } + if cli.IsFlagChanged(cmd, httpPrometheusIPFlag) { + config.HTTP.PrometheusIP = cli.GetStringFlagValue(cmd, httpPrometheusIPFlag) + isPrometheusSpecified = true + } + if cli.IsFlagChanged(cmd, httpPrometheusPortFlag) { config.HTTP.PrometheusPort = cli.GetIntFlagValue(cmd, httpPrometheusPortFlag) isPrometheusSpecified = true diff --git a/cmd/harmony/flags_test.go b/cmd/harmony/flags_test.go index 6e1559c4f..911f20bd4 100644 --- a/cmd/harmony/flags_test.go +++ b/cmd/harmony/flags_test.go @@ -61,6 +61,7 @@ func TestHarmonyFlags(t *testing.T) { RosettaEnabled: false, RosettaPort: 9700, PrometheusEnabled: true, + PrometheusIP: "0.0.0.0", PrometheusPort: 9900, }, WS: wsConfig{ @@ -361,6 +362,7 @@ func TestRPCFlags(t *testing.T) { Port: defaultConfig.HTTP.Port, RosettaPort: defaultConfig.HTTP.RosettaPort, PrometheusEnabled: true, + PrometheusIP: defaultConfig.HTTP.PrometheusIP, PrometheusPort: defaultConfig.HTTP.PrometheusPort, }, }, @@ -373,6 +375,7 @@ func TestRPCFlags(t *testing.T) { Port: 9001, RosettaPort: defaultConfig.HTTP.RosettaPort, PrometheusEnabled: true, + PrometheusIP: defaultConfig.HTTP.PrometheusIP, PrometheusPort: defaultConfig.HTTP.PrometheusPort, }, }, @@ -385,6 +388,7 @@ func TestRPCFlags(t *testing.T) { Port: 9001, RosettaPort: 10001, PrometheusEnabled: true, + PrometheusIP: defaultConfig.HTTP.PrometheusIP, PrometheusPort: defaultConfig.HTTP.PrometheusPort, }, }, @@ -397,6 +401,7 @@ func TestRPCFlags(t *testing.T) { Port: defaultConfig.HTTP.Port, RosettaPort: 10001, PrometheusEnabled: true, + PrometheusIP: defaultConfig.HTTP.PrometheusIP, PrometheusPort: defaultConfig.HTTP.PrometheusPort, }, }, @@ -409,6 +414,7 @@ func TestRPCFlags(t *testing.T) { Port: 9501, RosettaPort: 9701, PrometheusEnabled: true, + PrometheusIP: defaultConfig.HTTP.PrometheusIP, PrometheusPort: defaultConfig.HTTP.PrometheusPort + 1, }, }, @@ -421,6 +427,20 @@ func TestRPCFlags(t *testing.T) { Port: defaultConfig.HTTP.Port, RosettaPort: defaultConfig.HTTP.RosettaPort, PrometheusEnabled: true, + PrometheusIP: defaultConfig.HTTP.PrometheusIP, + PrometheusPort: 20001, + }, + }, + { + args: []string{"--http.prometheus.ip", "8.8.8.8", "--http.prometheus.port", "20001"}, + expConfig: httpConfig{ + Enabled: true, + RosettaEnabled: false, + IP: defaultConfig.HTTP.IP, + Port: defaultConfig.HTTP.Port, + RosettaPort: defaultConfig.HTTP.RosettaPort, + PrometheusEnabled: true, + PrometheusIP: "8.8.8.8", PrometheusPort: 20001, }, }, diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 7f93267ad..9f91cab4d 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -332,7 +332,7 @@ func setupNodeAndRun(hc harmonyConfig) { // Pares Prometheus config nodeConfig.PrometheusServer = nodeconfig.PrometheusServerConfig{ HTTPEnabled: hc.HTTP.PrometheusEnabled, - HTTPIp: hc.HTTP.IP, + HTTPIp: hc.HTTP.PrometheusIP, HTTPPort: hc.HTTP.PrometheusPort, }