Skip to content

Commit

Permalink
metrics/prometheus: minor typo cleanups, sorted report
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Apr 11, 2019
1 parent 55f2232 commit 88339c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
22 changes: 11 additions & 11 deletions metrics/prometheus/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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))
}

Expand Down
19 changes: 14 additions & 5 deletions metrics/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package prometheus
import (
"fmt"
"net/http"
"sort"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
Expand All @@ -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:
Expand All @@ -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())
Expand Down

0 comments on commit 88339c3

Please sign in to comment.