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.
This commit is contained in:
Aliaksandr Valialkin 2019-06-28 11:13:36 +03:00
parent 2280bf270b
commit 6fc4c03c79
4 changed files with 16 additions and 6 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/VictoriaMetrics/metrics
require github.com/valyala/histogram v1.0.1
go 1.13

14
set.go
View File

@ -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)

View File

@ -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() {

View File

@ -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)
}