Skip to content

Commit c14609d

Browse files
jmeagherMovieStoreGuy
authored andcommitted
[receiver/datadogreceiver] Return json for traces if the version is > 0.3 (open-telemetry#35705)
#### Description Problem: * [The Datadog agent for versions > 0.3 returns Json instead of "Ok"](https://github.com/DataDog/datadog-agent/blob/86b2ae24f93941447a5bf0a2b6419caed77e76dd/pkg/trace/api/api.go#L511-L519) * [dd-trace-js is using V0.4 for sending data to Datadog and that expects to get a JSON response back](https://github.com/DataDog/dd-trace-js/blob/2d175d30d5ad7291c238819116a07f003074316b/packages/dd-trace/src/exporters/agent/writer.js#L52) * When sending traces from NodeJS with the dd-trace-js library it works, but throws a high volume of errors from the non-json response. Fix: For trace submissions with a version > 0.3 return a valid, but empty Json response. <!--Describe what testing was performed and which tests were added.--> #### Testing Added unit test to ensure the expected responses to traces are returned. --------- Co-authored-by: Sean Marciniak <[email protected]>
1 parent 021d80e commit c14609d

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: datadogreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Return a json reponse instead of "OK" when a trace is received with a newer protocol version.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [35705]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/datadogreceiver/receiver.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"net/http"
13+
"strings"
1314

1415
"github.com/DataDog/agent-payload/v5/gogen"
1516
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
@@ -244,8 +245,21 @@ func (ddr *datadogReceiver) handleTraces(w http.ResponseWriter, req *http.Reques
244245
}
245246
}
246247

247-
_, _ = w.Write([]byte("OK"))
248-
248+
responseBody := "OK"
249+
contentType := "text/plain"
250+
urlSplit := strings.Split(req.RequestURI, "/")
251+
if len(urlSplit) == 3 {
252+
// Match the response logic from dd-agent https://github.com/DataDog/datadog-agent/blob/86b2ae24f93941447a5bf0a2b6419caed77e76dd/pkg/trace/api/api.go#L511-L519
253+
switch version := urlSplit[1]; version {
254+
case "v0.1", "v0.2", "v0.3":
255+
// Keep the "OK" response for these versions
256+
default:
257+
contentType = "application/json"
258+
responseBody = "{}"
259+
}
260+
}
261+
w.Header().Set("Content-Type", contentType)
262+
_, _ = w.Write([]byte(responseBody))
249263
}
250264

251265
// handleV1Series handles the v1 series endpoint https://docs.datadoghq.com/api/latest/metrics/#submit-metrics

receiver/datadogreceiver/receiver_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,49 @@ func TestDatadogServer(t *testing.T) {
7575
})
7676

7777
for _, tc := range []struct {
78-
name string
79-
op io.Reader
78+
name string
79+
op io.Reader
80+
endpoint string
8081

8182
expectCode int
8283
expectContent string
8384
}{
8485
{
8586
name: "invalid data",
8687
op: strings.NewReader("{"),
88+
endpoint: "http://%s/v0.7/traces",
8789
expectCode: http.StatusBadRequest,
8890
expectContent: "Unable to unmarshal reqs\n",
8991
},
9092
{
9193
name: "Fake featuresdiscovery",
9294
op: nil, // Content-length: 0.
95+
endpoint: "http://%s/v0.7/traces",
9396
expectCode: http.StatusBadRequest,
9497
expectContent: "Fake featuresdiscovery\n",
9598
},
99+
{
100+
name: "Older version returns OK",
101+
op: strings.NewReader("[]"),
102+
endpoint: "http://%s/v0.3/traces",
103+
expectCode: http.StatusOK,
104+
expectContent: "OK",
105+
},
106+
{
107+
name: "Older version returns JSON",
108+
op: strings.NewReader("[]"),
109+
endpoint: "http://%s/v0.4/traces",
110+
expectCode: http.StatusOK,
111+
expectContent: "{}",
112+
},
96113
} {
97114
tc := tc
98115
t.Run(tc.name, func(t *testing.T) {
99116
t.Parallel()
100117

101118
req, err := http.NewRequest(
102119
http.MethodPost,
103-
fmt.Sprintf("http://%s/v0.7/traces", dd.(*datadogReceiver).address),
120+
fmt.Sprintf(tc.endpoint, dd.(*datadogReceiver).address),
104121
tc.op,
105122
)
106123
require.NoError(t, err, "Must not error when creating request")

0 commit comments

Comments
 (0)