diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-06-29 21:11:01 +0800 |
---|---|---|
committer | Jeffrey Wilcke <geffobscura@gmail.com> | 2015-06-30 06:51:46 +0800 |
commit | 01fe97211354d13ecaba8a52c82b808b7a7e8393 (patch) | |
tree | 337e23800833c81e1d21f5850d4079c575e89ea7 /metrics/metrics.go | |
parent | ccbb56b4f2cdba352eaa859ce3e34f999287f5c0 (diff) | |
download | dexon-01fe97211354d13ecaba8a52c82b808b7a7e8393.tar.gz dexon-01fe97211354d13ecaba8a52c82b808b7a7e8393.tar.zst dexon-01fe97211354d13ecaba8a52c82b808b7a7e8393.zip |
cmd, core, eth, metrics, p2p: require enabling metrics
Diffstat (limited to 'metrics/metrics.go')
-rw-r--r-- | metrics/metrics.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/metrics/metrics.go b/metrics/metrics.go index cb0caaae4..33004ee3b 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -2,7 +2,9 @@ package metrics import ( + "os" "runtime" + "strings" "time" "github.com/ethereum/go-ethereum/logger" @@ -10,9 +12,49 @@ import ( "github.com/rcrowley/go-metrics" ) +// MetricsEnabledFlag is the CLI flag name to use to enable metrics collections. +var MetricsEnabledFlag = "metrics" + +// enabled is the flag specifying if metrics are enable or not. +var enabled = false + +// Init enables or disables the metrics system. Since we need this to run before +// any other code gets to create meters and timers, we'll actually do an ugly hack +// and peek into the command line args for the metrics flag. +func init() { + for _, arg := range os.Args { + if strings.TrimLeft(arg, "-") == MetricsEnabledFlag { + glog.V(logger.Info).Infof("Enabling metrics collection") + enabled = true + } + } +} + +// NewMeter create a new metrics Meter, either a real one of a NOP stub depending +// on the metrics flag. +func NewMeter(name string) metrics.Meter { + if !enabled { + return new(metrics.NilMeter) + } + return metrics.GetOrRegisterMeter(name, metrics.DefaultRegistry) +} + +// NewTimer create a new metrics Timer, either a real one of a NOP stub depending +// on the metrics flag. +func NewTimer(name string) metrics.Timer { + if !enabled { + return new(metrics.NilTimer) + } + return metrics.GetOrRegisterTimer(name, metrics.DefaultRegistry) +} + // CollectProcessMetrics periodically collects various metrics about the running // process. func CollectProcessMetrics(refresh time.Duration) { + // Short circuit if the metrics system is disabled + if !enabled { + return + } // Create the various data collectors memstats := make([]*runtime.MemStats, 2) diskstats := make([]*DiskStats, 2) |