From e0ba870ca7098ae60ef5850d620c845d681f2c56 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Wed, 18 Nov 2020 23:37:42 +0000 Subject: [PATCH] [prometheus] add EnablePush flag Signed-off-by: Leo Chen --- api/service/prometheus/service.go | 21 ++++++++++++++------- cmd/harmony/config.go | 9 +++++---- cmd/harmony/default.go | 18 ++++++++++-------- cmd/harmony/flags.go | 9 +++++++++ cmd/harmony/main.go | 1 + 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/api/service/prometheus/service.go b/api/service/prometheus/service.go index 3d970475a..c66d2dcd6 100644 --- a/api/service/prometheus/service.go +++ b/api/service/prometheus/service.go @@ -16,13 +16,14 @@ import ( // PrometheusConfig is the config for the prometheus service type PrometheusConfig struct { - Enabled bool - IP string - Port int - Gateway string // address of the pushgateway - Network string // network type, used as job prefix - Shard uint32 // shard id, used as job suffix - Instance string //identifier of the instance in prometheus metrics + Enabled bool + IP string + Port int + EnablePush bool // enable pushgateway support + Gateway string // address of the pushgateway + Network string // network type, used as job prefix + Shard uint32 // shard id, used as job suffix + Instance string //identifier of the instance in prometheus metrics } // Service provides Prometheus metrics via the /metrics route. This route will @@ -69,6 +70,10 @@ func NewService(additionalHandlers ...Handler) { // start pusher to push metrics to prometheus pushgateway // every minute go func(config PrometheusConfig) { + if !config.EnablePush { + utils.Logger().Info().Msg("Prometheus pushgateway support is disabled...") + return + } ticker := time.NewTicker(time.Minute) defer ticker.Stop() for { @@ -130,6 +135,7 @@ func SetConfig( enabled bool, ip string, port int, + enablepush bool, gateway string, network string, shard uint32, @@ -138,6 +144,7 @@ func SetConfig( config.Enabled = enabled config.IP = ip config.Port = port + config.EnablePush = enablepush config.Gateway = gateway config.Network = network config.Shard = shard diff --git a/cmd/harmony/config.go b/cmd/harmony/config.go index 5cd574562..11337c449 100644 --- a/cmd/harmony/config.go +++ b/cmd/harmony/config.go @@ -143,10 +143,11 @@ type legacyConfig struct { } type prometheusConfig struct { - Enabled bool - IP string - Port int - Gateway string + Enabled bool + IP string + Port int + EnablePush bool + Gateway string } // TODO: use specific type wise validation instead of general string types assertion. diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index a316bc5e8..cb446e4e7 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -66,10 +66,11 @@ var defaultConfig = harmonyConfig{ Verbosity: 3, }, Prometheus: &prometheusConfig{ - Enabled: true, - IP: "0.0.0.0", - Port: nodeconfig.DefaultPrometheusPort, - Gateway: "https://gateway.harmony.one", + Enabled: true, + IP: "0.0.0.0", + Port: nodeconfig.DefaultPrometheusPort, + Gateway: "https://gateway.harmony.one", + EnablePush: true, }, } @@ -100,10 +101,11 @@ var defaultConsensusConfig = consensusConfig{ } var defaultPrometheusConfig = prometheusConfig{ - Enabled: true, - IP: "0.0.0.0", - Port: 9900, - Gateway: "https://gateway.harmony.one", + Enabled: true, + IP: "0.0.0.0", + Port: 9900, + EnablePush: true, + Gateway: "https://gateway.harmony.one", } const ( diff --git a/cmd/harmony/flags.go b/cmd/harmony/flags.go index 5b17b957c..29ff1aa5b 100644 --- a/cmd/harmony/flags.go +++ b/cmd/harmony/flags.go @@ -171,6 +171,7 @@ var ( prometheusIPFlag, prometheusPortFlag, prometheusGatewayFlag, + prometheusEnablePushFlag, } ) @@ -1211,6 +1212,11 @@ var ( Usage: "prometheus pushgateway URL", DefValue: defaultConfig.Prometheus.Gateway, } + prometheusEnablePushFlag = cli.BoolFlag{ + Name: "prometheus.push", + Usage: "enable prometheus pushgateway", + DefValue: defaultConfig.Prometheus.EnablePush, + } ) func applyPrometheusFlags(cmd *cobra.Command, config *harmonyConfig) { @@ -1234,4 +1240,7 @@ func applyPrometheusFlags(cmd *cobra.Command, config *harmonyConfig) { if cli.IsFlagChanged(cmd, prometheusGatewayFlag) { config.Prometheus.Gateway = cli.GetStringFlagValue(cmd, prometheusGatewayFlag) } + if cli.IsFlagChanged(cmd, prometheusEnablePushFlag) { + config.Prometheus.EnablePush = cli.GetBoolFlagValue(cmd, prometheusEnablePushFlag) + } } diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 076847dc1..4c9a2f40e 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -377,6 +377,7 @@ func setupNodeAndRun(hc harmonyConfig) { hc.Prometheus.Enabled, hc.Prometheus.IP, hc.Prometheus.Port, + hc.Prometheus.EnablePush, hc.Prometheus.Gateway, hc.Network.NetworkType, nodeConfig.ShardID,