diff --git a/pkg/splunk/splunk.go b/pkg/splunk/splunk.go index acbc6c46d..915240c6f 100644 --- a/pkg/splunk/splunk.go +++ b/pkg/splunk/splunk.go @@ -174,6 +174,17 @@ func tryPostMessages(telemetryData MonitoringData, messages []log.Message) error resp, err := SplunkClient.splunkClient.SendRequest(http.MethodPost, SplunkClient.splunkDsn, bytes.NewBuffer(payload), nil, nil) + if resp.StatusCode != http.StatusOK { + // log it to stdout + rdr := io.LimitReader(resp.Body, 1000) + body, errRead := ioutil.ReadAll(rdr) + log.Entry().Infof("%v: Splunk logging failed - %v", resp.Status, string(body)) + if errRead != nil { + return errors.Wrap(errRead, "Error reading response body from Splunk.") + } + return errors.Wrapf(err, "%v: Splunk logging failed - %v", resp.Status, string(body)) + } + if err != nil { return errors.Wrap(err, "error sending the requests to Splunk") } @@ -184,13 +195,6 @@ func tryPostMessages(telemetryData MonitoringData, messages []log.Message) error errors.Wrap(err, "closing response body failed") } }() - if resp.StatusCode != http.StatusOK { - rdr := io.LimitReader(resp.Body, 1000) - body, err := ioutil.ReadAll(rdr) - if err != nil { - return errors.Wrap(err, "Error reading response body") - } - return errors.Wrapf(err, "%v: Splunk logging failed - %v", resp.Status, string(body)) - } + return nil }