| 1 |
// Metrics output to StatHat. |
| 2 |
package stathat |
| 3 |
|
| 4 |
import ( |
| 5 |
"github.com/rcrowley/go-metrics" |
| 6 |
"github.com/stathat/go" |
| 7 |
"log" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
func Stathat(r metrics.Registry, d time.Duration, userkey string) { |
| 12 |
for { |
| 13 |
if err := sh(r, userkey); nil != err { |
| 14 |
log.Println(err) |
| 15 |
} |
| 16 |
time.Sleep(d) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
func sh(r metrics.Registry, userkey string) error { |
| 21 |
r.Each(func(name string, i interface{}) { |
| 22 |
switch metric := i.(type) { |
| 23 |
case metrics.Counter: |
| 24 |
stathat.PostEZCount(name, userkey, int(metric.Count())) |
| 25 |
case metrics.Gauge: |
| 26 |
stathat.PostEZValue(name, userkey, float64(metric.Value())) |
| 27 |
case metrics.GaugeFloat64: |
| 28 |
stathat.PostEZValue(name, userkey, float64(metric.Value())) |
| 29 |
case metrics.Histogram: |
| 30 |
h := metric.Snapshot() |
| 31 |
ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) |
| 32 |
stathat.PostEZCount(name+".count", userkey, int(h.Count())) |
| 33 |
stathat.PostEZValue(name+".min", userkey, float64(h.Min())) |
| 34 |
stathat.PostEZValue(name+".max", userkey, float64(h.Max())) |
| 35 |
stathat.PostEZValue(name+".mean", userkey, float64(h.Mean())) |
| 36 |
stathat.PostEZValue(name+".std-dev", userkey, float64(h.StdDev())) |
| 37 |
stathat.PostEZValue(name+".50-percentile", userkey, float64(ps[0])) |
| 38 |
stathat.PostEZValue(name+".75-percentile", userkey, float64(ps[1])) |
| 39 |
stathat.PostEZValue(name+".95-percentile", userkey, float64(ps[2])) |
| 40 |
stathat.PostEZValue(name+".99-percentile", userkey, float64(ps[3])) |
| 41 |
stathat.PostEZValue(name+".999-percentile", userkey, float64(ps[4])) |
| 42 |
case metrics.Meter: |
| 43 |
m := metric.Snapshot() |
| 44 |
stathat.PostEZCount(name+".count", userkey, int(m.Count())) |
| 45 |
stathat.PostEZValue(name+".one-minute", userkey, float64(m.Rate1())) |
| 46 |
stathat.PostEZValue(name+".five-minute", userkey, float64(m.Rate5())) |
| 47 |
stathat.PostEZValue(name+".fifteen-minute", userkey, float64(m.Rate15())) |
| 48 |
stathat.PostEZValue(name+".mean", userkey, float64(m.RateMean())) |
| 49 |
case metrics.Timer: |
| 50 |
t := metric.Snapshot() |
| 51 |
ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999}) |
| 52 |
stathat.PostEZCount(name+".count", userkey, int(t.Count())) |
| 53 |
stathat.PostEZValue(name+".min", userkey, float64(t.Min())) |
| 54 |
stathat.PostEZValue(name+".max", userkey, float64(t.Max())) |
| 55 |
stathat.PostEZValue(name+".mean", userkey, float64(t.Mean())) |
| 56 |
stathat.PostEZValue(name+".std-dev", userkey, float64(t.StdDev())) |
| 57 |
stathat.PostEZValue(name+".50-percentile", userkey, float64(ps[0])) |
| 58 |
stathat.PostEZValue(name+".75-percentile", userkey, float64(ps[1])) |
| 59 |
stathat.PostEZValue(name+".95-percentile", userkey, float64(ps[2])) |
| 60 |
stathat.PostEZValue(name+".99-percentile", userkey, float64(ps[3])) |
| 61 |
stathat.PostEZValue(name+".999-percentile", userkey, float64(ps[4])) |
| 62 |
stathat.PostEZValue(name+".one-minute", userkey, float64(t.Rate1())) |
| 63 |
stathat.PostEZValue(name+".five-minute", userkey, float64(t.Rate5())) |
| 64 |
stathat.PostEZValue(name+".fifteen-minute", userkey, float64(t.Rate15())) |
| 65 |
stathat.PostEZValue(name+".mean-rate", userkey, float64(t.RateMean())) |
| 66 |
} |
| 67 |
}) |
| 68 |
return nil |
| 69 |
} |