Add metrics support for Azure & GCS Gateway (#8954)

We added support for caching and S3 related metrics in #8591. As
a continuation, it would be helpful to add support for Azure & GCS
gateway related metrics as well.
This commit is contained in:
Nitish Tiwari
2020-02-11 21:08:01 +05:30
committed by GitHub
parent 6b1f2fc133
commit 63be4709b7
6 changed files with 77 additions and 46 deletions

View File

@@ -417,3 +417,32 @@ func gatewayHandleEnvVars() {
}
}
}
// shouldMeterRequest checks whether incoming request should be added to prometheus gateway metrics
func shouldMeterRequest(req *http.Request) bool {
return !(guessIsBrowserReq(req) || guessIsHealthCheckReq(req) || guessIsMetricsReq(req))
}
// MetricsTransport is a custom wrapper around Transport to track metrics
type MetricsTransport struct {
Transport *http.Transport
Metrics *Metrics
}
// RoundTrip implements the RoundTrip method for MetricsTransport
func (m MetricsTransport) RoundTrip(r *http.Request) (*http.Response, error) {
metered := shouldMeterRequest(r)
if metered && (r.Method == http.MethodGet || r.Method == http.MethodHead) {
m.Metrics.IncRequests(r.Method)
m.Metrics.IncBytesSent(r.ContentLength)
}
// Make the request to the server.
resp, err := m.Transport.RoundTrip(r)
if err != nil {
return nil, err
}
if metered && (r.Method == http.MethodGet || r.Method == http.MethodHead) {
m.Metrics.IncBytesReceived(resp.ContentLength)
}
return resp, nil
}