Skip to content

Commit 31bc2a2

Browse files
chapsukkaralabe
authored andcommitted
metrics/prometheus: expose metrics in prometheus format too (#17077)
* metrics/prometheus: added prometheus http server and metrics collector * metrics/prometheus: minor cleanups * metrics/prometheus: named keys instead name in tag * metrics/prometheus: minor typo cleanups, sorted report
1 parent 74acde4 commit 31bc2a2

3 files changed

Lines changed: 185 additions & 0 deletions

File tree

‎metrics/exp/exp.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sync"
1010

1111
"github.com/ethereum/go-ethereum/metrics"
12+
"github.com/ethereum/go-ethereum/metrics/prometheus"
1213
)
1314

1415
type exp struct {
@@ -42,6 +43,7 @@ func Exp(r metrics.Registry) {
4243
// http.HandleFunc("/debug/vars", e.expHandler)
4344
// haven't found an elegant way, so just use a different endpoint
4445
http.Handle("/debug/metrics", h)
46+
http.Handle("/debug/metrics/prometheus", prometheus.Handler(r))
4547
}
4648

4749
// ExpHandler will return an expvar powered metrics handler.

‎metrics/prometheus/collector.go‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package prometheus
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"strconv"
23+
"strings"
24+
25+
"github.com/ethereum/go-ethereum/metrics"
26+
)
27+
28+
var (
29+
typeGaugeTpl = "# TYPE %s gauge\n"
30+
typeCounterTpl = "# TYPE %s counter\n"
31+
typeSummaryTpl = "# TYPE %s summary\n"
32+
keyValueTpl = "%s %v\n\n"
33+
keyQuantileTagValueTpl = "%s {quantile=\"%s\"} %v\n\n"
34+
)
35+
36+
// collector is a collection of byte buffers that aggregate Prometheus reports
37+
// for different metric types.
38+
type collector struct {
39+
buff *bytes.Buffer
40+
}
41+
42+
// newCollector createa a new Prometheus metric aggregator.
43+
func newCollector() *collector {
44+
return &collector{
45+
buff: &bytes.Buffer{},
46+
}
47+
}
48+
49+
func (c *collector) addCounter(name string, m metrics.Counter) {
50+
c.writeGaugeCounter(name, m.Count())
51+
}
52+
53+
func (c *collector) addGauge(name string, m metrics.Gauge) {
54+
c.writeGaugeCounter(name, m.Value())
55+
}
56+
57+
func (c *collector) addGaugeFloat64(name string, m metrics.GaugeFloat64) {
58+
c.writeGaugeCounter(name, m.Value())
59+
}
60+
61+
func (c *collector) addHistogram(name string, m metrics.Histogram) {
62+
pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999}
63+
ps := m.Percentiles(pv)
64+
c.writeSummaryCounter(name, m.Count())
65+
for i := range pv {
66+
c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i])
67+
}
68+
}
69+
70+
func (c *collector) addMeter(name string, m metrics.Meter) {
71+
c.writeGaugeCounter(name, m.Count())
72+
}
73+
74+
func (c *collector) addTimer(name string, m metrics.Timer) {
75+
pv := []float64{0.5, 0.75, 0.95, 0.99, 0.999, 0.9999}
76+
ps := m.Percentiles(pv)
77+
c.writeSummaryCounter(name, m.Count())
78+
for i := range pv {
79+
c.writeSummaryPercentile(name, strconv.FormatFloat(pv[i], 'f', -1, 64), ps[i])
80+
}
81+
}
82+
83+
func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) {
84+
if len(m.Values()) <= 0 {
85+
return
86+
}
87+
ps := m.Percentiles([]float64{50, 95, 99})
88+
val := m.Values()
89+
c.writeSummaryCounter(name, len(val))
90+
c.writeSummaryPercentile(name, "0.50", ps[0])
91+
c.writeSummaryPercentile(name, "0.95", ps[1])
92+
c.writeSummaryPercentile(name, "0.99", ps[2])
93+
}
94+
95+
func (c *collector) writeGaugeCounter(name string, value interface{}) {
96+
name = mutateKey(name)
97+
c.buff.WriteString(fmt.Sprintf(typeGaugeTpl, name))
98+
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
99+
}
100+
101+
func (c *collector) writeSummaryCounter(name string, value interface{}) {
102+
name = mutateKey(name + "_count")
103+
c.buff.WriteString(fmt.Sprintf(typeCounterTpl, name))
104+
c.buff.WriteString(fmt.Sprintf(keyValueTpl, name, value))
105+
}
106+
107+
func (c *collector) writeSummaryPercentile(name, p string, value interface{}) {
108+
name = mutateKey(name)
109+
c.buff.WriteString(fmt.Sprintf(typeSummaryTpl, name))
110+
c.buff.WriteString(fmt.Sprintf(keyQuantileTagValueTpl, name, p, value))
111+
}
112+
113+
func mutateKey(key string) string {
114+
return strings.Replace(key, "/", "_", -1)
115+
}

‎metrics/prometheus/prometheus.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package prometheus exposes go-metrics into a Prometheus format.
18+
package prometheus
19+
20+
import (
21+
"fmt"
22+
"net/http"
23+
"sort"
24+
25+
"github.com/ethereum/go-ethereum/log"
26+
"github.com/ethereum/go-ethereum/metrics"
27+
)
28+
29+
// Handler returns an HTTP handler which dump metrics in Prometheus format.
30+
func Handler(reg metrics.Registry) http.Handler {
31+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
32+
// Gather and pre-sort the metrics to avoid random listings
33+
var names []string
34+
reg.Each(func(name string, i interface{}) {
35+
names = append(names, name)
36+
})
37+
sort.Strings(names)
38+
39+
// Aggregate all the metris into a Prometheus collector
40+
c := newCollector()
41+
42+
for _, name := range names {
43+
i := reg.Get(name)
44+
45+
switch m := i.(type) {
46+
case metrics.Counter:
47+
c.addCounter(name, m.Snapshot())
48+
case metrics.Gauge:
49+
c.addGauge(name, m.Snapshot())
50+
case metrics.GaugeFloat64:
51+
c.addGaugeFloat64(name, m.Snapshot())
52+
case metrics.Histogram:
53+
c.addHistogram(name, m.Snapshot())
54+
case metrics.Meter:
55+
c.addMeter(name, m.Snapshot())
56+
case metrics.Timer:
57+
c.addTimer(name, m.Snapshot())
58+
case metrics.ResettingTimer:
59+
c.addResettingTimer(name, m.Snapshot())
60+
default:
61+
log.Warn("Unknown Prometheus metric type", "type", fmt.Sprintf("%T", i))
62+
}
63+
}
64+
w.Header().Add("Content-Type", "text/plain")
65+
w.Header().Add("Content-Length", fmt.Sprint(c.buff.Len()))
66+
w.Write(c.buff.Bytes())
67+
})
68+
}

0 commit comments

Comments
 (0)