@@ -108,7 +108,7 @@ func New(options Options) Agent {
108
108
}
109
109
}
110
110
if options .ReportMetadataInterval == 0 {
111
- options .ReportMetadataInterval = 1 * time .Minute
111
+ options .ReportMetadataInterval = time .Second
112
112
}
113
113
if options .ServiceBannerRefreshInterval == 0 {
114
114
options .ServiceBannerRefreshInterval = 2 * time .Minute
@@ -328,16 +328,45 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
328
328
// baseInterval to run.
329
329
var flight trySingleflight
330
330
331
+ postMetadata := func (mr metadataResultAndKey ) {
332
+ lastCollectedAts [mr .key ] = mr .result .CollectedAt
333
+ err := a .client .PostMetadata (ctx , mr .key , * mr .result )
334
+ if err != nil {
335
+ a .logger .Error (ctx , "agent failed to report metadata" , slog .Error (err ))
336
+ }
337
+ }
338
+ flushAllMetadata := func () {
339
+ wg := sync.WaitGroup {}
340
+ defer wg .Wait ()
341
+ for {
342
+ select {
343
+ case <- ctx .Done ():
344
+ return
345
+ case mr := <- metadataResults :
346
+ wg .Add (1 )
347
+ go func () {
348
+ defer wg .Done ()
349
+ postMetadata (mr )
350
+ }()
351
+ continue
352
+ default :
353
+ return
354
+ }
355
+ }
356
+ }
357
+
331
358
for {
359
+ // Ensure all backpressured metadata is posted.
360
+ if len (metadataResults ) > 1 {
361
+ flushAllMetadata ()
362
+ }
363
+
332
364
select {
333
365
case <- ctx .Done ():
334
366
return
335
367
case mr := <- metadataResults :
336
- lastCollectedAts [mr .key ] = mr .result .CollectedAt
337
- err := a .client .PostMetadata (ctx , mr .key , * mr .result )
338
- if err != nil {
339
- a .logger .Error (ctx , "agent failed to report metadata" , slog .Error (err ))
340
- }
368
+ postMetadata (mr )
369
+ continue
341
370
case <- baseTicker .C :
342
371
}
343
372
@@ -386,8 +415,15 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
386
415
if md .Interval == 0 {
387
416
continue
388
417
}
418
+
419
+ intervalUnit := time .Second
420
+ // reportMetadataInterval is only less than a second in tests,
421
+ // so adjust the interval unit for them.
422
+ if a .reportMetadataInterval < time .Second {
423
+ intervalUnit = 100 * time .Millisecond
424
+ }
389
425
// The last collected value isn't quite stale yet, so we skip it.
390
- if collectedAt .Add (a . reportMetadataInterval ).After (time .Now ()) {
426
+ if collectedAt .Add (time . Duration ( md . Interval ) * intervalUnit ).After (time .Now ()) {
391
427
continue
392
428
}
393
429
}
@@ -399,11 +435,19 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
399
435
go flight .Do (md .Key , func () {
400
436
timeout := md .Timeout
401
437
if timeout == 0 {
402
- timeout = md .Interval
438
+ if md .Interval != 0 {
439
+ timeout = md .Interval
440
+ } else if interval := int64 (a .reportMetadataInterval .Seconds ()); interval != 0 {
441
+ // Fallback to the report interval
442
+ timeout = interval
443
+ } else {
444
+ // If the interval is still 0 (possible if the interval
445
+ // is less than a second), default to 5. This was
446
+ // randomly picked.
447
+ timeout = 5
448
+ }
403
449
}
404
- ctx , cancel := context .WithTimeout (ctx ,
405
- time .Duration (timeout )* time .Second ,
406
- )
450
+ ctx , cancel := context .WithTimeout (ctx , time .Duration (timeout )* time .Second )
407
451
defer cancel ()
408
452
409
453
select {
0 commit comments