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.

37 lines
925 B

4 years ago
4 years ago
  1. package s3api
  2. import (
  3. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. )
  8. type StatusRecorder struct {
  9. http.ResponseWriter
  10. Status int
  11. }
  12. func NewStatusResponseWriter(w http.ResponseWriter) *StatusRecorder {
  13. return &StatusRecorder{w, http.StatusOK}
  14. }
  15. func (r *StatusRecorder) WriteHeader(status int) {
  16. r.Status = status
  17. r.ResponseWriter.WriteHeader(status)
  18. }
  19. func (r *StatusRecorder) Flush() {
  20. r.ResponseWriter.(http.Flusher).Flush()
  21. }
  22. func track(f http.HandlerFunc, action string) http.HandlerFunc {
  23. return func(w http.ResponseWriter, r *http.Request) {
  24. w.Header().Set("Server", "SeaweedFS S3")
  25. recorder := NewStatusResponseWriter(w)
  26. start := time.Now()
  27. f(recorder, r)
  28. stats_collect.S3RequestHistogram.WithLabelValues(action).Observe(time.Since(start).Seconds())
  29. stats_collect.S3RequestCounter.WithLabelValues(action, strconv.Itoa(recorder.Status)).Inc()
  30. }
  31. }