Use atomic.Uint64 for gateway metrics count instead of mutex (#8615)

master
Nitish Tiwari 5 years ago committed by GitHub
parent be0c8b1ec0
commit 24ad59316d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      cmd/gateway-metrics.go
  2. 30
      cmd/metrics.go

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

@ -290,17 +290,25 @@ func (c *minioCollector) Collect(ch chan<- prometheus.Metric) {
prometheus.CounterValue, prometheus.CounterValue,
float64(m.GetBytesSent()), float64(m.GetBytesSent()),
) )
for method, count := range m.GetRequests() { s := m.GetRequests()
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName("gateway", globalGatewayName, "requests"), prometheus.BuildFQName("gateway", globalGatewayName, "requests"),
"Total number of requests made to AWS S3 by current MinIO S3 Gateway", "Total number of requests made to AWS S3 by current MinIO S3 Gateway",
[]string{"method"}, nil), []string{"method"}, nil),
prometheus.CounterValue, prometheus.CounterValue,
float64(count), float64(s.Get.Load()),
method, http.MethodGet,
) )
} ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName("gateway", globalGatewayName, "requests"),
"Total number of requests made to AWS S3 by current MinIO S3 Gateway",
[]string{"method"}, nil),
prometheus.CounterValue,
float64(s.Head.Load()),
http.MethodHead,
)
} }
} }

Loading…
Cancel
Save