From d4ff2cc87a8dd6c009438450fd111e969c433959 Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Fri, 3 May 2013 16:02:03 +0200 Subject: [PATCH] Fix race conditions in metric methods Methods which expect to use a mutex must be defined for the pointer value, because mutexes are not copyable. --- prometheus/counter.go | 4 ++-- prometheus/gauge.go | 4 ++-- prometheus/histogram.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/prometheus/counter.go b/prometheus/counter.go index 40f0532..dd07f60 100644 --- a/prometheus/counter.go +++ b/prometheus/counter.go @@ -73,7 +73,7 @@ func (metric *counter) ResetAll() { } } -func (metric counter) String() string { +func (metric *counter) String() string { formatString := "[Counter %s]" metric.mutex.RLock() @@ -132,7 +132,7 @@ func (metric *counter) Decrement(labels map[string]string) float64 { return metric.DecrementBy(labels, 1) } -func (metric counter) MarshalJSON() ([]byte, error) { +func (metric *counter) MarshalJSON() ([]byte, error) { metric.mutex.RLock() defer metric.mutex.RUnlock() diff --git a/prometheus/gauge.go b/prometheus/gauge.go index dd99976..f6aa265 100644 --- a/prometheus/gauge.go +++ b/prometheus/gauge.go @@ -37,7 +37,7 @@ type gauge struct { values map[string]*gaugeVector } -func (metric gauge) String() string { +func (metric *gauge) String() string { formatString := "[Gauge %s]" metric.mutex.RLock() @@ -80,7 +80,7 @@ func (metric *gauge) ResetAll() { } } -func (metric gauge) MarshalJSON() ([]byte, error) { +func (metric *gauge) MarshalJSON() ([]byte, error) { metric.mutex.RLock() defer metric.mutex.RUnlock() diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 9988ac2..7631c3a 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -122,7 +122,7 @@ func (h *histogram) Add(labels map[string]string, value float64) { histogram.buckets[lastIndex].Add(value) } -func (h histogram) String() string { +func (h *histogram) String() string { h.mutex.RLock() defer h.mutex.RUnlock() @@ -239,7 +239,7 @@ func formatFloat(value float64) string { return strconv.FormatFloat(value, floatFormat, floatPrecision, floatBitCount) } -func (h histogram) MarshalJSON() ([]byte, error) { +func (h *histogram) MarshalJSON() ([]byte, error) { h.mutex.RLock() defer h.mutex.RUnlock()