func report(sinker Sinker, interval time.Duration) { if sinker == nil { sinker = defaultSinker } var ( lastTime = time.Now() lastSnapshot, _ = metrics.Snapshot() ) for _ = range time.Tick(interval) { var ( now = time.Now() counters, _ = metrics.Snapshot() deltaCounters = make(map[string]uint64) dur = now.Sub(lastTime) ) for m, n := range counters { o, _ := lastSnapshot[m] if delta := n - o; delta != 0 { deltaCounters[m] = delta } } lastTime, lastSnapshot = now, counters sinker.Sink(deltaCounters, dur) } }
func TestMemStats(t *testing.T) { counters, gauges := metrics.Snapshot() expectedCounters := []string{ "Mem.NumGC", "Mem.PauseTotalNs", } expectedGauges := []string{ "Mem.LastGC", "Mem.Alloc", "Mem.HeapObjects", } for _, name := range expectedCounters { if _, ok := counters[name]; !ok { t.Errorf("Missing counters %q", name) } } for _, name := range expectedGauges { if _, ok := gauges[name]; !ok { t.Errorf("Missing gauge %q", name) } } }
func TestCounterBatchFunc(t *testing.T) { metrics.Reset() var a, b uint64 metrics.Counter("whee").SetBatchFunc( "yay", func() { a, b = 1, 2 }, func() uint64 { return a }, ) metrics.Counter("woo").SetBatchFunc( "yay", func() { a, b = 1, 2 }, func() uint64 { return b }, ) counters, _ := metrics.Snapshot() if v, want := counters["whee"], uint64(1); v != want { t.Errorf("Counter was %v, but expected %v", v, want) } if v, want := counters["woo"], uint64(2); v != want { t.Errorf("Counter was %v, but expected %v", v, want) } }
func (r *ReportResource) GetAll(res http.ResponseWriter, req *http.Request) { c, g := metrics.Snapshot() if err := rest.SetOKResponse(res, api.Report{Counters: c, Gauges: g}); err != nil { rest.SetInternalServerErrorResponse(res, err) return } }
func TestMetrics(t *testing.T) { h := Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { time.Sleep(100 * time.Millisecond) fmt.Fprintln(w, "hello, world") })) s := httptest.NewServer(h) defer s.Close() resp, err := http.Get(s.URL + "/hello") if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != 200 { t.Errorf("Status code was %d, but expected 200", resp.StatusCode) } b, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } a := string(b) e := "hello, world\n" if a != e { t.Errorf("Response was %q, but expected %q", a, e) } counters, gauges := metrics.Snapshot() expectedGauges := []string{ "HTTP.Latency.P50", "HTTP.Latency.P75", "HTTP.Latency.P90", "HTTP.Latency.P95", "HTTP.Latency.P99", "HTTP.Latency.P999", } for _, name := range expectedGauges { if _, ok := gauges[name]; !ok { t.Errorf("Missing gauge %q", name) } } expectedCounters := []string{ "HTTP.Requests", "HTTP.Responses", } for _, name := range expectedCounters { if _, ok := counters[name]; !ok { t.Errorf("Missing counter %q", name) } } }
func (s *server) Metrics(ctx context.Context, req *pb.MetricsRequest) (*pb.MetricsReply, error) { counters, _ := metrics.Snapshot() cms := make([]*pb.Metric, 0, len(counters)) for n, v := range counters { cms = append(cms, &pb.Metric{Name: n, Val: strconv.FormatUint(v, 10)}) } sort.Sort(metricsByName(cms)) r := &pb.MetricsReply{Counters: cms} return r, nil }
func Save() { if archaius.Conf.Collect { file, _ := os.Create("json_metrics/" + archaius.Conf.Arch + "_metrics.json") counters, gauges := metrics.Snapshot() cj, _ := json.Marshal(counters) gj, _ := json.Marshal(gauges) file.WriteString(fmt.Sprintf("{\n\"counters\":%v\n\"gauges\":%v\n}\n", string(cj), string(gj))) file.Close() } }
func TestGaugeValue(t *testing.T) { metrics.Reset() metrics.Gauge("whee").Set(-100) _, gauges := metrics.Snapshot() if v, want := gauges["whee"], int64(-100); v != want { t.Errorf("Gauge was %v, but expected %v", v, want) } }
func TestCounter(t *testing.T) { metrics.Reset() metrics.Counter("whee").Add() metrics.Counter("whee").AddN(10) counters, _ := metrics.Snapshot() if v, want := counters["whee"], uint64(11); v != want { t.Errorf("Counter was %v, but expected %v", v, want) } }
func TestGaugeRemove(t *testing.T) { metrics.Reset() metrics.Gauge("whee").Set(1) metrics.Gauge("whee").Remove() _, gauges := metrics.Snapshot() if v, ok := gauges["whee"]; ok { t.Errorf("Gauge was %v, but expected nothing", v) } }
func TestCounterRemove(t *testing.T) { metrics.Reset() metrics.Counter("whee").Add() metrics.Counter("whee").Remove() counters, _ := metrics.Snapshot() if v, ok := counters["whee"]; ok { t.Errorf("Counter was %v, but expected nothing", v) } }
func TestHistogramRemove(t *testing.T) { metrics.Reset() h := metrics.NewHistogram("heyo", 1, 1000, 3) h.Remove() _, gauges := metrics.Snapshot() if v, ok := gauges["heyo.P50"]; ok { t.Errorf("Gauge was %v, but expected nothing", v) } }
func HasQuota(clientID int64) bool { if _, ok := quotas[clientID]; !ok { return true } counters, _ := metrics.Snapshot() nops := counters[stats.ClientCounterName(clientID)] mu.Lock() defer mu.Unlock() return int(nops)-ops[clientID] <= quotas[clientID]*flushIntervalSecond }
func updateOps() { mu.Lock() defer mu.Unlock() counters, _ := metrics.Snapshot() for k, v := range counters { id, err := stats.ParseClientCounterName(k) if err != nil { continue } ops[id] = int(v) } }
func TestCounterFunc(t *testing.T) { metrics.Reset() metrics.Counter("whee").SetFunc(func() uint64 { return 100 }) counters, _ := metrics.Snapshot() if v, want := counters["whee"], uint64(100); v != want { t.Errorf("Counter was %v, but expected %v", v, want) } }
func TestGoroutinesStats(t *testing.T) { _, gauges := metrics.Snapshot() expected := []string{ "Goroutines.Num", } for _, name := range expected { if _, ok := gauges[name]; !ok { t.Errorf("Missing gauge %q", name) } } }
func TestFdStats(t *testing.T) { _, gauges := metrics.Snapshot() expected := []string{ "FileDescriptors.Max", "FileDescriptors.Used", } for _, name := range expected { if _, ok := gauges[name]; !ok { t.Errorf("Missing gauge %q", name) } } }
func (s *TestSuite) TestHistogram(c *C) { expected := map[string]int64{ "foo.P50": 5, "foo.P75": 5, "foo.P90": 15, "foo.P95": 15, "foo.P99": 15, "foo.P999": 15, } h := metrics.NewHistogram("foo", 0, 100, 4) h.RecordValue(1) h.RecordValue(5) h.RecordValue(5) h.RecordValue(15) _, g := metrics.Snapshot() log.Printf("g: %+v", g) c.Assert(g, DeepEquals, expected) }
func TestHistogram(t *testing.T) { metrics.Reset() h := metrics.NewHistogram("heyo", 1, 1000, 3) for i := 100; i > 0; i-- { for j := 0; j < i; j++ { h.RecordValue(int64(i)) } } _, gauges := metrics.Snapshot() if v, want := gauges["heyo.P50"], int64(71); v != want { t.Errorf("P50 was %v, but expected %v", v, want) } if v, want := gauges["heyo.P75"], int64(87); v != want { t.Errorf("P75 was %v, but expected %v", v, want) } if v, want := gauges["heyo.P90"], int64(95); v != want { t.Errorf("P90 was %v, but expected %v", v, want) } if v, want := gauges["heyo.P95"], int64(98); v != want { t.Errorf("P95 was %v, but expected %v", v, want) } if v, want := gauges["heyo.P99"], int64(100); v != want { t.Errorf("P99 was %v, but expected %v", v, want) } if v, want := gauges["heyo.P999"], int64(100); v != want { t.Errorf("P999 was %v, but expected %v", v, want) } }
func (h *RequestHandler) HandleStatusRequest(w http.ResponseWriter, r *http.Request) error { counters, guages := metrics.Snapshot() return writeJsonResponse(w, Status{true, counters, guages, viper.AllSettings()}, nil) }