diff --git a/cmd/tools/rest/client.go b/cmd/tools/rest/client.go index 5b8c6ac88..6a486f299 100644 --- a/cmd/tools/rest/client.go +++ b/cmd/tools/rest/client.go @@ -158,9 +158,9 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) { doInvoke := func() ([]byte, error) { var ( - response *http.Response - body []byte - err error + response *http.Response + innerBody []byte + innerErr error ) if c.request.Body != nil { @@ -174,23 +174,23 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) { restReq := c.request.URL.String() // send request to server - if response, err = c.client.Do(c.request); err != nil { - return nil, fmt.Errorf("connection error %w", err) + if response, innerErr = c.client.Do(c.request); innerErr != nil { + return nil, fmt.Errorf("connection error %w", innerErr) } //goland:noinspection GoUnhandledErrorResult defer response.Body.Close() + innerBody, innerErr = io.ReadAll(response.Body) + if innerErr != nil { + return nil, errs.Rest(response.StatusCode, innerErr.Error(), 0, "") + } if response.StatusCode != http.StatusOK { - body2, err2 := io.ReadAll(response.Body) - if err2 != nil { - return nil, errs.Rest(response.StatusCode, err2.Error(), 0, "") - } if response.StatusCode == http.StatusUnauthorized { return nil, errs.New(errs.ErrAuthFailed, response.Status) } - result := gjson.GetBytes(body2, "error") + result := gjson.GetBytes(innerBody, "error") if response.StatusCode == http.StatusForbidden { message := result.Get(Message).String() @@ -206,16 +206,9 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) { return nil, errs.Rest(response.StatusCode, "", 0, "") } - // read response body - if body, err = io.ReadAll(response.Body); err != nil { - return nil, err - } - defer c.printRequestAndResponse(restReq, body) + defer c.printRequestAndResponse(restReq, innerBody) - if err != nil { - return nil, err - } - return body, nil + return innerBody, nil } body, err = doInvoke() diff --git a/pkg/matrix/matrix.go b/pkg/matrix/matrix.go index b88921e55..c20a77c70 100644 --- a/pkg/matrix/matrix.go +++ b/pkg/matrix/matrix.go @@ -78,7 +78,6 @@ func (m *Matrix) Clone(with With) *Matrix { clone.globalLabels = m.globalLabels clone.exportOptions = m.exportOptions clone.exportable = m.exportable - clone.displayMetrics = make(map[string]string) if with.Instances { clone.instances = make(map[string]*Instance, len(m.GetInstances())) @@ -95,6 +94,7 @@ func (m *Matrix) Clone(with With) *Matrix { if with.Metrics { clone.metrics = make(map[string]*Metric, len(m.GetMetrics())) + clone.displayMetrics = make(map[string]string) for key, metric := range m.GetMetrics() { c := metric.Clone(with.Data) clone.metrics[key] = c