Skip to content

Commit

Permalink
Set timeout for requests made by the EVP proxy
Browse files Browse the repository at this point in the history
Without this, a request would continue running even after our Server
(which received a request from e.g. a tracer) has already timed out and,
because of this, would not propagate the response of the request anyway.
  • Loading branch information
albertvaka committed Jun 4, 2024
1 parent f4e0649 commit 80e32ef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/trace/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ func replyWithVersion(hash string, version string, h http.Handler) http.Handler
})
}

func getConfiguredRequestTimeoutDuration(conf *config.AgentConfig) time.Duration {
timeout := 5 * time.Second
if conf.ReceiverTimeout > 0 {
timeout = time.Duration(conf.ReceiverTimeout) * time.Second
}
return timeout
}

// Start starts doing the HTTP server and is ready to receive traces
func (r *HTTPReceiver) Start() {
if r.conf.ReceiverPort == 0 &&
Expand All @@ -205,10 +213,7 @@ func (r *HTTPReceiver) Start() {
return
}

timeout := 5 * time.Second
if r.conf.ReceiverTimeout > 0 {
timeout = time.Duration(r.conf.ReceiverTimeout) * time.Second
}
timeout := getConfiguredRequestTimeoutDuration(r.conf)
httpLogger := log.NewThrottled(5, 10*time.Second) // limit to 5 messages every 10 seconds
r.server = &http.Server{
ReadTimeout: timeout,
Expand Down
8 changes: 8 additions & 0 deletions pkg/trace/api/evp_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package api

import (
"bytes"
"context"
"fmt"
"io"
stdlog "log"
Expand Down Expand Up @@ -174,6 +175,13 @@ func (t *evpProxyTransport) RoundTrip(req *http.Request) (rresp *http.Response,
req.Header.Set("DD-APPLICATION-KEY", t.conf.EVPProxy.ApplicationKey)
}

// Timeout: Our outbound request(s) can't take longer than the WriteTimeout of the server
timeout := getConfiguredRequestTimeoutDuration(t.conf)
deadline := time.Now().Add(timeout)
ctx, ctxCancel := context.WithDeadline(req.Context(), deadline)
req = req.WithContext(ctx)
defer ctxCancel()

// Set target URL and API key header (per domain)
req.URL.Scheme = "https"
setTarget := func(r *http.Request, host, apiKey string) {
Expand Down

0 comments on commit 80e32ef

Please sign in to comment.