Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Merge branch 'main' into colin/metadata-fe-fix
  • Loading branch information
coadler committed Jul 20, 2023
commit fabacc5324020828cc6ebd7322d785b61e6d9fb5
34 changes: 10 additions & 24 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
flight := trySingleflight{m: map[string]struct{}{}}

postMetadata := func(mr metadataResultAndKey) {
lastCollectedAts[mr.key] = mr.result.CollectedAt
err := a.client.PostMetadata(ctx, mr.key, *mr.result)
if err != nil {
a.logger.Error(ctx, "agent failed to report metadata", slog.Error(err))
Expand Down Expand Up @@ -399,26 +398,6 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
// channel to synchronize the results and avoid both messy
// mutex logic and overloading the API.
for _, md := range manifest.Metadata {
collectedAt, ok := lastCollectedAts[md.Key]
if ok {
// If the interval is zero, we assume the user just wants
// a single collection at startup, not a spinning loop.
if md.Interval == 0 {
continue
}

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(time.Duration(md.Interval) * intervalUnit).After(time.Now()) {
continue
}
}

md := md
// We send the result to the channel in the goroutine to avoid
// sending the same result multiple times. So, we don't care about
Expand All @@ -434,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 All @@ -446,15 +431,16 @@ func (a *agent) reportMetadataLoop(ctx context.Context) {
timeout = md.Interval
} else if interval := int64(a.reportMetadataInterval.Seconds()); interval != 0 {
// Fallback to the report interval
timeout = interval
timeout = interval * 3
} else {
// If the interval is still 0 (possible if the interval
// is less than a second), default to 5. This was
// randomly picked.
timeout = 5
}
}
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second)
ctxTimeout := time.Duration(timeout) * time.Second
ctx, cancel := context.WithTimeout(ctx, ctxTimeout)
defer cancel()

now := time.Now()
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.