Merge pull request #881 from maticnetwork/DEVOPS-829/prometheus-metrics-format

DEVOPS-829 format fix for prometheus metrics
This commit is contained in:
Will Button 2023-06-01 07:42:01 -07:00 committed by GitHub
commit f1c8226839
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 10 deletions

View file

@ -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,16 @@ 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 +82,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 +96,19 @@ 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 +118,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)
}

View file

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