From 4a92134235a522447102b4076b5dd680faf922cf Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Mon, 12 Sep 2022 20:40:51 +0100 Subject: [PATCH] prometheus: track errors during REST read/write calls (#15678) minio_inter_node_traffic_errors_total currently does not track requests body write/read errors of internode REST communications. This commit fixes this by wrapping resp.Body. --- internal/rest/client.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/rest/client.go b/internal/rest/client.go index e8a42b76f..b0f7e2d18 100644 --- a/internal/rest/client.go +++ b/internal/rest/client.go @@ -211,6 +211,26 @@ func (c *Client) newRequest(ctx context.Context, u *url.URL, body io.Reader) (*h return req, nil } +type respBodyMonitor struct { + io.ReadCloser +} + +func (r respBodyMonitor) Read(p []byte) (n int, err error) { + n, err = r.ReadCloser.Read(p) + if err != nil && err != io.EOF { + atomic.AddUint64(&networkErrsCounter, 1) + } + return +} + +func (r respBodyMonitor) Close() (err error) { + err = r.ReadCloser.Close() + if err != nil { + atomic.AddUint64(&networkErrsCounter, 1) + } + return +} + // Call - make a REST call with context. func (c *Client) Call(ctx context.Context, method string, values url.Values, body io.Reader, length int64) (reply io.ReadCloser, err error) { urlStr := c.url.String() @@ -286,6 +306,9 @@ func (c *Client) Call(ctx context.Context, method string, values url.Values, bod } return nil, errors.New(resp.Status) } + if !c.NoMetrics && !c.ExpectTimeouts { + resp.Body = &respBodyMonitor{resp.Body} + } return resp.Body, nil }