From 24ad59316d362d800a0d55cf8557baf2d0fba921 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Sat, 7 Dec 2019 11:21:52 +0530 Subject: [PATCH] Use atomic.Uint64 for gateway metrics count instead of mutex (#8615) --- cmd/gateway-metrics.go | 48 ++++++++++++++++++++---------------------- cmd/metrics.go | 30 ++++++++++++++++---------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/cmd/gateway-metrics.go b/cmd/gateway-metrics.go index b766a17f5..183b81bd4 100644 --- a/cmd/gateway-metrics.go +++ b/cmd/gateway-metrics.go @@ -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 + // 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) } - 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 -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 diff --git a/cmd/metrics.go b/cmd/metrics.go index 9e6a614ba..be34f64d5 100644 --- a/cmd/metrics.go +++ b/cmd/metrics.go @@ -290,17 +290,25 @@ func (c *minioCollector) Collect(ch chan<- prometheus.Metric) { prometheus.CounterValue, float64(m.GetBytesSent()), ) - for method, count := range m.GetRequests() { - 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(count), - method, - ) - } + s := m.GetRequests() + 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.Get.Load()), + 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, + ) } }