stats.go 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. package s3api
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  4. stats_collect "github.com/seaweedfs/seaweedfs/weed/stats"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. )
  9. func track(f http.HandlerFunc, action string) http.HandlerFunc {
  10. return func(w http.ResponseWriter, r *http.Request) {
  11. bucket, _ := s3_constants.GetBucketAndObject(r)
  12. w.Header().Set("Server", "SeaweedFS S3")
  13. recorder := stats_collect.NewStatusResponseWriter(w)
  14. start := time.Now()
  15. f(recorder, r)
  16. if recorder.Status == http.StatusForbidden {
  17. if m, _ := stats_collect.S3RequestCounter.GetMetricWithLabelValues(
  18. action, strconv.Itoa(http.StatusOK), bucket); m == nil {
  19. bucket = ""
  20. }
  21. }
  22. stats_collect.S3RequestHistogram.WithLabelValues(action, bucket).Observe(time.Since(start).Seconds())
  23. stats_collect.S3RequestCounter.WithLabelValues(action, strconv.Itoa(recorder.Status), bucket).Inc()
  24. }
  25. }
  26. func TimeToFirstByte(action string, start time.Time, r *http.Request) {
  27. bucket, _ := s3_constants.GetBucketAndObject(r)
  28. stats_collect.S3TimeToFirstByteHistogram.WithLabelValues(action, bucket).Observe(float64(time.Since(start).Milliseconds()))
  29. }