2018-02-23 09:56:08 +00:00
|
|
|
// Go port of Coda Hale's Metrics library
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2018-02-23 09:56:08 +00:00
|
|
|
// <https://github.com/rcrowley/go-metrics>
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2018-02-23 09:56:08 +00:00
|
|
|
// Coda Hale's original work: <https://github.com/codahale/metrics>
|
2015-06-27 15:12:58 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2015-06-29 13:11:01 +00:00
|
|
|
"os"
|
2021-07-03 07:44:23 +00:00
|
|
|
"runtime/metrics"
|
2015-06-29 13:11:01 +00:00
|
|
|
"strings"
|
2021-03-09 06:34:13 +00:00
|
|
|
"sync/atomic"
|
2015-06-27 15:12:58 +00:00
|
|
|
)
|
|
|
|
|
2018-02-23 09:56:08 +00:00
|
|
|
// Enabled is checked by the constructor functions for all of the
|
2019-03-25 08:01:18 +00:00
|
|
|
// standard metrics. If it is true, the metric returned is a stub.
|
2018-02-23 09:56:08 +00:00
|
|
|
//
|
|
|
|
// This global kill-switch helps quantify the observer effect and makes
|
|
|
|
// for less cluttered pprof profiles.
|
2019-02-18 11:37:31 +00:00
|
|
|
var Enabled = false
|
2018-02-23 09:56:08 +00:00
|
|
|
|
2021-03-09 06:34:13 +00:00
|
|
|
// callbacks - storing list of callbacks as type []func()
|
|
|
|
// use metrics.AddCallback to add your function to metrics collection loop (to avoid multiple goroutines collecting metrics)
|
|
|
|
var callbacks atomic.Value
|
|
|
|
|
|
|
|
func init() {
|
2021-07-03 07:44:23 +00:00
|
|
|
metrics.All()
|
2021-03-09 06:34:13 +00:00
|
|
|
callbacks.Store([]func(){})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling Load method
|
|
|
|
|
2019-03-25 08:01:18 +00:00
|
|
|
// EnabledExpensive is a soft-flag meant for external packages to check if costly
|
|
|
|
// metrics gathering is allowed or not. The goal is to separate standard metrics
|
|
|
|
// for health monitoring and debug metrics that might impact runtime performance.
|
|
|
|
var EnabledExpensive = false
|
|
|
|
|
|
|
|
// enablerFlags is the CLI flag names to use to enable metrics collections.
|
2019-11-14 09:04:16 +00:00
|
|
|
var enablerFlags = []string{"metrics"}
|
2019-03-25 08:01:18 +00:00
|
|
|
|
|
|
|
// expensiveEnablerFlags is the CLI flag names to use to enable metrics collections.
|
|
|
|
var expensiveEnablerFlags = []string{"metrics.expensive"}
|
2015-06-29 13:11:01 +00:00
|
|
|
|
|
|
|
// 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 {
|
2019-03-25 08:01:18 +00:00
|
|
|
flag := strings.TrimLeft(arg, "-")
|
|
|
|
|
|
|
|
for _, enabler := range enablerFlags {
|
|
|
|
if !Enabled && flag == enabler {
|
|
|
|
Enabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, enabler := range expensiveEnablerFlags {
|
2019-03-25 08:40:46 +00:00
|
|
|
if !EnabledExpensive && flag == enabler {
|
2019-03-25 08:01:18 +00:00
|
|
|
EnabledExpensive = true
|
|
|
|
}
|
2015-06-29 13:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|