From 1bdff076ad903c184c33bcdb70caf4b9f8c26c9c Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Fri, 15 Jun 2018 17:21:04 +0400 Subject: [PATCH] add config option --- CHANGELOG.md | 5 +++++ config/config.go | 9 +++++++-- config/toml.go | 4 ++++ node/node.go | 25 +++++++++++++++++++++---- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5afc0221..f69127d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## TBD + +FEATURES: +- [node] added metrics (served under /metrics using a Prometheus client; disabled by default) + ## 0.20.1 BUG FIXES: diff --git a/config/config.go b/config/config.go index 5ba568f2..c6cb6549 100644 --- a/config/config.go +++ b/config/config.go @@ -142,6 +142,10 @@ type BaseConfig struct { // Database directory DBPath string `mapstructure:"db_dir"` + + // When true, metrics are served under `/metrics` using a Prometheus client + // Check out the documentation for the list of available metrics. + Monitoring bool `mapstructure:"monitoring"` } // DefaultBaseConfig returns a default base configuration for a Tendermint node @@ -159,6 +163,7 @@ func DefaultBaseConfig() BaseConfig { FilterPeers: false, DBBackend: "leveldb", DBPath: "data", + Monitoring: false, } } @@ -411,7 +416,7 @@ func (cfg *MempoolConfig) WalDir() string { //----------------------------------------------------------------------------- // ConsensusConfig -// ConsensusConfig defines the confuguration for the Tendermint consensus service, +// ConsensusConfig defines the configuration for the Tendermint consensus service, // including timeouts and details about the WAL and the block structure. type ConsensusConfig struct { RootDir string `mapstructure:"home"` @@ -536,7 +541,7 @@ func (cfg *ConsensusConfig) SetWalFile(walFile string) { //----------------------------------------------------------------------------- // TxIndexConfig -// TxIndexConfig defines the confuguration for the transaction +// TxIndexConfig defines the configuration for the transaction // indexer, including tags to index. type TxIndexConfig struct { // What indexer to use for transactions diff --git a/config/toml.go b/config/toml.go index 7ed3e971..8927f15d 100644 --- a/config/toml.go +++ b/config/toml.go @@ -107,6 +107,10 @@ prof_laddr = "{{ .BaseConfig.ProfListenAddress }}" # so the app can decide if we should keep the connection or not filter_peers = {{ .BaseConfig.FilterPeers }} +# When true, metrics are served under /metrics using a Prometheus client +# Check out the documentation for the list of available metrics. +monitoring = {{ .BaseConfig.Monitoring }} + ##### advanced configuration options ##### ##### rpc server configuration options ##### diff --git a/node/node.go b/node/node.go index acb3461b..00d6cc40 100644 --- a/node/node.go +++ b/node/node.go @@ -93,8 +93,8 @@ func DefaultNewNode(config *cfg.Config, logger log.Logger) (*Node, error) { // MetricsProvider returns a consensus and p2p Metrics. type MetricsProvider func() (*cs.Metrics, *p2p.Metrics, *mempl.Metrics) -// DefaultMetricsProvider returns a consensus and p2p Metrics build using -// Prometheus client library. +// DefaultMetricsProvider returns consensus, p2p and mempool Metrics build +// using Prometheus client library. func DefaultMetricsProvider() (*cs.Metrics, *p2p.Metrics, *mempl.Metrics) { return &cs.Metrics{ Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ @@ -176,6 +176,11 @@ func DefaultMetricsProvider() (*cs.Metrics, *p2p.Metrics, *mempl.Metrics) { } } +// NopMetricsProvider returns consensus, p2p and mempool Metrics as no-op. +func NopMetricsProvider() (*cs.Metrics, *p2p.Metrics, *mempl.Metrics) { + return cs.NopMetrics(), p2p.NopMetrics(), mempl.NopMetrics() +} + //------------------------------------------------------------------------------ // Node is the highest level interface to a full Tendermint node. @@ -300,7 +305,17 @@ func NewNode(config *cfg.Config, consensusLogger.Info("This node is not a validator", "addr", privValidator.GetAddress(), "pubKey", privValidator.GetPubKey()) } - csMetrics, p2pMetrics, memplMetrics := metricsProvider() + // metrics + var ( + csMetrics *cs.Metrics + p2pMetrics *p2p.Metrics + memplMetrics *mempl.Metrics + ) + if config.BaseConfig.Monitoring { + csMetrics, p2pMetrics, memplMetrics = metricsProvider() + } else { + csMetrics, p2pMetrics, memplMetrics = NopMetricsProvider() + } // Make MempoolReactor mempoolLogger := logger.With("module", "mempool") @@ -600,7 +615,9 @@ func (n *Node) startRPC() ([]net.Listener, error) { wm := rpcserver.NewWebsocketManager(rpccore.Routes, coreCodec, rpcserver.EventSubscriber(n.eventBus)) wm.SetLogger(rpcLogger.With("protocol", "websocket")) mux.HandleFunc("/websocket", wm.WebsocketHandler) - mux.Handle("/metrics", promhttp.Handler()) + if n.config.BaseConfig.Monitoring { + mux.Handle("/metrics", promhttp.Handler()) + } rpcserver.RegisterRPCFuncs(mux, rpccore.Routes, coreCodec, rpcLogger) listener, err := rpcserver.StartHTTPServer(listenAddr, mux, rpcLogger) if err != nil {