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.

50 lines
1.5 KiB

4 years ago
4 years ago
  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. }
  40. func TimeToFirstByte(action string, start time.Time, r *http.Request) {
  41. bucket, _ := s3_constants.GetBucketAndObject(r)
  42. stats_collect.S3TimeToFirstByteHistogram.WithLabelValues(action, bucket).Observe(float64(time.Since(start).Milliseconds()))
  43. }