diff options
Diffstat (limited to 'metrics')
-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) |