From 464ca4565295a7afef8577c69b22d2e2eae8a02f Mon Sep 17 00:00:00 2001 From: John Meagher Date: Tue, 8 Oct 2024 15:12:45 -0500 Subject: [PATCH 1/5] Return json for traces if the version is > 0.3 --- receiver/datadogreceiver/receiver.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/receiver/datadogreceiver/receiver.go b/receiver/datadogreceiver/receiver.go index bdfb6b9c53a8..c348eda0a515 100644 --- a/receiver/datadogreceiver/receiver.go +++ b/receiver/datadogreceiver/receiver.go @@ -243,7 +243,12 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques } } - _, _ = w.Write([]byte("OK")) + switch version := req.Header.Get(header.TracerVersion); version { + case "0.1", "0.2", "0.3": + _, _ = w.Write([]byte("OK")) + default: + _, _ = w.Write([]byte("{}")) + } } From 03db92fbf220ed720ea33eb25e2265324969197d Mon Sep 17 00:00:00 2001 From: John Meagher Date: Wed, 9 Oct 2024 15:21:47 -0500 Subject: [PATCH 2/5] Add test to verify functionality and fix things --- receiver/datadogreceiver/receiver.go | 6 ++++-- receiver/datadogreceiver/receiver_test.go | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/receiver/datadogreceiver/receiver.go b/receiver/datadogreceiver/receiver.go index c348eda0a515..5b1300a9d4fe 100644 --- a/receiver/datadogreceiver/receiver.go +++ b/receiver/datadogreceiver/receiver.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "net/http" + "strings" "github.com/DataDog/agent-payload/v5/gogen" pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace" @@ -243,8 +244,9 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques } } - switch version := req.Header.Get(header.TracerVersion); version { - case "0.1", "0.2", "0.3": + urlSplit := strings.Split(req.RequestURI, "/") + switch version := urlSplit[1]; version { + case "v0.1", "v0.2", "v0.3": _, _ = w.Write([]byte("OK")) default: _, _ = w.Write([]byte("{}")) diff --git a/receiver/datadogreceiver/receiver_test.go b/receiver/datadogreceiver/receiver_test.go index 9e53fcf43e8f..c2f0f68cb730 100644 --- a/receiver/datadogreceiver/receiver_test.go +++ b/receiver/datadogreceiver/receiver_test.go @@ -74,8 +74,9 @@ func TestDatadogServer(t *testing.T) { }) for _, tc := range []struct { - name string - op io.Reader + name string + op io.Reader + endpoint string expectCode int expectContent string @@ -83,15 +84,31 @@ func TestDatadogServer(t *testing.T) { { name: "invalid data", op: strings.NewReader("{"), + endpoint: "http://%s/v0.7/traces", expectCode: http.StatusBadRequest, expectContent: "Unable to unmarshal reqs\n", }, { name: "Fake featuresdiscovery", op: nil, // Content-length: 0. + endpoint: "http://%s/v0.7/traces", expectCode: http.StatusBadRequest, expectContent: "Fake featuresdiscovery\n", }, + { + name: "Older version returns OK", + op: strings.NewReader("[]"), + endpoint: "http://%s/v0.3/traces", + expectCode: http.StatusOK, + expectContent: "OK", + }, + { + name: "Older version returns JSON", + op: strings.NewReader("[]"), + endpoint: "http://%s/v0.4/traces", + expectCode: http.StatusOK, + expectContent: "{}", + }, } { tc := tc t.Run(tc.name, func(t *testing.T) { @@ -99,7 +116,7 @@ func TestDatadogServer(t *testing.T) { req, err := http.NewRequest( http.MethodPost, - fmt.Sprintf("http://%s/v0.7/traces", dd.(*datadogReceiver).address), + fmt.Sprintf(tc.endpoint, dd.(*datadogReceiver).address), tc.op, ) require.NoError(t, err, "Must not error when creating request") From d3affdeea07027da1bb4a37a00f5aa7e149bbc1e Mon Sep 17 00:00:00 2001 From: John Meagher Date: Wed, 9 Oct 2024 15:28:09 -0500 Subject: [PATCH 3/5] extra defensive coding --- receiver/datadogreceiver/receiver.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/receiver/datadogreceiver/receiver.go b/receiver/datadogreceiver/receiver.go index 5b1300a9d4fe..babc35202bb1 100644 --- a/receiver/datadogreceiver/receiver.go +++ b/receiver/datadogreceiver/receiver.go @@ -245,13 +245,17 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques } urlSplit := strings.Split(req.RequestURI, "/") - switch version := urlSplit[1]; version { - case "v0.1", "v0.2", "v0.3": + if len(urlSplit) == 3 { + // Match the response logic from dd-agent https://github.com/DataDog/datadog-agent/blob/86b2ae24f93941447a5bf0a2b6419caed77e76dd/pkg/trace/api/api.go#L511-L519 + switch version := urlSplit[1]; version { + case "v0.1", "v0.2", "v0.3": + _, _ = w.Write([]byte("OK")) + default: + _, _ = w.Write([]byte("{}")) + } + } else { _, _ = w.Write([]byte("OK")) - default: - _, _ = w.Write([]byte("{}")) } - } // handleV1Series handles the v1 series endpoint https://docs.datadoghq.com/api/latest/metrics/#submit-metrics From 6d763858f25fa6322cd323d31a096969682287ec Mon Sep 17 00:00:00 2001 From: John Meagher Date: Wed, 9 Oct 2024 17:42:52 -0500 Subject: [PATCH 4/5] Add changelog --- .../datadog-trace-receiver-return-body.yaml | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .chloggen/datadog-trace-receiver-return-body.yaml diff --git a/.chloggen/datadog-trace-receiver-return-body.yaml b/.chloggen/datadog-trace-receiver-return-body.yaml new file mode 100644 index 000000000000..26fb041b2540 --- /dev/null +++ b/.chloggen/datadog-trace-receiver-return-body.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# 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: Return a json reponse instead of "OK" when a trace is received with a newer protocol version. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [35705] + +# (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: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] From 703400d91bef42b8d4a20f5d61d73f9bcfe0110e Mon Sep 17 00:00:00 2001 From: John Meagher Date: Fri, 11 Oct 2024 07:21:02 -0500 Subject: [PATCH 5/5] Set response type and minor restructure --- receiver/datadogreceiver/receiver.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/receiver/datadogreceiver/receiver.go b/receiver/datadogreceiver/receiver.go index babc35202bb1..ba682cb44e38 100644 --- a/receiver/datadogreceiver/receiver.go +++ b/receiver/datadogreceiver/receiver.go @@ -244,18 +244,21 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques } } + responseBody := "OK" + contentType := "text/plain" urlSplit := strings.Split(req.RequestURI, "/") if len(urlSplit) == 3 { // Match the response logic from dd-agent https://github.com/DataDog/datadog-agent/blob/86b2ae24f93941447a5bf0a2b6419caed77e76dd/pkg/trace/api/api.go#L511-L519 switch version := urlSplit[1]; version { case "v0.1", "v0.2", "v0.3": - _, _ = w.Write([]byte("OK")) + // Keep the "OK" response for these versions default: - _, _ = w.Write([]byte("{}")) + contentType = "application/json" + responseBody = "{}" } - } else { - _, _ = w.Write([]byte("OK")) } + w.Header().Set("Content-Type", contentType) + _, _ = w.Write([]byte(responseBody)) } // handleV1Series handles the v1 series endpoint https://docs.datadoghq.com/api/latest/metrics/#submit-metrics