From 595123e7655549eccc87430205d4abde91ee8c4f Mon Sep 17 00:00:00 2001 From: Sean Marciniak <30928402+MovieStoreGuy@users.noreply.github.com> Date: Tue, 1 Aug 2023 09:27:33 +0930 Subject: [PATCH] [datadog receiver] Exit function on failed payload decode (#24658) **Description:** Exit on error to avoid NPE **Link to tracking Issue:** Fixes https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/24562 **Testing:** I would like to fix up how the component does tests in future PR. **Documentation:** N/A --- .chloggen/msg_fix-npe-datadog-receiver.yaml | 20 +++++++ receiver/datadogreceiver/receiver.go | 3 +- receiver/datadogreceiver/receiver_test.go | 60 +++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100755 .chloggen/msg_fix-npe-datadog-receiver.yaml diff --git a/.chloggen/msg_fix-npe-datadog-receiver.yaml b/.chloggen/msg_fix-npe-datadog-receiver.yaml new file mode 100755 index 000000000000..f75789f0638f --- /dev/null +++ b/.chloggen/msg_fix-npe-datadog-receiver.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'bug_fix' + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: datadogreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fixed NPE on failed to decode message path + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [24562] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/receiver/datadogreceiver/receiver.go b/receiver/datadogreceiver/receiver.go index b91eecce185c..81caa1debbee 100644 --- a/receiver/datadogreceiver/receiver.go +++ b/receiver/datadogreceiver/receiver.go @@ -89,8 +89,9 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques ddTraces, err = handlePayload(req) if err != nil { - http.Error(w, "Unable to unmarshal reqs", http.StatusInternalServerError) + http.Error(w, "Unable to unmarshal reqs", http.StatusBadRequest) ddr.params.Logger.Error("Unable to unmarshal reqs") + return } otelTraces := toTraces(ddTraces, req) diff --git a/receiver/datadogreceiver/receiver_test.go b/receiver/datadogreceiver/receiver_test.go index b293c168d82e..5c7d4c6f6552 100644 --- a/receiver/datadogreceiver/receiver_test.go +++ b/receiver/datadogreceiver/receiver_test.go @@ -5,12 +5,18 @@ package datadogreceiver import ( "context" + "fmt" + "io" + "net/http" + "strings" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/receiver/receivertest" + "go.uber.org/multierr" ) func TestDatadogReceiver_Lifecycle(t *testing.T) { @@ -25,3 +31,57 @@ func TestDatadogReceiver_Lifecycle(t *testing.T) { err = ddr.Shutdown(context.Background()) assert.NoError(t, err, "Server should stop") } + +func TestDatadogServer(t *testing.T) { + cfg := createDefaultConfig().(*Config) + dd, err := newDataDogReceiver( + cfg, + consumertest.NewNop(), + receivertest.NewNopCreateSettings(), + ) + require.NoError(t, err, "Must not error when creating receiver") + + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + require.NoError(t, dd.Start(ctx, componenttest.NewNopHost())) + t.Cleanup(func() { + require.NoError(t, dd.Shutdown(ctx), "Must not error shutting down") + }) + + for _, tc := range []struct { + name string + op io.Reader + + expectCode int + expectContent string + }{ + { + name: "invalid data", + op: strings.NewReader("{"), + expectCode: http.StatusBadRequest, + expectContent: "Unable to unmarshal reqs\n", + }, + } { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + req, err := http.NewRequest( + http.MethodPost, + fmt.Sprintf("http://%s/v0.7/traces", cfg.Endpoint), + tc.op, + ) + require.NoError(t, err, "Must not error when creating request") + + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err, "Must not error performing request") + + actual, err := io.ReadAll(resp.Body) + require.NoError(t, multierr.Combine(err, resp.Body.Close()), "Must not error when reading body") + + assert.Equal(t, tc.expectContent, string(actual)) + assert.Equal(t, tc.expectCode, resp.StatusCode, "Must match the expected status code") + }) + } +}