Add common middleware to S3 API handlers (#19171)

The middleware sets up tracing, throttling, gzipped responses and
collecting API stats.

Additionally, this change updates the names of handler functions in
metric labels to be the same as the name derived from Go lang reflection
on the handler name.

The metric api labels are now stored in memory the same as the handler
name - they will be camelcased, e.g. `GetObject` instead of `getobject`.

For compatibility, we lowercase the metric api label values when emitting the metrics.
This commit is contained in:
Aditya Manthramurthy
2024-03-04 10:05:56 -08:00
committed by GitHub
parent d5656eeb65
commit 9a4d003ac7
12 changed files with 372 additions and 203 deletions

View File

@@ -19,6 +19,7 @@ package cmd
import (
"net/http"
"strings"
"sync"
"sync/atomic"
@@ -326,7 +327,7 @@ func (stats *HTTPAPIStats) Get(api string) int {
}
// Load returns the recorded stats.
func (stats *HTTPAPIStats) Load() map[string]int {
func (stats *HTTPAPIStats) Load(toLower bool) map[string]int {
if stats == nil {
return map[string]int{}
}
@@ -336,6 +337,9 @@ func (stats *HTTPAPIStats) Load() map[string]int {
apiStats := make(map[string]int, len(stats.apiStats))
for k, v := range stats.apiStats {
if toLower {
k = strings.ToLower(k)
}
apiStats[k] = v
}
return apiStats
@@ -373,7 +377,7 @@ func (st *HTTPStats) incS3RequestsIncoming() {
}
// Converts http stats into struct to be sent back to the client.
func (st *HTTPStats) toServerHTTPStats() ServerHTTPStats {
func (st *HTTPStats) toServerHTTPStats(toLowerKeys bool) ServerHTTPStats {
serverStats := ServerHTTPStats{}
serverStats.S3RequestsIncoming = atomic.SwapUint64(&st.s3RequestsIncoming, 0)
serverStats.S3RequestsInQueue = atomic.LoadInt32(&st.s3RequestsInQueue)
@@ -382,22 +386,22 @@ func (st *HTTPStats) toServerHTTPStats() ServerHTTPStats {
serverStats.TotalS3RejectedHeader = atomic.LoadUint64(&st.rejectedRequestsHeader)
serverStats.TotalS3RejectedInvalid = atomic.LoadUint64(&st.rejectedRequestsInvalid)
serverStats.CurrentS3Requests = ServerHTTPAPIStats{
APIStats: st.currentS3Requests.Load(),
APIStats: st.currentS3Requests.Load(toLowerKeys),
}
serverStats.TotalS3Requests = ServerHTTPAPIStats{
APIStats: st.totalS3Requests.Load(),
APIStats: st.totalS3Requests.Load(toLowerKeys),
}
serverStats.TotalS3Errors = ServerHTTPAPIStats{
APIStats: st.totalS3Errors.Load(),
APIStats: st.totalS3Errors.Load(toLowerKeys),
}
serverStats.TotalS34xxErrors = ServerHTTPAPIStats{
APIStats: st.totalS34xxErrors.Load(),
APIStats: st.totalS34xxErrors.Load(toLowerKeys),
}
serverStats.TotalS35xxErrors = ServerHTTPAPIStats{
APIStats: st.totalS35xxErrors.Load(),
APIStats: st.totalS35xxErrors.Load(toLowerKeys),
}
serverStats.TotalS3Canceled = ServerHTTPAPIStats{
APIStats: st.totalS3Canceled.Load(),
APIStats: st.totalS3Canceled.Load(toLowerKeys),
}
return serverStats
}