You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

4 years ago
4 years ago
  1. package s3api
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  4. stats_collect "github.com/chrislusf/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. stats_collect.S3RequestHistogram.WithLabelValues(action, bucket).Observe(time.Since(start).Seconds())
  31. stats_collect.S3RequestCounter.WithLabelValues(action, strconv.Itoa(recorder.Status), bucket).Inc()
  32. }
  33. }