func metricsMiddleware(requestCount metrics.Counter) endpoint.Middleware { return func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, request interface{}) (interface{}, error) { requestCount.Add(1) return next(ctx, request) } } }
func httpInstrument(requests metrics.Counter, duration metrics.Histogram) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requests.Add(1) defer func(begin time.Time) { duration.Observe(time.Since(begin).Nanoseconds()) }(time.Now()) next.ServeHTTP(w, r) }) } }
func instrument(requests metrics.Counter, duration metrics.TimeHistogram) func(Add) Add { return func(next Add) Add { return func(ctx context.Context, a, b int64) int64 { defer func(begin time.Time) { requests.Add(1) duration.Observe(time.Since(begin)) }(time.Now()) return next(ctx, a, b) } } }
// TestCounter puts some deltas through the counter, and then calls the value // func to check that the counter has the correct final value. func TestCounter(counter metrics.Counter, value func() float64) error { a := rand.Perm(100) n := rand.Intn(len(a)) var want float64 for i := 0; i < n; i++ { f := float64(a[i]) counter.Add(f) want += f } if have := value(); want != have { return fmt.Errorf("want %f, have %f", want, have) } return nil }