Skip to content

fix(agent): check agent metadata every second instead of minute #8614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func New(options Options) Agent {
}
}
if options.ReportMetadataInterval == 0 {
options.ReportMetadataInterval = 1 * time.Minute
options.ReportMetadataInterval = time.Second
}
if options.ServiceBannerRefreshInterval == 0 {
options.ServiceBannerRefreshInterval = 2 * time.Minute
Expand Down Expand Up @@ -339,15 +339,19 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
// baseInterval to run.
flight := trySingleflight{m: map[string]struct{}{}}

postMetadata := func(mr metadataResultAndKey) {
err := a.client.PostMetadata(ctx, mr.key, *mr.result)
if err != nil {
a.logger.Error(ctx, "agent failed to report metadata", slog.Error(err))
}
}

for {
select {
case <-ctx.Done():
return
case mr := <-metadataResults:
err := a.client.PostMetadata(ctx, mr.key, *mr.result)
if err != nil {
a.logger.Error(ctx, "agent failed to report metadata", slog.Error(err))
}
postMetadata(mr)
continue
case <-baseTicker.C:
}
Expand Down Expand Up @@ -409,8 +413,14 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
if md.Interval == 0 {
return
}
intervalUnit := time.Second
// reportMetadataInterval is only less than a second in tests,
// so adjust the interval unit for them.
if a.reportMetadataInterval < time.Second {
intervalUnit = 100 * time.Millisecond
}
// The last collected value isn't quite stale yet, so we skip it.
if collectedAt.Add(a.reportMetadataInterval).After(time.Now()) {
if collectedAt.Add(time.Duration(md.Interval) * intervalUnit).After(time.Now()) {
return
}
}
Expand Down