[prometheus] add prometheus ip configuration flag

Signed-off-by: Leo Chen <leo@harmony.one>
pull/3454/head
Leo Chen 4 years ago
parent d84bb532cb
commit cc9f435303
  1. 2
      api/service/prometheus/service.go
  2. 1
      cmd/harmony/config.go
  3. 7
      cmd/harmony/config_test.go
  4. 1
      cmd/harmony/default.go
  5. 11
      cmd/harmony/flags.go
  6. 20
      cmd/harmony/flags_test.go
  7. 2
      cmd/harmony/main.go

@ -49,7 +49,7 @@ func NewService(config nodeconfig.PrometheusServerConfig, additionalHandlers ...
mux.HandleFunc(h.Path, h.Handler) mux.HandleFunc(h.Path, h.Handler)
} }
utils.Logger().Info().Int("port", config.HTTPPort). utils.Logger().Debug().Int("port", config.HTTPPort).
Str("ip", config.HTTPIp). Str("ip", config.HTTPIp).
Msg("Starting Prometheus server") Msg("Starting Prometheus server")
endpoint := fmt.Sprintf("%s:%d", config.HTTPIp, config.HTTPPort) endpoint := fmt.Sprintf("%s:%d", config.HTTPIp, config.HTTPPort)

@ -112,6 +112,7 @@ type httpConfig struct {
RosettaEnabled bool RosettaEnabled bool
RosettaPort int RosettaPort int
PrometheusEnabled bool PrometheusEnabled bool
PrometheusIP string
PrometheusPort int PrometheusPort int
} }

@ -30,7 +30,7 @@ func init() {
} }
func TestV1_0_0Config(t *testing.T) { func TestV1_0_0Config(t *testing.T) {
testConfig := `Version = "1.0.0" testConfig := `Version = "1.0.3"
[BLSKeys] [BLSKeys]
KMSConfigFile = "" KMSConfigFile = ""
@ -56,6 +56,7 @@ func TestV1_0_0Config(t *testing.T) {
IP = "127.0.0.1" IP = "127.0.0.1"
Port = 9500 Port = 9500
PrometheusEnabled = true PrometheusEnabled = true
PrometheusIP = "0.0.0.0"
PrometheusPort = 9900 PrometheusPort = 9900
[Log] [Log]
@ -107,8 +108,8 @@ func TestV1_0_0Config(t *testing.T) {
if config.P2P.IP != defaultConfig.P2P.IP { if config.P2P.IP != defaultConfig.P2P.IP {
t.Errorf("Expect default p2p IP if old config is provided") t.Errorf("Expect default p2p IP if old config is provided")
} }
if config.Version != "1.0.0" { if config.Version != "1.0.3" {
t.Errorf("Expected config version: 1.0.0, not %v", config.Version) t.Errorf("Expected config version: 1.0.3, not %v", config.Version)
} }
config.Version = defaultConfig.Version // Shortcut for testing, value checked above config.Version = defaultConfig.Version // Shortcut for testing, value checked above
if !reflect.DeepEqual(config, defaultConfig) { if !reflect.DeepEqual(config, defaultConfig) {

@ -31,6 +31,7 @@ var defaultConfig = harmonyConfig{
Port: nodeconfig.DefaultRPCPort, Port: nodeconfig.DefaultRPCPort,
RosettaPort: nodeconfig.DefaultRosettaPort, RosettaPort: nodeconfig.DefaultRosettaPort,
PrometheusEnabled: true, PrometheusEnabled: true,
PrometheusIP: "0.0.0.0",
PrometheusPort: nodeconfig.DefaultPrometheusPort, PrometheusPort: nodeconfig.DefaultPrometheusPort,
}, },
WS: wsConfig{ WS: wsConfig{

@ -52,6 +52,7 @@ var (
httpPortFlag, httpPortFlag,
httpRosettaPortFlag, httpRosettaPortFlag,
httpPrometheusEnabledFlag, httpPrometheusEnabledFlag,
httpPrometheusIPFlag,
httpPrometheusPortFlag, httpPrometheusPortFlag,
} }
@ -452,6 +453,11 @@ var (
Usage: "enable HTTP / Prometheus requests", Usage: "enable HTTP / Prometheus requests",
DefValue: defaultConfig.HTTP.PrometheusEnabled, 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{ httpPrometheusPortFlag = cli.IntFlag{
Name: "http.prometheus.port", Name: "http.prometheus.port",
Usage: "prometheus port to listen for HTTP requests", Usage: "prometheus port to listen for HTTP requests",
@ -477,6 +483,11 @@ func applyHTTPFlags(cmd *cobra.Command, config *harmonyConfig) {
isRosettaSpecified = true isRosettaSpecified = true
} }
if cli.IsFlagChanged(cmd, httpPrometheusIPFlag) {
config.HTTP.PrometheusIP = cli.GetStringFlagValue(cmd, httpPrometheusIPFlag)
isPrometheusSpecified = true
}
if cli.IsFlagChanged(cmd, httpPrometheusPortFlag) { if cli.IsFlagChanged(cmd, httpPrometheusPortFlag) {
config.HTTP.PrometheusPort = cli.GetIntFlagValue(cmd, httpPrometheusPortFlag) config.HTTP.PrometheusPort = cli.GetIntFlagValue(cmd, httpPrometheusPortFlag)
isPrometheusSpecified = true isPrometheusSpecified = true

@ -61,6 +61,7 @@ func TestHarmonyFlags(t *testing.T) {
RosettaEnabled: false, RosettaEnabled: false,
RosettaPort: 9700, RosettaPort: 9700,
PrometheusEnabled: true, PrometheusEnabled: true,
PrometheusIP: "0.0.0.0",
PrometheusPort: 9900, PrometheusPort: 9900,
}, },
WS: wsConfig{ WS: wsConfig{
@ -361,6 +362,7 @@ func TestRPCFlags(t *testing.T) {
Port: defaultConfig.HTTP.Port, Port: defaultConfig.HTTP.Port,
RosettaPort: defaultConfig.HTTP.RosettaPort, RosettaPort: defaultConfig.HTTP.RosettaPort,
PrometheusEnabled: true, PrometheusEnabled: true,
PrometheusIP: defaultConfig.HTTP.PrometheusIP,
PrometheusPort: defaultConfig.HTTP.PrometheusPort, PrometheusPort: defaultConfig.HTTP.PrometheusPort,
}, },
}, },
@ -373,6 +375,7 @@ func TestRPCFlags(t *testing.T) {
Port: 9001, Port: 9001,
RosettaPort: defaultConfig.HTTP.RosettaPort, RosettaPort: defaultConfig.HTTP.RosettaPort,
PrometheusEnabled: true, PrometheusEnabled: true,
PrometheusIP: defaultConfig.HTTP.PrometheusIP,
PrometheusPort: defaultConfig.HTTP.PrometheusPort, PrometheusPort: defaultConfig.HTTP.PrometheusPort,
}, },
}, },
@ -385,6 +388,7 @@ func TestRPCFlags(t *testing.T) {
Port: 9001, Port: 9001,
RosettaPort: 10001, RosettaPort: 10001,
PrometheusEnabled: true, PrometheusEnabled: true,
PrometheusIP: defaultConfig.HTTP.PrometheusIP,
PrometheusPort: defaultConfig.HTTP.PrometheusPort, PrometheusPort: defaultConfig.HTTP.PrometheusPort,
}, },
}, },
@ -397,6 +401,7 @@ func TestRPCFlags(t *testing.T) {
Port: defaultConfig.HTTP.Port, Port: defaultConfig.HTTP.Port,
RosettaPort: 10001, RosettaPort: 10001,
PrometheusEnabled: true, PrometheusEnabled: true,
PrometheusIP: defaultConfig.HTTP.PrometheusIP,
PrometheusPort: defaultConfig.HTTP.PrometheusPort, PrometheusPort: defaultConfig.HTTP.PrometheusPort,
}, },
}, },
@ -409,6 +414,7 @@ func TestRPCFlags(t *testing.T) {
Port: 9501, Port: 9501,
RosettaPort: 9701, RosettaPort: 9701,
PrometheusEnabled: true, PrometheusEnabled: true,
PrometheusIP: defaultConfig.HTTP.PrometheusIP,
PrometheusPort: defaultConfig.HTTP.PrometheusPort + 1, PrometheusPort: defaultConfig.HTTP.PrometheusPort + 1,
}, },
}, },
@ -421,6 +427,20 @@ func TestRPCFlags(t *testing.T) {
Port: defaultConfig.HTTP.Port, Port: defaultConfig.HTTP.Port,
RosettaPort: defaultConfig.HTTP.RosettaPort, RosettaPort: defaultConfig.HTTP.RosettaPort,
PrometheusEnabled: true, 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, PrometheusPort: 20001,
}, },
}, },

@ -332,7 +332,7 @@ func setupNodeAndRun(hc harmonyConfig) {
// Pares Prometheus config // Pares Prometheus config
nodeConfig.PrometheusServer = nodeconfig.PrometheusServerConfig{ nodeConfig.PrometheusServer = nodeconfig.PrometheusServerConfig{
HTTPEnabled: hc.HTTP.PrometheusEnabled, HTTPEnabled: hc.HTTP.PrometheusEnabled,
HTTPIp: hc.HTTP.IP, HTTPIp: hc.HTTP.PrometheusIP,
HTTPPort: hc.HTTP.PrometheusPort, HTTPPort: hc.HTTP.PrometheusPort,
} }

Loading…
Cancel
Save