From 2c308dd067092d737d1e1fe4002e522e704ca617 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 28 Jun 2019 14:14:47 +0300 Subject: [PATCH] Properly handle metric names with lables when printing *_count and *_sum values for Summary --- go.mod | 2 ++ summary.go | 14 ++++++++++++-- summary_test.go | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 7a4629b..15e3dcc 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.12 diff --git a/summary.go b/summary.go index 3bb002d..def1f4e 100644 --- a/summary.go +++ b/summary.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "math" + "strings" "sync" "time" @@ -107,11 +108,20 @@ func (sm *Summary) marshalTo(prefix string, w io.Writer) { sm.mu.Unlock() if count > 0 { - fmt.Fprintf(w, "%s_sum %g\n", prefix, sum) - fmt.Fprintf(w, "%s_count %d\n", prefix, count) + name, filters := splitMetricName(prefix) + 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() { sm.mu.Lock() sm.quantileValues = sm.curr.Quantiles(sm.quantileValues[:0], sm.quantiles) diff --git a/summary_test.go b/summary_test.go index d8f45ca..9182a65 100644 --- a/summary_test.go +++ b/summary_test.go @@ -30,6 +30,7 @@ func TestSummarySerial(t *testing.T) { // Make sure the summary prints _sum and _count on marshalTo call 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 s.updateQuantiles()