Skip to content

Commit

Permalink
internal/remoteconfig: fix http request errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio-Guerra committed Dec 16, 2022
1 parent 4012444 commit 0c6e458
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions internal/remoteconfig/remoteconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 0c6e458

Please sign in to comment.