|
|
@@ -4,6 +4,7 @@ import (
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"time"
|
|
|
+ "sort"
|
|
|
)
|
|
|
|
|
|
// Output each metric in the given registry periodically using the given
|
|
|
@@ -15,26 +16,35 @@ func Write(r Registry, d time.Duration, w io.Writer) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// WriteOnce outputs metrics for the given registry to the specified io.Writer
|
|
|
+//
|
|
|
+// WriteOnce sorts by metric name to ensure the output remains consistent between runs
|
|
|
func WriteOnce(r Registry, w io.Writer) {
|
|
|
+ var data metrics
|
|
|
r.Each(func(name string, i interface{}) {
|
|
|
- switch metric := i.(type) {
|
|
|
+ data = append(data, metric{name, i})
|
|
|
+ })
|
|
|
+
|
|
|
+ sort.Sort(data)
|
|
|
+ for _, i := range data {
|
|
|
+ switch metric := i.m.(type) {
|
|
|
case Counter:
|
|
|
- fmt.Fprintf(w, "counter %s\n", name)
|
|
|
+ fmt.Fprintf(w, "counter %s\n", i.name)
|
|
|
fmt.Fprintf(w, " count: %9d\n", metric.Count())
|
|
|
case Gauge:
|
|
|
- fmt.Fprintf(w, "gauge %s\n", name)
|
|
|
+ fmt.Fprintf(w, "gauge %s\n", i.name)
|
|
|
fmt.Fprintf(w, " value: %9d\n", metric.Value())
|
|
|
case GaugeFloat64:
|
|
|
- fmt.Fprintf(w, "gauge %s\n", name)
|
|
|
+ fmt.Fprintf(w, "gauge %s\n", i.name)
|
|
|
fmt.Fprintf(w, " value: %f\n", metric.Value())
|
|
|
case Healthcheck:
|
|
|
metric.Check()
|
|
|
- fmt.Fprintf(w, "healthcheck %s\n", name)
|
|
|
+ fmt.Fprintf(w, "healthcheck %s\n", i.name)
|
|
|
fmt.Fprintf(w, " error: %v\n", metric.Error())
|
|
|
case Histogram:
|
|
|
h := metric.Snapshot()
|
|
|
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
|
|
- fmt.Fprintf(w, "histogram %s\n", name)
|
|
|
+ fmt.Fprintf(w, "histogram %s\n", i.name)
|
|
|
fmt.Fprintf(w, " count: %9d\n", h.Count())
|
|
|
fmt.Fprintf(w, " min: %9d\n", h.Min())
|
|
|
fmt.Fprintf(w, " max: %9d\n", h.Max())
|
|
|
@@ -47,7 +57,7 @@ func WriteOnce(r Registry, w io.Writer) {
|
|
|
fmt.Fprintf(w, " 99.9%%: %12.2f\n", ps[4])
|
|
|
case Meter:
|
|
|
m := metric.Snapshot()
|
|
|
- fmt.Fprintf(w, "meter %s\n", name)
|
|
|
+ fmt.Fprintf(w, "meter %s\n", i.name)
|
|
|
fmt.Fprintf(w, " count: %9d\n", m.Count())
|
|
|
fmt.Fprintf(w, " 1-min rate: %12.2f\n", m.Rate1())
|
|
|
fmt.Fprintf(w, " 5-min rate: %12.2f\n", m.Rate5())
|
|
|
@@ -56,7 +66,7 @@ func WriteOnce(r Registry, w io.Writer) {
|
|
|
case Timer:
|
|
|
t := metric.Snapshot()
|
|
|
ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
|
|
|
- fmt.Fprintf(w, "timer %s\n", name)
|
|
|
+ fmt.Fprintf(w, "timer %s\n", i.name)
|
|
|
fmt.Fprintf(w, " count: %9d\n", t.Count())
|
|
|
fmt.Fprintf(w, " min: %9d\n", t.Min())
|
|
|
fmt.Fprintf(w, " max: %9d\n", t.Max())
|
|
|
@@ -72,5 +82,5 @@ func WriteOnce(r Registry, w io.Writer) {
|
|
|
fmt.Fprintf(w, " 15-min rate: %12.2f\n", t.Rate15())
|
|
|
fmt.Fprintf(w, " mean rate: %12.2f\n", t.RateMean())
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
}
|