vendor: fix data race in github.com/armon/go-metrics/start.go

96 -> 42 data races
pull/3037/head
Frank Schroeder 8 years ago
parent 3caadb37ac
commit d5f87a20cf
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD

@ -2,6 +2,7 @@ package metrics
import (
"os"
"sync/atomic"
"time"
)
@ -25,11 +26,10 @@ type Metrics struct {
}
// Shared global metrics instance
var globalMetrics *Metrics
var global atomic.Value
func init() {
// Initialize to a blackhole sink to avoid errors
globalMetrics = &Metrics{sink: &BlackholeSink{}}
global.Store(&Metrics{sink: &BlackholeSink{}})
}
// DefaultConfig provides a sane default configuration
@ -68,28 +68,28 @@ func New(conf *Config, sink MetricSink) (*Metrics, error) {
func NewGlobal(conf *Config, sink MetricSink) (*Metrics, error) {
metrics, err := New(conf, sink)
if err == nil {
globalMetrics = metrics
global.Store(metrics)
}
return metrics, err
}
// Proxy all the methods to the globalMetrics instance
func SetGauge(key []string, val float32) {
globalMetrics.SetGauge(key, val)
global.Load().(*Metrics).SetGauge(key, val)
}
func EmitKey(key []string, val float32) {
globalMetrics.EmitKey(key, val)
global.Load().(*Metrics).EmitKey(key, val)
}
func IncrCounter(key []string, val float32) {
globalMetrics.IncrCounter(key, val)
global.Load().(*Metrics).IncrCounter(key, val)
}
func AddSample(key []string, val float32) {
globalMetrics.AddSample(key, val)
global.Load().(*Metrics).AddSample(key, val)
}
func MeasureSince(key []string, start time.Time) {
globalMetrics.MeasureSince(key, start)
global.Load().(*Metrics).MeasureSince(key, start)
}

Loading…
Cancel
Save