diff --git a/pkg/insights/insightsclient/insightsclient.go b/pkg/insights/insightsclient/insightsclient.go index bd70706f69..60e4235528 100644 --- a/pkg/insights/insightsclient/insightsclient.go +++ b/pkg/insights/insightsclient/insightsclient.go @@ -180,7 +180,6 @@ func (c *Client) Send(ctx context.Context, endpoint string, source Source) error klog.Warningf("Failed to close response body: %v", err) } }() - switch resp.StatusCode { case http.StatusOK: counterRequestSend.WithLabelValues(c.metricsName, "200").Inc() @@ -189,25 +188,17 @@ func (c *Client) Send(ctx context.Context, endpoint string, source Source) error case http.StatusUnauthorized: counterRequestSend.WithLabelValues(c.metricsName, "401").Inc() klog.V(2).Infof("gateway server %s returned 401, x-rh-insights-request-id=%s", resp.Request.URL, requestID) - return authorizer.Error{Err: fmt.Errorf("your Red Hat account is not enabled for remote support or your token has expired")} + return authorizer.Error{Err: fmt.Errorf("your Red Hat account is not enabled for remote support or your token has expired: %s", responseBody(resp))} case http.StatusForbidden: counterRequestSend.WithLabelValues(c.metricsName, "403").Inc() klog.V(2).Infof("gateway server %s returned 403, x-rh-insights-request-id=%s", resp.Request.URL, requestID) return authorizer.Error{Err: fmt.Errorf("your Red Hat account is not enabled for remote support")} case http.StatusBadRequest: counterRequestSend.WithLabelValues(c.metricsName, "400").Inc() - body, _ := ioutil.ReadAll(resp.Body) - if len(body) > 1024 { - body = body[:1024] - } - return fmt.Errorf("gateway server bad request: %s (request=%s): %s", resp.Request.URL, requestID, string(body)) + return fmt.Errorf("gateway server bad request: %s (request=%s): %s", resp.Request.URL, requestID, responseBody(resp)) default: counterRequestSend.WithLabelValues(c.metricsName, strconv.Itoa(resp.StatusCode)).Inc() - body, _ := ioutil.ReadAll(resp.Body) - if len(body) > 1024 { - body = body[:1024] - } - return fmt.Errorf("gateway server reported unexpected error code: %d (request=%s): %s", resp.StatusCode, requestID, string(body)) + return fmt.Errorf("gateway server reported unexpected error code: %d (request=%s): %s", resp.StatusCode, requestID, responseBody(resp)) } if len(requestID) > 0 { @@ -217,6 +208,17 @@ func (c *Client) Send(ctx context.Context, endpoint string, source Source) error return nil } +func responseBody(r *http.Response) string { + if r == nil { + return "" + } + body, _ := ioutil.ReadAll(r.Body) + if len(body) > 1024 { + body = body[:1024] + } + return string(body) +} + var ( counterRequestSend = metrics.NewCounterVec(&metrics.CounterOpts{ Name: "insightsclient_request_send_total",