Skip to content

Commit

Permalink
Report the returned response body to log the error detail from cloud.…
Browse files Browse the repository at this point in the history
…redhat.com
  • Loading branch information
martinkunc committed Jul 13, 2020
1 parent 87e744e commit 019c588
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions pkg/insights/insightsclient/insightsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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",
Expand Down

0 comments on commit 019c588

Please sign in to comment.