forked from rcrowley/go-metrics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
59 lines (57 loc) · 1.59 KB
/
json.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
package metrics
import (
"encoding/json"
)
// MarshalJSON returns a byte slice containing a JSON representation of all
// the metrics in the Registry.
func (r StandardRegistry) MarshalJSON() ([]byte, error) {
data := make(map[string]map[string]interface{})
r.Each(func(name string, i interface{}) {
values := make(map[string]interface{})
switch m := i.(type) {
case Counter:
values["count"] = m.Count()
case Gauge:
values["value"] = m.Value()
case Healthcheck:
m.Check()
values["error"] = m.Error()
case Histogram:
ps := m.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
values["count"] = m.Count()
values["min"] = m.Min()
values["max"] = m.Max()
values["mean"] = m.Mean()
values["stddev"] = m.StdDev()
values["median"] = ps[0]
values["75%%"] = ps[1]
values["95%%"] = ps[2]
values["99%%"] = ps[3]
values["99.9%%"] = ps[4]
case Meter:
values["count"] = m.Count()
values["1m.rate"] = m.Rate1()
values["5m.rate"] = m.Rate5()
values["15m.rate"] = m.Rate15()
values["mean.rate"] = m.RateMean()
case Timer:
ps := m.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
values["count"] = m.Count()
values["min"] = m.Min()
values["max"] = m.Max()
values["mean"] = m.Mean()
values["stddev"] = m.StdDev()
values["median"] = ps[0]
values["75%%"] = ps[1]
values["95%%"] = ps[2]
values["99%%"] = ps[3]
values["99.9%%"] = ps[4]
values["1m.rate"] = m.Rate1()
values["5m.rate"] = m.Rate5()
values["15m.rate"] = m.Rate15()
values["mean.rate"] = m.RateMean()
}
data[name] = values
})
return json.Marshal(data)
}