stats.go 1.0 KB

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