diff --git a/agent/collector.go b/agent/collector.go index 48b2a4f5c..1e26c691d 100644 --- a/agent/collector.go +++ b/agent/collector.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "context" "io" "io/ioutil" "math/rand" @@ -59,7 +60,7 @@ func NewCollector(cfg *config.AgentConfig) (Collector, error) { rtIntervalCh: make(chan time.Duration), cfg: cfg, groupID: rand.Int31(), - httpClient: http.Client{Transport: cfg.Transport}, + httpClient: http.Client{Timeout: HTTPTimeout, Transport: cfg.Transport}, enabledChecks: enabledChecks, // Defaults for real-time on start @@ -180,6 +181,10 @@ func (l *Collector) postMessage(endpoint string, m model.MessageBody) { req.Header.Add("X-Dd-Hostname", l.cfg.HostName) req.Header.Add("X-Dd-Processagentversion", Version) + ctx, cancel := context.WithTimeout(context.Background(), ReqCtxTimeout) + defer cancel() + req.WithContext(ctx) + resp, err := l.httpClient.Do(req) if err != nil { if isHTTPTimeout(err) { diff --git a/agent/util.go b/agent/util.go index 12fe6245a..4829b6d10 100644 --- a/agent/util.go +++ b/agent/util.go @@ -2,6 +2,14 @@ package main import ( "strings" + "time" +) + +const ( + // HTTPTimeout is the timeout in seconds for process-agent to send process payloads to DataDog + HTTPTimeout = 20 * time.Second + // ReqCtxTimeout is the timeout in seconds for process-agent to cancel POST request using context timeout + ReqCtxTimeout = 30 * time.Second ) // IsTimeout returns true if the error is due to reaching the timeout limit on the http.client