khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "os" |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 5 | "sync" |
| 6 | "sync/atomic" |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 7 | "time" |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 8 | |
| 9 | "github.com/hashicorp/go-immutable-radix" |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | // Config is used to configure metrics settings |
| 13 | type Config struct { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 14 | ServiceName string // Prefixed with keys to separate services |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 15 | HostName string // Hostname to use. If not provided and EnableHostname, it will be os.Hostname |
| 16 | EnableHostname bool // Enable prefixing gauge values with hostname |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 17 | EnableHostnameLabel bool // Enable adding hostname to labels |
| 18 | EnableServiceLabel bool // Enable adding service to labels |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 19 | EnableRuntimeMetrics bool // Enables profiling of runtime metrics (GC, Goroutines, Memory) |
| 20 | EnableTypePrefix bool // Prefixes key with a type ("counter", "gauge", "timer") |
| 21 | TimerGranularity time.Duration // Granularity of timers. |
| 22 | ProfileInterval time.Duration // Interval to profile runtime metrics |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 23 | |
| 24 | AllowedPrefixes []string // A list of metric prefixes to allow, with '.' as the separator |
| 25 | BlockedPrefixes []string // A list of metric prefixes to block, with '.' as the separator |
| 26 | AllowedLabels []string // A list of metric labels to allow, with '.' as the separator |
| 27 | BlockedLabels []string // A list of metric labels to block, with '.' as the separator |
| 28 | FilterDefault bool // Whether to allow metrics by default |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // Metrics represents an instance of a metrics sink that can |
| 32 | // be used to emit |
| 33 | type Metrics struct { |
| 34 | Config |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 35 | lastNumGC uint32 |
| 36 | sink MetricSink |
| 37 | filter *iradix.Tree |
| 38 | allowedLabels map[string]bool |
| 39 | blockedLabels map[string]bool |
| 40 | filterLock sync.RWMutex // Lock filters and allowedLabels/blockedLabels access |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Shared global metrics instance |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 44 | var globalMetrics atomic.Value // *Metrics |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 45 | |
| 46 | func init() { |
| 47 | // Initialize to a blackhole sink to avoid errors |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 48 | globalMetrics.Store(&Metrics{sink: &BlackholeSink{}}) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // DefaultConfig provides a sane default configuration |
| 52 | func DefaultConfig(serviceName string) *Config { |
| 53 | c := &Config{ |
| 54 | ServiceName: serviceName, // Use client provided service |
| 55 | HostName: "", |
| 56 | EnableHostname: true, // Enable hostname prefix |
| 57 | EnableRuntimeMetrics: true, // Enable runtime profiling |
| 58 | EnableTypePrefix: false, // Disable type prefix |
| 59 | TimerGranularity: time.Millisecond, // Timers are in milliseconds |
| 60 | ProfileInterval: time.Second, // Poll runtime every second |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 61 | FilterDefault: true, // Don't filter metrics by default |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Try to get the hostname |
| 65 | name, _ := os.Hostname() |
| 66 | c.HostName = name |
| 67 | return c |
| 68 | } |
| 69 | |
| 70 | // New is used to create a new instance of Metrics |
| 71 | func New(conf *Config, sink MetricSink) (*Metrics, error) { |
| 72 | met := &Metrics{} |
| 73 | met.Config = *conf |
| 74 | met.sink = sink |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 75 | met.UpdateFilterAndLabels(conf.AllowedPrefixes, conf.BlockedPrefixes, conf.AllowedLabels, conf.BlockedLabels) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 76 | |
| 77 | // Start the runtime collector |
| 78 | if conf.EnableRuntimeMetrics { |
| 79 | go met.collectStats() |
| 80 | } |
| 81 | return met, nil |
| 82 | } |
| 83 | |
| 84 | // NewGlobal is the same as New, but it assigns the metrics object to be |
| 85 | // used globally as well as returning it. |
| 86 | func NewGlobal(conf *Config, sink MetricSink) (*Metrics, error) { |
| 87 | metrics, err := New(conf, sink) |
| 88 | if err == nil { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 89 | globalMetrics.Store(metrics) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 90 | } |
| 91 | return metrics, err |
| 92 | } |
| 93 | |
| 94 | // Proxy all the methods to the globalMetrics instance |
| 95 | func SetGauge(key []string, val float32) { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 96 | globalMetrics.Load().(*Metrics).SetGauge(key, val) |
| 97 | } |
| 98 | |
| 99 | func SetGaugeWithLabels(key []string, val float32, labels []Label) { |
| 100 | globalMetrics.Load().(*Metrics).SetGaugeWithLabels(key, val, labels) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | func EmitKey(key []string, val float32) { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 104 | globalMetrics.Load().(*Metrics).EmitKey(key, val) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | func IncrCounter(key []string, val float32) { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 108 | globalMetrics.Load().(*Metrics).IncrCounter(key, val) |
| 109 | } |
| 110 | |
| 111 | func IncrCounterWithLabels(key []string, val float32, labels []Label) { |
| 112 | globalMetrics.Load().(*Metrics).IncrCounterWithLabels(key, val, labels) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func AddSample(key []string, val float32) { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 116 | globalMetrics.Load().(*Metrics).AddSample(key, val) |
| 117 | } |
| 118 | |
| 119 | func AddSampleWithLabels(key []string, val float32, labels []Label) { |
| 120 | globalMetrics.Load().(*Metrics).AddSampleWithLabels(key, val, labels) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func MeasureSince(key []string, start time.Time) { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 124 | globalMetrics.Load().(*Metrics).MeasureSince(key, start) |
| 125 | } |
| 126 | |
| 127 | func MeasureSinceWithLabels(key []string, start time.Time, labels []Label) { |
| 128 | globalMetrics.Load().(*Metrics).MeasureSinceWithLabels(key, start, labels) |
| 129 | } |
| 130 | |
| 131 | func UpdateFilter(allow, block []string) { |
| 132 | globalMetrics.Load().(*Metrics).UpdateFilter(allow, block) |
| 133 | } |
| 134 | |
| 135 | // UpdateFilterAndLabels set allow/block prefixes of metrics while allowedLabels |
| 136 | // and blockedLabels - when not nil - allow filtering of labels in order to |
| 137 | // block/allow globally labels (especially useful when having large number of |
| 138 | // values for a given label). See README.md for more information about usage. |
| 139 | func UpdateFilterAndLabels(allow, block, allowedLabels, blockedLabels []string) { |
| 140 | globalMetrics.Load().(*Metrics).UpdateFilterAndLabels(allow, block, allowedLabels, blockedLabels) |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 141 | } |