From c9e77eb445416f2427a13d8d0091775f072f57bf Mon Sep 17 00:00:00 2001 From: Will Button Date: Wed, 31 May 2023 14:36:07 -0700 Subject: [PATCH 1/2] DEVOPS-829 format fix for prometheus metrics --- metrics/prometheus/collector.go | 27 +++++++++++++++++++++++---- metrics/prometheus/collector_test.go | 12 ++++++------ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/metrics/prometheus/collector.go b/metrics/prometheus/collector.go index 3959cbf5e1..feb4c758cd 100644 --- a/metrics/prometheus/collector.go +++ b/metrics/prometheus/collector.go @@ -30,6 +30,7 @@ var ( typeCounterTpl = "# TYPE %s counter\n" typeSummaryTpl = "# TYPE %s summary\n" keyValueTpl = "%s %v\n\n" + keyCounterTpl = "%s %v\n" keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n" ) @@ -61,11 +62,14 @@ func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64) { func (c *collector) addHistogram(name string, m metrics.Histogram) { pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999} ps := m.Percentiles(pv) - c.writeSummaryCounter(name, m.Count()) + var sum float64 = 0 c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name))) for i := range pv { c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i]) + sum += ps[i] } + c.writeSummarySum(name, fmt.Sprintf("%f", sum)) + c.writeSummaryCounter(name, len(ps)) c.buff.WriteRune('\n') } @@ -76,7 +80,7 @@ func (c *collector) addMeter(name string, m metrics.Meter) { func (c *collector) addTimer(name string, m metrics.Timer) { pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999} ps := m.Percentiles(pv) - c.writeSummaryCounter(name, m.Count()) + c.writeCounter(name, m.Count()) c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name))) for i := range pv { c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i]) @@ -90,11 +94,16 @@ func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) { } ps := m.Percentiles([]float64{50, 95, 99}) val := m.Values() - c.writeSummaryCounter(name, len(val)) c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name))) c.writeSummaryPercentile(name, "0.50", ps[0]) c.writeSummaryPercentile(name, "0.95", ps[1]) c.writeSummaryPercentile(name, "0.99", ps[2]) + var sum int64 = 0 + for _, v := range val { + sum += v + } + c.writeSummarySum(name, fmt.Sprintf("%d", sum)) + c.writeSummaryCounter(name, len(val)) c.buff.WriteRune('\n') } @@ -104,17 +113,27 @@ func (c *collector) writeGaugeCounter(name string, value interface{}) { c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) } -func (c *collector) writeSummaryCounter(name string, value interface{}) { +func (c *collector) writeCounter(name string, value interface{}) { name = mutateKey(name + "_count") c.buff.WriteString(fmt.Sprintf(typeCounterTpl, name)) c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value)) } +func (c *collector) writeSummaryCounter(name string, value interface{}) { + name = mutateKey(name + "_count") + c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name, value)) +} + func (c *collector) writeSummaryPercentile(name, p string, value interface{}) { name = mutateKey(name) c.buff.WriteString(fmt.Sprintf(keyQuantileTagValueTpl, name, p, value)) } +func (c *collector) writeSummarySum(name string, value string) { + name = mutateKey(name + "_sum") + c.buff.WriteString(fmt.Sprintf(keyCounterTpl, name, value)) +} + func mutateKey(key string) string { return strings.Replace(key, "/", "_", -1) } diff --git a/metrics/prometheus/collector_test.go b/metrics/prometheus/collector_test.go index 43f2f804d3..9fc784f8c2 100644 --- a/metrics/prometheus/collector_test.go +++ b/metrics/prometheus/collector_test.go @@ -67,9 +67,6 @@ test_gauge 23456 # TYPE test_gauge_float64 gauge test_gauge_float64 34567.89 -# TYPE test_histogram_count counter -test_histogram_count 0 - # TYPE test_histogram summary test_histogram {quantile="0.5"} 0 test_histogram {quantile="0.75"} 0 @@ -77,6 +74,8 @@ test_histogram {quantile="0.95"} 0 test_histogram {quantile="0.99"} 0 test_histogram {quantile="0.999"} 0 test_histogram {quantile="0.9999"} 0 +test_histogram_sum 0.000000 +test_histogram_count 6 # TYPE test_meter gauge test_meter 9999999 @@ -92,15 +91,16 @@ test_timer {quantile="0.99"} 1.2e+08 test_timer {quantile="0.999"} 1.2e+08 test_timer {quantile="0.9999"} 1.2e+08 -# TYPE test_resetting_timer_count counter -test_resetting_timer_count 6 - # TYPE test_resetting_timer summary test_resetting_timer {quantile="0.50"} 12000000 test_resetting_timer {quantile="0.95"} 120000000 test_resetting_timer {quantile="0.99"} 120000000 +test_resetting_timer_sum 180000000 +test_resetting_timer_count 6 ` + c.addResettingTimer("test/empty_resetting_timer", emptyResettingTimer) + exp := c.buff.String() if exp != expectedOutput { t.Log("Expected Output:\n", expectedOutput) From 9c1b90591f8947c4bb4fba56bec6a426b725e123 Mon Sep 17 00:00:00 2001 From: Will Button Date: Wed, 31 May 2023 15:37:35 -0700 Subject: [PATCH 2/2] DEVOPS-829 fix complaints from linter --- metrics/prometheus/collector.go | 5 +++++ metrics/prometheus/collector_test.go | 1 + 2 files changed, 6 insertions(+) diff --git a/metrics/prometheus/collector.go b/metrics/prometheus/collector.go index feb4c758cd..8187f4af02 100644 --- a/metrics/prometheus/collector.go +++ b/metrics/prometheus/collector.go @@ -62,12 +62,14 @@ func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64) { func (c *collector) addHistogram(name string, m metrics.Histogram) { pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999} ps := m.Percentiles(pv) + var sum float64 = 0 c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, mutateKey(name))) for i := range pv { c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i]) sum += ps[i] } + c.writeSummarySum(name, fmt.Sprintf("%f", sum)) c.writeSummaryCounter(name, len(ps)) c.buff.WriteRune('\n') @@ -98,10 +100,13 @@ func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) { c.writeSummaryPercentile(name, "0.50", ps[0]) c.writeSummaryPercentile(name, "0.95", ps[1]) c.writeSummaryPercentile(name, "0.99", ps[2]) + var sum int64 = 0 + for _, v := range val { sum += v } + c.writeSummarySum(name, fmt.Sprintf("%d", sum)) c.writeSummaryCounter(name, len(val)) c.buff.WriteRune('\n') diff --git a/metrics/prometheus/collector_test.go b/metrics/prometheus/collector_test.go index 9fc784f8c2..b1cc2aaf89 100644 --- a/metrics/prometheus/collector_test.go +++ b/metrics/prometheus/collector_test.go @@ -99,6 +99,7 @@ test_resetting_timer_sum 180000000 test_resetting_timer_count 6 ` + c.addResettingTimer("test/empty_resetting_timer", emptyResettingTimer) exp := c.buff.String()