From 0c6e458a18ff0968aee42acffc78fd77b91d4aef Mon Sep 17 00:00:00 2001 From: Julio Guerra Date: Thu, 15 Dec 2022 16:56:55 +0100 Subject: [PATCH] internal/remoteconfig: fix http request errors --- internal/remoteconfig/remoteconfig.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/internal/remoteconfig/remoteconfig.go b/internal/remoteconfig/remoteconfig.go index 582aa636bf..fe3e36f4e5 100644 --- a/internal/remoteconfig/remoteconfig.go +++ b/internal/remoteconfig/remoteconfig.go @@ -21,6 +21,7 @@ import ( rc "github.com/DataDog/datadog-agent/pkg/remoteconfig/state" "gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig" + "gopkg.in/DataDog/dd-trace-go.v1/internal/log" "gopkg.in/DataDog/dd-trace-go.v1/internal/version" ) @@ -148,19 +149,19 @@ func (c *Client) Stop() { func (c *Client) updateState() { data, err := c.newUpdateRequest() if err != nil { - c.lastError = err + log.Error("remoteconfig: unexpected error while creating a new update request payload: %v", err) return } req, err := http.NewRequest(http.MethodGet, c.endpoint, &data) if err != nil { - c.lastError = err + log.Error("remoteconfig: unexpected error while creating a new http request: %v", err) return } resp, err := c.HTTP.Do(req) if err != nil { - c.lastError = err + log.Debug("remoteconfig: http request error: %v", err) return } // Flush and close the response body when returning (cf. https://pkg.go.dev/net/http#Client.Do) @@ -169,15 +170,18 @@ func (c *Client) updateState() { resp.Body.Close() }() + if sc := resp.StatusCode; sc != http.StatusOK { + log.Debug("remoteconfig: http request error: response status code is not 200 (OK) but %s", http.StatusText(sc)) + return + } + var update clientGetConfigsResponse - err = json.NewDecoder(resp.Body).Decode(&update) - if err != nil { - c.lastError = err + if err := json.NewDecoder(resp.Body).Decode(&update); err != nil { + log.Error("remoteconfig: http request: could not parse the json response body: %v", err) return } - err = c.applyUpdate(&update) - c.lastError = err + c.lastError = c.applyUpdate(&update) } // RegisterCallback allows registering a callback that will be invoked when the client