forked from getamis/istanbul-tools
/
metrics.go
140 lines (121 loc) · 4.32 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2017 AMIS Technologies
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package metrics
import (
"fmt"
"github.com/rcrowley/go-metrics"
)
type DefaultRegistry struct {
metrics.Registry
}
func NewRegistry() *DefaultRegistry {
r := metrics.NewRegistry()
return &DefaultRegistry{r}
}
func (r *DefaultRegistry) NewCounter(name string) *Counter {
return &Counter{metrics.GetOrRegisterCounter(name, r.Registry), name}
}
func (r *DefaultRegistry) NewMeter(name string) *Meter {
return &Meter{metrics.GetOrRegisterMeter(name, r.Registry), name}
}
func (r *DefaultRegistry) NewTimer(name string) *Timer {
return &Timer{metrics.GetOrRegisterTimer(name, r.Registry), name}
}
func (r *DefaultRegistry) NewHistogram(name string) *Histogram {
return &Histogram{metrics.GetOrRegisterHistogram(name, r.Registry, metrics.NewExpDecaySample(1028, 0.015)), name}
}
func (r *DefaultRegistry) Export() {
r.export()
}
func (r *DefaultRegistry) export() {
r.Registry.Each(func(name string, i interface{}) {
switch metric := i.(type) {
case metrics.Counter:
fmt.Printf("counter %s\n", name)
fmt.Printf(" count: %9d\n", metric.Count())
case metrics.Gauge:
fmt.Printf("gauge %s\n", name)
fmt.Printf(" value: %9d\n", metric.Value())
case metrics.GaugeFloat64:
fmt.Printf("gauge %s\n", name)
fmt.Printf(" value: %f\n", metric.Value())
case metrics.Healthcheck:
metric.Check()
fmt.Printf("healthcheck %s\n", name)
fmt.Printf(" error: %v\n", metric.Error())
case metrics.Histogram:
h := metric.Snapshot()
ps := h.Percentiles([]float64{0.5, 0.75, 0.90, 0.95, 0.99})
fmt.Printf("histogram %s\n", name)
fmt.Printf(" count: %9d\n", h.Count())
fmt.Printf(" min: %9d\n", h.Min())
fmt.Printf(" max: %9d\n", h.Max())
fmt.Printf(" mean: %e\n", h.Mean())
fmt.Printf(" stddev: %e\n", h.StdDev())
fmt.Printf(" median: %e\n", ps[0])
fmt.Printf(" 75%%: %e\n", ps[1])
fmt.Printf(" 90%%: %e\n", ps[2])
fmt.Printf(" 95%%: %e\n", ps[3])
fmt.Printf(" 99%%: %e\n", ps[4])
case metrics.Meter:
m := metric.Snapshot()
fmt.Printf("meter %s\n", name)
fmt.Printf(" count: %9d\n", m.Count())
fmt.Printf(" 1-min rate: %e\n", m.Rate1())
fmt.Printf(" 5-min rate: %e\n", m.Rate5())
fmt.Printf(" 15-min rate: %e\n", m.Rate15())
fmt.Printf(" mean rate: %e\n", m.RateMean())
case metrics.Timer:
t := metric.Snapshot()
ps := t.Percentiles([]float64{0.5, 0.75, 0.90, 0.95, 0.99})
fmt.Printf("timer %s\n", name)
fmt.Printf(" count: %9d\n", t.Count())
fmt.Printf(" min: %e\n", float64(t.Min()))
fmt.Printf(" max: %e\n", float64(t.Max()))
fmt.Printf(" mean: %e\n", t.Mean())
fmt.Printf(" stddev: %e\n", t.StdDev())
fmt.Printf(" median: %e\n", ps[0])
fmt.Printf(" 75%%: %e\n", ps[1])
fmt.Printf(" 90%%: %e\n", ps[2])
fmt.Printf(" 95%%: %e\n", ps[3])
fmt.Printf(" 99%%: %e\n", ps[4])
fmt.Printf(" 1-min rate: %e\n", t.Rate1())
fmt.Printf(" 5-min rate: %e\n", t.Rate5())
fmt.Printf(" 15-min rate: %e\n", t.Rate15())
fmt.Printf(" mean rate: %e\n", t.RateMean())
}
})
}
type Counter struct {
metrics.Counter
name string
}
func (c *Counter) Name() string { return c.name }
type Meter struct {
metrics.Meter
name string
}
func (m *Meter) Name() string { return m.name }
type Timer struct {
metrics.Timer
name string
}
func (t *Timer) Name() string { return t.name }
type Histogram struct {
metrics.Histogram
name string
}
func (h *Histogram) Name() string { return h.name }