stats.go 925 B

12345678910111213141516171819202122232425262728293031323334353637
  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. }