|
|
|
@ -17,60 +17,58 @@ |
|
|
|
|
package cmd |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"sync" |
|
|
|
|
"net/http" |
|
|
|
|
|
|
|
|
|
"go.uber.org/atomic" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// RequestStats - counts for Get and Head requests
|
|
|
|
|
type RequestStats struct { |
|
|
|
|
Get atomic.Uint64 `json:"Get"` |
|
|
|
|
Head atomic.Uint64 `json:"Head"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Metrics - represents bytes served from backend
|
|
|
|
|
// only implemented for S3 Gateway
|
|
|
|
|
type Metrics struct { |
|
|
|
|
BytesReceived atomic.Uint64 |
|
|
|
|
BytesSent atomic.Uint64 |
|
|
|
|
RequestStats map[string]int |
|
|
|
|
sync.RWMutex |
|
|
|
|
bytesReceived atomic.Uint64 |
|
|
|
|
bytesSent atomic.Uint64 |
|
|
|
|
requestStats RequestStats |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// IncBytesReceived - Increase total bytes received from gateway backend
|
|
|
|
|
func (s *Metrics) IncBytesReceived(n int64) { |
|
|
|
|
s.BytesReceived.Add(uint64(n)) |
|
|
|
|
s.bytesReceived.Add(uint64(n)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetBytesReceived - Get total bytes received from gateway backend
|
|
|
|
|
func (s *Metrics) GetBytesReceived() uint64 { |
|
|
|
|
return s.BytesReceived.Load() |
|
|
|
|
return s.bytesReceived.Load() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// IncBytesSent - Increase total bytes sent to gateway backend
|
|
|
|
|
func (s *Metrics) IncBytesSent(n int64) { |
|
|
|
|
s.BytesSent.Add(uint64(n)) |
|
|
|
|
s.bytesSent.Add(uint64(n)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetBytesSent - Get total bytes received from gateway backend
|
|
|
|
|
func (s *Metrics) GetBytesSent() uint64 { |
|
|
|
|
return s.BytesSent.Load() |
|
|
|
|
return s.bytesSent.Load() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// IncRequests - Increase request sent to gateway backend by 1
|
|
|
|
|
// IncRequests - Increase request count sent to gateway backend by 1
|
|
|
|
|
func (s *Metrics) IncRequests(method string) { |
|
|
|
|
s.Lock() |
|
|
|
|
defer s.Unlock() |
|
|
|
|
if s == nil { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
if s.RequestStats == nil { |
|
|
|
|
s.RequestStats = make(map[string]int) |
|
|
|
|
} |
|
|
|
|
if _, ok := s.RequestStats[method]; ok { |
|
|
|
|
s.RequestStats[method]++ |
|
|
|
|
return |
|
|
|
|
// Only increment for Head & Get requests, else no op
|
|
|
|
|
if method == http.MethodGet { |
|
|
|
|
s.requestStats.Get.Add(1) |
|
|
|
|
} else if method == http.MethodHead { |
|
|
|
|
s.requestStats.Head.Add(1) |
|
|
|
|
} |
|
|
|
|
s.RequestStats[method] = 1 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// GetRequests - Get total number of requests sent to gateway backend
|
|
|
|
|
func (s *Metrics) GetRequests() map[string]int { |
|
|
|
|
return s.RequestStats |
|
|
|
|
// GetRequests - Get total number of Get & Headrequests sent to gateway backend
|
|
|
|
|
func (s *Metrics) GetRequests() RequestStats { |
|
|
|
|
return s.requestStats |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewMetrics - Prepare new Metrics structure
|
|
|
|
|