@@ -21,6 +21,7 @@ import (
21
21
rc "github.com/DataDog/datadog-agent/pkg/remoteconfig/state"
22
22
23
23
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
24
+ "gopkg.in/DataDog/dd-trace-go.v1/internal/log"
24
25
"gopkg.in/DataDog/dd-trace-go.v1/internal/version"
25
26
)
26
27
@@ -148,19 +149,19 @@ func (c *Client) Stop() {
148
149
func (c * Client ) updateState () {
149
150
data , err := c .newUpdateRequest ()
150
151
if err != nil {
151
- c . lastError = err
152
+ log . Error ( "remoteconfig: unexpected error while creating a new update request payload: %v" , err )
152
153
return
153
154
}
154
155
155
156
req , err := http .NewRequest (http .MethodGet , c .endpoint , & data )
156
157
if err != nil {
157
- c . lastError = err
158
+ log . Error ( "remoteconfig: unexpected error while creating a new http request: %v" , err )
158
159
return
159
160
}
160
161
161
162
resp , err := c .HTTP .Do (req )
162
163
if err != nil {
163
- c . lastError = err
164
+ log . Debug ( "remoteconfig: http request error: %v" , err )
164
165
return
165
166
}
166
167
// Flush and close the response body when returning (cf. https://pkg.go.dev/net/http#Client.Do)
@@ -169,15 +170,28 @@ func (c *Client) updateState() {
169
170
resp .Body .Close ()
170
171
}()
171
172
172
- var update clientGetConfigsResponse
173
- err = json .NewDecoder (resp .Body ).Decode (& update )
173
+ if sc := resp .StatusCode ; sc != http .StatusOK {
174
+ log .Debug ("remoteconfig: http request error: response status code is not 200 (OK) but %s" , http .StatusText (sc ))
175
+ return
176
+ }
177
+
178
+ respBody , err := io .ReadAll (resp .Body )
174
179
if err != nil {
175
- c . lastError = err
180
+ log . Error ( "remoteconfig: http request error: could not read the response body: %v" , err )
176
181
return
177
182
}
178
183
179
- err = c .applyUpdate (& update )
180
- c .lastError = err
184
+ if body := string (respBody ); body == `{}` || body == `null` {
185
+ return
186
+ }
187
+
188
+ var update clientGetConfigsResponse
189
+ if err := json .Unmarshal (respBody , & update ); err != nil {
190
+ log .Error ("remoteconfig: http request error: could not parse the json response body: %v" , err )
191
+ return
192
+ }
193
+
194
+ c .lastError = c .applyUpdate (& update )
181
195
}
182
196
183
197
// RegisterCallback allows registering a callback that will be invoked when the client
@@ -199,13 +213,6 @@ func (c *Client) applyUpdate(pbUpdate *clientGetConfigsResponse) error {
199
213
}
200
214
}
201
215
202
- update := rc.Update {
203
- TUFRoots : pbUpdate .Roots ,
204
- TUFTargets : pbUpdate .Targets ,
205
- TargetFiles : fileMap ,
206
- ClientConfigs : pbUpdate .ClientConfigs ,
207
- }
208
-
209
216
mapify := func (s * rc.RepositoryState ) map [string ]string {
210
217
m := make (map [string ]string )
211
218
for i := range s .Configs {
@@ -219,30 +226,43 @@ func (c *Client) applyUpdate(pbUpdate *clientGetConfigsResponse) error {
219
226
// Check the repository state before and after the update to detect which configs are not being sent anymore.
220
227
// This is needed because some products can stop sending configurations, and we want to make sure that the subscribers
221
228
// are provided with this information in this case
222
- stateBefore , _ := c .repository .CurrentState ()
223
- products , err := c .repository .Update (update )
224
- stateAfter , _ := c .repository .CurrentState ()
229
+ stateBefore , err := c .repository .CurrentState ()
230
+ if err != nil {
231
+ return fmt .Errorf ("repository current state error: %v" , err )
232
+ }
233
+ products , err := c .repository .Update (rc.Update {
234
+ TUFRoots : pbUpdate .Roots ,
235
+ TUFTargets : pbUpdate .Targets ,
236
+ TargetFiles : fileMap ,
237
+ ClientConfigs : pbUpdate .ClientConfigs ,
238
+ })
239
+ if err != nil {
240
+ return fmt .Errorf ("repository update error: %v" , err )
241
+ }
242
+ stateAfter , err := c .repository .CurrentState ()
243
+ if err != nil {
244
+ return fmt .Errorf ("repository current state error after update: %v" , err )
245
+ }
225
246
226
247
// Create a config files diff between before/after the update to see which config files are missing
227
248
mBefore := mapify (& stateBefore )
228
- mAfter := mapify (& stateAfter )
229
- for k := range mAfter {
249
+ for k := range mapify (& stateAfter ) {
230
250
delete (mBefore , k )
231
251
}
232
252
233
253
// Set the payload data to nil for missing config files. The callbacks then can handle the nil config case to detect
234
254
// that this config will not be updated anymore.
235
- updatedProducts := make (map [string ]bool )
255
+ updatedProducts := make (map [string ]struct {} )
236
256
for path , product := range mBefore {
237
257
if productUpdates [product ] == nil {
238
258
productUpdates [product ] = make (ProductUpdate )
239
259
}
240
260
productUpdates [product ][path ] = nil
241
- updatedProducts [product ] = true
261
+ updatedProducts [product ] = struct {}{}
242
262
}
243
263
// Aggregate updated products and missing products so that callbacks get called for both
244
264
for _ , p := range products {
245
- updatedProducts [p ] = true
265
+ updatedProducts [p ] = struct {}{}
246
266
}
247
267
248
268
// Performs the callbacks registered for all updated products and update the application status in the repository
@@ -255,7 +275,7 @@ func (c *Client) applyUpdate(pbUpdate *clientGetConfigsResponse) error {
255
275
}
256
276
}
257
277
258
- return err
278
+ return nil
259
279
}
260
280
261
281
func (c * Client ) newUpdateRequest () (bytes.Buffer , error ) {
@@ -290,15 +310,18 @@ func (c *Client) newUpdateRequest() (bytes.Buffer, error) {
290
310
errMsg = c .lastError .Error ()
291
311
}
292
312
293
- pbConfigState := make ([]* configState , 0 , len (state .Configs ))
294
- for _ , f := range state .Configs {
295
- pbConfigState = append (pbConfigState , & configState {
296
- ID : f .ID ,
297
- Version : f .Version ,
298
- Product : f .Product ,
299
- ApplyState : f .ApplyStatus .State ,
300
- ApplyError : f .ApplyStatus .Error ,
301
- })
313
+ var pbConfigState []* configState
314
+ if ! hasError {
315
+ pbConfigState = make ([]* configState , 0 , len (state .Configs ))
316
+ for _ , f := range state .Configs {
317
+ pbConfigState = append (pbConfigState , & configState {
318
+ ID : f .ID ,
319
+ Version : f .Version ,
320
+ Product : f .Product ,
321
+ ApplyState : f .ApplyStatus .State ,
322
+ ApplyError : f .ApplyStatus .Error ,
323
+ })
324
+ }
302
325
}
303
326
304
327
cap := big .NewInt (0 )
0 commit comments