fix data race when accessing h.sum during histogram marshaling

This commit is contained in:
Aliaksandr Valialkin 2020-03-13 12:36:02 +02:00
parent 93f7956e2a
commit f6ff0f32a2

View File

@ -246,14 +246,22 @@ func (h *Histogram) marshalTo(prefix string, w io.Writer) {
return return
} }
name, filters := splitMetricName(prefix) name, filters := splitMetricName(prefix)
if float64(int64(h.sum)) == h.sum { sum := h.getSum()
fmt.Fprintf(w, "%s_sum%s %d\n", name, filters, int64(h.sum)) if float64(int64(sum)) == sum {
fmt.Fprintf(w, "%s_sum%s %d\n", name, filters, int64(sum))
} else { } else {
fmt.Fprintf(w, "%s_sum%s %g\n", name, filters, h.sum) fmt.Fprintf(w, "%s_sum%s %g\n", name, filters, sum)
} }
fmt.Fprintf(w, "%s_count%s %d\n", name, filters, countTotal) fmt.Fprintf(w, "%s_count%s %d\n", name, filters, countTotal)
} }
func (h *Histogram) getSum() float64 {
h.mu.Lock()
sum := h.sum
h.mu.Unlock()
return sum
}
func getBucketIdxAndOffset(v float64) (int, uint) { func getBucketIdxAndOffset(v float64) (int, uint) {
if v < 0 { if v < 0 {
panic(fmt.Errorf("BUG: v must be positive; got %g", v)) panic(fmt.Errorf("BUG: v must be positive; got %g", v))