stats.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. type StatusRecorder struct {
  10. http.ResponseWriter
  11. Status int
  12. }
  13. func NewStatusResponseWriter(w http.ResponseWriter) *StatusRecorder {
  14. return &StatusRecorder{w, http.StatusOK}
  15. }
  16. func (r *StatusRecorder) WriteHeader(status int) {
  17. r.Status = status
  18. r.ResponseWriter.WriteHeader(status)
  19. }
  20. func (r *StatusRecorder) Flush() {
  21. r.ResponseWriter.(http.Flusher).Flush()
  22. }
  23. func track(f http.HandlerFunc, action string) http.HandlerFunc {
  24. return func(w http.ResponseWriter, r *http.Request) {
  25. bucket, _ := s3_constants.GetBucketAndObject(r)
  26. w.Header().Set("Server", "SeaweedFS S3")
  27. recorder := NewStatusResponseWriter(w)
  28. start := time.Now()
  29. f(recorder, r)
  30. if recorder.Status == http.StatusForbidden {
  31. if m, _ := stats_collect.S3RequestCounter.GetMetricWithLabelValues(
  32. action, strconv.Itoa(http.StatusOK), bucket); m == nil {
  33. bucket = ""
  34. }
  35. }
  36. stats_collect.S3RequestHistogram.WithLabelValues(action, bucket).Observe(time.Since(start).Seconds())
  37. stats_collect.S3RequestCounter.WithLabelValues(action, strconv.Itoa(recorder.Status), bucket).Inc()
  38. }
  39. }