metrichandler.go 482 B

1234567891011121314151617181920212223
  1. package handler
  2. import (
  3. "net/http"
  4. "github.com/tal-tech/go-zero/core/stat"
  5. "github.com/tal-tech/go-zero/core/timex"
  6. )
  7. func MetricHandler(metrics *stat.Metrics) func(http.Handler) http.Handler {
  8. return func(next http.Handler) http.Handler {
  9. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. startTime := timex.Now()
  11. defer func() {
  12. metrics.Add(stat.Task{
  13. Duration: timex.Since(startTime),
  14. })
  15. }()
  16. next.ServeHTTP(w, r)
  17. })
  18. }
  19. }