fix: erroneous high value for gateway received bytes metrics (#8978)

http.Request.ContentLength can be negative, which affects
the gateway_s3_bytes_received value in Prometheus output.

The commit only increases the value of the total received bytes
in gateway mode when r.ContentLength is greater than zero.
This commit is contained in:
Anis Elleuch
2020-02-12 05:45:00 +01:00
committed by GitHub
parent c56c2f5fd3
commit 7d6766adc6
2 changed files with 10 additions and 6 deletions
+6 -2
View File
@@ -434,7 +434,9 @@ 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)
if r.ContentLength > 0 {
m.Metrics.IncBytesSent(uint64(r.ContentLength))
}
}
// Make the request to the server.
resp, err := m.Transport.RoundTrip(r)
@@ -442,7 +444,9 @@ func (m MetricsTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, err
}
if metered && (r.Method == http.MethodGet || r.Method == http.MethodHead) {
m.Metrics.IncBytesReceived(resp.ContentLength)
if r.ContentLength > 0 {
m.Metrics.IncBytesReceived(uint64(resp.ContentLength))
}
}
return resp, nil
}