From 05f3ece2ce4591aefae4522b3c143f6b3a220e7c Mon Sep 17 00:00:00 2001 From: Ryan Fitzpatrick Date: Mon, 25 Jul 2022 13:54:35 -0400 Subject: [PATCH] [exporter/signalfx] fix invalid error response message (#12654) Fixing a bug - The signalfx exporter discards metric submission response messages and thus provides invalid error content --- exporter/signalfxexporter/dpclient.go | 6 ++++-- exporter/signalfxexporter/exporter_test.go | 7 +++++++ unreleased/sfxexportearlyrespclose.yaml | 5 +++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100755 unreleased/sfxexportearlyrespclose.yaml diff --git a/exporter/signalfxexporter/dpclient.go b/exporter/signalfxexporter/dpclient.go index 3e110e585a5c..33c4d3de4dd0 100644 --- a/exporter/signalfxexporter/dpclient.go +++ b/exporter/signalfxexporter/dpclient.go @@ -138,8 +138,10 @@ func (s *sfxDPClient) pushMetricsDataForToken(ctx context.Context, sfxDataPoints return len(sfxDataPoints), err } - io.Copy(ioutil.Discard, resp.Body) - resp.Body.Close() + defer func() { + io.Copy(ioutil.Discard, resp.Body) + resp.Body.Close() + }() err = splunk.HandleHTTPCode(resp) if err != nil { diff --git a/exporter/signalfxexporter/exporter_test.go b/exporter/signalfxexporter/exporter_test.go index 21e07e98e827..b5caa6517a0c 100644 --- a/exporter/signalfxexporter/exporter_test.go +++ b/exporter/signalfxexporter/exporter_test.go @@ -140,6 +140,7 @@ func TestConsumeMetrics(t *testing.T) { wantErr bool wantPermanentErr bool wantThrottleErr bool + expectedErrorMsg string }{ { name: "happy_path", @@ -152,6 +153,7 @@ func TestConsumeMetrics(t *testing.T) { httpResponseCode: http.StatusForbidden, numDroppedTimeSeries: 1, wantErr: true, + expectedErrorMsg: "HTTP 403 \"Forbidden\"", }, { name: "response_bad_request", @@ -159,6 +161,7 @@ func TestConsumeMetrics(t *testing.T) { httpResponseCode: http.StatusBadRequest, numDroppedTimeSeries: 1, wantPermanentErr: true, + expectedErrorMsg: "Permanent error: \"HTTP/1.1 400 Bad Request", }, { name: "response_throttle", @@ -190,6 +193,7 @@ func TestConsumeMetrics(t *testing.T) { w.Header().Add(splunk.HeaderRetryAfter, strconv.Itoa(tt.retryAfter)) } w.WriteHeader(tt.httpResponseCode) + w.Write([]byte("response content")) })) defer server.Close() @@ -219,12 +223,15 @@ func TestConsumeMetrics(t *testing.T) { if tt.wantErr { assert.Error(t, err) + assert.EqualError(t, err, tt.expectedErrorMsg) return } if tt.wantPermanentErr { assert.Error(t, err) assert.True(t, consumererror.IsPermanent(err)) + assert.True(t, strings.HasPrefix(err.Error(), tt.expectedErrorMsg)) + assert.Contains(t, err.Error(), "response content") return } diff --git a/unreleased/sfxexportearlyrespclose.yaml b/unreleased/sfxexportearlyrespclose.yaml new file mode 100755 index 000000000000..55a95dc9c84a --- /dev/null +++ b/unreleased/sfxexportearlyrespclose.yaml @@ -0,0 +1,5 @@ +change_type: bug_fix +component: signalfxexporter +note: fix invalid response error message +issues: [12654] +