Properly handle metric names with lables when printing *_count and *_sum values for Summary

This commit is contained in:
Aliaksandr Valialkin 2019-06-28 14:14:47 +03:00
parent 9ee7f68256
commit 2c308dd067
3 changed files with 15 additions and 2 deletions

2
go.mod
View File

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

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"math" "math"
"strings"
"sync" "sync"
"time" "time"
@ -107,11 +108,20 @@ func (sm *Summary) marshalTo(prefix string, w io.Writer) {
sm.mu.Unlock() sm.mu.Unlock()
if count > 0 { if count > 0 {
fmt.Fprintf(w, "%s_sum %g\n", prefix, sum) name, filters := splitMetricName(prefix)
fmt.Fprintf(w, "%s_count %d\n", prefix, count) fmt.Fprintf(w, "%s_sum%s %g\n", name, filters, sum)
fmt.Fprintf(w, "%s_count%s %d\n", name, filters, count)
} }
} }
func splitMetricName(name string) (string, string) {
n := strings.IndexByte(name, '{')
if n < 0 {
return name, ""
}
return name[:n], name[n:]
}
func (sm *Summary) updateQuantiles() { func (sm *Summary) updateQuantiles() {
sm.mu.Lock() sm.mu.Lock()
sm.quantileValues = sm.curr.Quantiles(sm.quantileValues[:0], sm.quantiles) sm.quantileValues = sm.curr.Quantiles(sm.quantileValues[:0], sm.quantiles)

View File

@ -30,6 +30,7 @@ func TestSummarySerial(t *testing.T) {
// Make sure the summary prints <prefix>_sum and <prefix>_count on marshalTo call // Make sure the summary prints <prefix>_sum and <prefix>_count on marshalTo call
testMarshalTo(t, s, "prefix", fmt.Sprintf("prefix_sum %g\nprefix_count %d\n", s.sum, s.count)) testMarshalTo(t, s, "prefix", fmt.Sprintf("prefix_sum %g\nprefix_count %d\n", s.sum, s.count))
testMarshalTo(t, s, `m{foo="bar"}`, fmt.Sprintf("m_sum{foo=\"bar\"} %g\nm_count{foo=\"bar\"} %d\n", s.sum, s.count))
// Verify s.quantileValues // Verify s.quantileValues
s.updateQuantiles() s.updateQuantiles()