metrics/prometheus: minor typo cleanups, sorted report

This commit is contained in:
Péter Szilágyi 2019-04-11 12:31:08 +03:00
parent 55f22329fd
commit 88339c3759
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D
2 changed files with 25 additions and 16 deletions

View file

@ -26,11 +26,11 @@ import (
)
var (
typeGuageTpl = "# TYPE %s gauge\n"
typeGaugeTpl = "# TYPE %s gauge\n"
typeCounterTpl = "# TYPE %s counter\n"
typeSummaryTpl = "# TYPE %s summary\n"
keyValueTpl = "%s %v\n"
keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n"
keyValueTpl = "%s %v\n\n"
keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n\n"
)
// collector is a collection of byte buffers that aggregate Prometheus reports
@ -47,15 +47,15 @@ func newCollector() *collector {
}
func (c *collector) addCounter(name string, m metrics.Counter) {
c.writeGuageCounter(name, m.Count())
c.writeGaugeCounter(name, m.Count())
}
func (c *collector) addGuage(name string, m metrics.Gauge) {
c.writeGuageCounter(name, m.Value())
func (c *collector) addGauge(name string, m metrics.Gauge) {
c.writeGaugeCounter(name, m.Value())
}
func (c *collector) addGuageFloat64(name string, m metrics.GaugeFloat64) {
c.writeGuageCounter(name, m.Value())
func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64) {
c.writeGaugeCounter(name, m.Value())
}
func (c *collector) addHistogram(name string, m metrics.Histogram) {
@ -68,7 +68,7 @@ func (c *collector) addHistogram(name string, m metrics.Histogram) {
}
func (c *collector) addMeter(name string, m metrics.Meter) {
c.writeGuageCounter(name, m.Count())
c.writeGaugeCounter(name, m.Count())
}
func (c *collector) addTimer(name string, m metrics.Timer) {
@ -92,9 +92,9 @@ func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) {
c.writeSummaryPercentile(name, "0.99", ps[2])
}
func (c *collector) writeGuageCounter(name string, value interface{}) {
func (c *collector) writeGaugeCounter(name string, value interface{}) {
name = mutateKey(name)
c.buff.WriteString(fmt.Sprintf(typeGuageTpl, name))
c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, name))
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
}

View file

@ -20,6 +20,7 @@ package prometheus
import (
"fmt"
"net/http"
"sort"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
@ -28,17 +29,26 @@ import (
// Handler returns an HTTP handler which dump metrics in Prometheus format.
func Handler(reg metrics.Registry) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Gather and pre-sort the metrics to avoid random listings
var names []string
reg.Each(func(name string, i interface{}) {
names = append(names, name)
})
sort.Strings(names)
// Aggregate all the metris into a Prometheus collector
c := newCollector()
reg.Each(func(name string, i interface{}) {
for _, name := range names {
i := reg.Get(name)
switch m := i.(type) {
case metrics.Counter:
c.addCounter(name, m.Snapshot())
case metrics.Gauge:
c.addGuage(name, m.Snapshot())
c.addGauge(name, m.Snapshot())
case metrics.GaugeFloat64:
c.addGuageFloat64(name, m.Snapshot())
c.addGaugeFloat64(name, m.Snapshot())
case metrics.Histogram:
c.addHistogram(name, m.Snapshot())
case metrics.Meter:
@ -50,8 +60,7 @@ func Handler(reg metrics.Registry) http.Handler {
default:
log.Warn("Unknown Prometheus metric type", "type", fmt.Sprintf("%T", i))
}
})
}
w.Header().Add("Content-Type", "text/plain")
w.Header().Add("Content-Length", fmt.Sprint(c.buff.Len()))
w.Write(c.buff.Bytes())