mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
metrics/prometheus: minor typo cleanups, sorted report
This commit is contained in:
parent
55f22329fd
commit
88339c3759
2 changed files with 25 additions and 16 deletions
|
|
@ -26,11 +26,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
typeGuageTpl = "# TYPE %s gauge\n"
|
typeGaugeTpl = "# TYPE %s gauge\n"
|
||||||
typeCounterTpl = "# TYPE %s counter\n"
|
typeCounterTpl = "# TYPE %s counter\n"
|
||||||
typeSummaryTpl = "# TYPE %s summary\n"
|
typeSummaryTpl = "# TYPE %s summary\n"
|
||||||
keyValueTpl = "%s %v\n"
|
keyValueTpl = "%s %v\n\n"
|
||||||
keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n"
|
keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
// collector is a collection of byte buffers that aggregate Prometheus reports
|
// 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) {
|
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) {
|
func (c *collector) addGauge(name string, m metrics.Gauge) {
|
||||||
c.writeGuageCounter(name, m.Value())
|
c.writeGaugeCounter(name, m.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *collector) addGuageFloat64(name string, m metrics.GaugeFloat64) {
|
func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64) {
|
||||||
c.writeGuageCounter(name, m.Value())
|
c.writeGaugeCounter(name, m.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *collector) addHistogram(name string, m metrics.Histogram) {
|
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) {
|
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) {
|
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])
|
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)
|
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))
|
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ package prometheus
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
|
|
@ -28,17 +29,26 @@ import (
|
||||||
// Handler returns an HTTP handler which dump metrics in Prometheus format.
|
// Handler returns an HTTP handler which dump metrics in Prometheus format.
|
||||||
func Handler(reg metrics.Registry) http.Handler {
|
func Handler(reg metrics.Registry) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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
|
// Aggregate all the metris into a Prometheus collector
|
||||||
c := newCollector()
|
c := newCollector()
|
||||||
|
|
||||||
reg.Each(func(name string, i interface{}) {
|
for _, name := range names {
|
||||||
|
i := reg.Get(name)
|
||||||
|
|
||||||
switch m := i.(type) {
|
switch m := i.(type) {
|
||||||
case metrics.Counter:
|
case metrics.Counter:
|
||||||
c.addCounter(name, m.Snapshot())
|
c.addCounter(name, m.Snapshot())
|
||||||
case metrics.Gauge:
|
case metrics.Gauge:
|
||||||
c.addGuage(name, m.Snapshot())
|
c.addGauge(name, m.Snapshot())
|
||||||
case metrics.GaugeFloat64:
|
case metrics.GaugeFloat64:
|
||||||
c.addGuageFloat64(name, m.Snapshot())
|
c.addGaugeFloat64(name, m.Snapshot())
|
||||||
case metrics.Histogram:
|
case metrics.Histogram:
|
||||||
c.addHistogram(name, m.Snapshot())
|
c.addHistogram(name, m.Snapshot())
|
||||||
case metrics.Meter:
|
case metrics.Meter:
|
||||||
|
|
@ -50,8 +60,7 @@ func Handler(reg metrics.Registry) http.Handler {
|
||||||
default:
|
default:
|
||||||
log.Warn("Unknown Prometheus metric type", "type", fmt.Sprintf("%T", i))
|
log.Warn("Unknown Prometheus metric type", "type", fmt.Sprintf("%T", i))
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
w.Header().Add("Content-Type", "text/plain")
|
w.Header().Add("Content-Type", "text/plain")
|
||||||
w.Header().Add("Content-Length", fmt.Sprint(c.buff.Len()))
|
w.Header().Add("Content-Length", fmt.Sprint(c.buff.Len()))
|
||||||
w.Write(c.buff.Bytes())
|
w.Write(c.buff.Bytes())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue