stats.go 983 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package s3api
  2. import (
  3. stats_collect "github.com/chrislusf/seaweedfs/weed/stats"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  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. w.Header().Set("Server", "SeaweedFS S3 "+util.VERSION)
  26. recorder := NewStatusResponseWriter(w)
  27. start := time.Now()
  28. f(recorder, r)
  29. stats_collect.S3RequestHistogram.WithLabelValues(action).Observe(time.Since(start).Seconds())
  30. stats_collect.S3RequestCounter.WithLabelValues(action, strconv.Itoa(recorder.Status)).Inc()
  31. }
  32. }