Skip to content

Commit

Permalink
[EVPProxy] Do not manually cancel the request context (#30183)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertvaka authored Oct 17, 2024
1 parent 63ded02 commit b77289c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/trace/api/evp_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ func (t *evpProxyTransport) RoundTrip(req *http.Request) (rresp *http.Response,
timeout := getConfiguredEVPRequestTimeoutDuration(t.conf)
req.Header.Set("X-Datadog-Timeout", strconv.Itoa((int(timeout.Seconds()))))
deadline := time.Now().Add(timeout)
ctx, ctxCancel := context.WithDeadline(req.Context(), deadline)
//nolint:govet,lostcancel we don't need to manually cancel this context, we can rely on the parent context being cancelled
ctx, _ := context.WithDeadline(req.Context(), deadline)
req = req.WithContext(ctx)
defer ctxCancel()

// Set target URL and API key header (per domain)
req.URL.Scheme = "https"
Expand Down
26 changes: 26 additions & 0 deletions pkg/trace/api/evp_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,30 @@ func TestE2E(t *testing.T) {
require.Equal(t, http.StatusBadGateway, resp.StatusCode, "Got: ", fmt.Sprint(resp.StatusCode))
assert.Equal(t, "http: proxy error: context deadline exceeded\n", logs)
})

t.Run("chunked-response", func(t *testing.T) {
conf := newTestReceiverConfig()
conf.Site = "us3.datadoghq.com"
conf.Endpoints[0].APIKey = "test_api_key"
conf.EVPProxy.ReceiverTimeout = 1 // in seconds

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Transfer-Encoding", "chunked")
w.Write([]byte(`Hello`))
w.(http.Flusher).Flush()
time.Sleep(200 * time.Millisecond)
w.Write([]byte(`World`)) // this will be discarded if the context was cancelled
}))

req := httptest.NewRequest("POST", "/mypath/mysubpath?arg=test", bytes.NewReader(randBodyBuf))
req.Header.Set("X-Datadog-EVP-Subdomain", "my.subdomain")
resp, logs := sendRequestThroughForwarderAgainstDummyServer(conf, req, stats, strings.TrimPrefix(server.URL, "http://"))

resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode, "Got: ", fmt.Sprint(resp.StatusCode))
assert.Equal(t, "", logs)
body, err := io.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "HelloWorld", string(body))
})
}

0 comments on commit b77289c

Please sign in to comment.