Skip to content

Commit a4d0950

Browse files
refactor(analytics): align service execution and persistence to 15m boundaries (#306)
1 parent d01e238 commit a4d0950

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

internal/analytics/service.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,29 @@ func NewService(thanos ThanosSource, store DataStore, resources ResourceProvider
8282
func (s *Service) Start(ctx context.Context) error {
8383
telemetry.Info("service_started", "interval", Interval.String())
8484

85-
// 3. Main Loop
85+
// Run immediate first batch
86+
s.RunBatch(ctx)
87+
88+
// Calculate time until next interval boundary
89+
now := time.Now().UTC()
90+
next := now.Truncate(Interval).Add(Interval)
91+
sleepDuration := next.Sub(now)
92+
93+
telemetry.Info("waiting_for_time_boundary", "sleep", sleepDuration.String(), "next_run", next.Format(time.RFC3339))
94+
95+
// Wait for the boundary or shutdown
96+
select {
97+
case <-time.After(sleepDuration):
98+
case <-ctx.Done():
99+
telemetry.Info("service_shutting_down")
100+
return nil
101+
}
102+
103+
// Now we are aligned, start the regular ticker
86104
ticker := time.NewTicker(Interval)
87105
defer ticker.Stop()
88106

89-
// Run immediate first batch
107+
// Run the first aligned batch immediately
90108
s.RunBatch(ctx)
91109

92110
for {
@@ -122,7 +140,7 @@ func (s *Service) RunBatch(ctx context.Context) {
122140

123141
telemetry.AddInt64Counter(ctx, collTotal, 1)
124142

125-
end := time.Now().UTC().Truncate(time.Minute)
143+
end := time.Now().UTC().Truncate(Interval)
126144
start := end.Add(-Interval)
127145

128146
// 1. Get Host Metadata directly from mounted host files

makefiles/k3s.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ build-postgres:
3838

3939
k3s-analytics-up:
4040
@echo "Regenerating Analytics manifest..."
41-
$(NIX_RUN) "helm template analytics k3s/analytics -f k3s/analytics/values.yaml --namespace $(NS) > k3s/analytics/manifest.yaml"
41+
helm template analytics k3s/analytics -f k3s/analytics/values.yaml --namespace $(NS) > k3s/analytics/manifest.yaml
4242
@echo "Deploying Analytics..."
4343
@$(KC) apply -f k3s/analytics/manifest.yaml
4444
@$(KC) rollout restart daemonset/analytics

0 commit comments

Comments
 (0)