From 6fc4c03c7942b54d404b02e6ea9806a0cea92109 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 28 Jun 2019 11:13:36 +0300 Subject: [PATCH] Update Summary quantiles before writing them to the output. Previously Summary quantiles were updated after writing them to the output, so the output contained old quantile values. --- go.mod | 2 ++ set.go | 14 +++++++++++--- summary.go | 4 ++-- summary_test.go | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 7a4629b..c933ff3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/VictoriaMetrics/metrics require github.com/valyala/histogram v1.0.1 + +go 1.13 diff --git a/set.go b/set.go index 293a46b..33a39a0 100644 --- a/set.go +++ b/set.go @@ -14,9 +14,10 @@ import ( // // Set.WritePrometheus must be called for exporting metrics from the set. type Set struct { - mu sync.Mutex - a []*namedMetric - m map[string]*namedMetric + mu sync.Mutex + a []*namedMetric + m map[string]*namedMetric + summaries []*Summary } // NewSet creates new set of metrics. @@ -32,6 +33,9 @@ func (s *Set) WritePrometheus(w io.Writer) { return s.a[i].name < s.a[j].name } s.mu.Lock() + for _, sm := range s.summaries { + sm.updateQuantiles() + } if !sort.SliceIsSorted(s.a, lessFunc) { sort.Slice(s.a, lessFunc) } @@ -200,6 +204,9 @@ func (s *Set) NewSummaryExt(name string, window time.Duration, quantiles []float s.registerMetric(name, sm) registerSummary(sm) s.registerSummaryQuantiles(name, sm) + s.mu.Lock() + s.summaries = append(s.summaries, sm) + s.mu.Unlock() return sm } @@ -258,6 +265,7 @@ func (s *Set) GetOrCreateSummaryExt(name string, window time.Duration, quantiles registerSummary(sm) mustRegisterQuantiles = true } + s.summaries = append(s.summaries, sm) s.mu.Unlock() if mustRegisterQuantiles { s.registerSummaryQuantiles(name, sm) diff --git a/summary.go b/summary.go index 7075aab..41454e4 100644 --- a/summary.go +++ b/summary.go @@ -93,9 +93,9 @@ func (sm *Summary) UpdateDuration(startTime time.Time) { } func (sm *Summary) marshalTo(prefix string, w io.Writer) { - // Just update sm.quantileValues and don't write anything to w. + // Do nothing. Quantile values should be already updated by the caller + // via sm.updateQuantiles() call. // sm.quantileValues will be marshaled later via quantileValue.marshalTo. - sm.updateQuantiles() } func (sm *Summary) updateQuantiles() { diff --git a/summary_test.go b/summary_test.go index ceaffa7..007a417 100644 --- a/summary_test.go +++ b/summary_test.go @@ -29,10 +29,10 @@ func TestSummarySerial(t *testing.T) { } // Make sure the summary doesn't print anything on marshalTo call - // and updates s.quantileValues. testMarshalTo(t, s, "prefix", "") // Verify s.quantileValues + s.updateQuantiles() if s.quantileValues[len(s.quantileValues)-1] != 1999 { t.Fatalf("unexpected quantileValues[last]; got %v; want %v", s.quantileValues[len(s.quantileValues)-1], 1999) }