Skip to content

fix(prometheusmetrics): ensure periodic metrics tick on startup #7585

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 2 commits into from
Jun 2, 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
62 changes: 36 additions & 26 deletions coderd/prometheusmetrics/prometheusmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,32 @@ func Workspaces(ctx context.Context, registerer prometheus.Registerer, db databa
ctx, cancelFunc := context.WithCancel(ctx)
done := make(chan struct{})

ticker := time.NewTicker(duration)
// Use time.Nanosecond to force an initial tick. It will be reset to the
// correct duration after executing once.
ticker := time.NewTicker(time.Nanosecond)
doTick := func() {
defer ticker.Reset(duration)
Comment on lines +103 to +105
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick!


builds, err := db.GetLatestWorkspaceBuilds(ctx)
if err != nil {
return
}
jobIDs := make([]uuid.UUID, 0, len(builds))
for _, build := range builds {
jobIDs = append(jobIDs, build.JobID)
}
jobs, err := db.GetProvisionerJobsByIDs(ctx, jobIDs)
if err != nil {
return
}

gauge.Reset()
for _, job := range jobs {
status := db2sdk.ProvisionerJobStatus(job)
gauge.WithLabelValues(string(status)).Add(1)
}
}

go func() {
defer close(done)
defer ticker.Stop()
Expand All @@ -107,25 +132,7 @@ func Workspaces(ctx context.Context, registerer prometheus.Registerer, db databa
case <-ctx.Done():
return
case <-ticker.C:
}

builds, err := db.GetLatestWorkspaceBuilds(ctx)
if err != nil {
continue
}
jobIDs := make([]uuid.UUID, 0, len(builds))
for _, build := range builds {
jobIDs = append(jobIDs, build.JobID)
}
jobs, err := db.GetProvisionerJobsByIDs(ctx, jobIDs)
if err != nil {
continue
}

gauge.Reset()
for _, job := range jobs {
status := db2sdk.ProvisionerJobStatus(job)
gauge.WithLabelValues(string(status)).Add(1)
doTick()
}
}
}()
Expand Down Expand Up @@ -202,7 +209,9 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis
ctx = dbauthz.AsSystemRestricted(ctx)
done := make(chan struct{})

ticker := time.NewTicker(duration)
// Use time.Nanosecond to force an initial tick. It will be reset to the
// correct duration after executing once.
ticker := time.NewTicker(time.Nanosecond)
go func() {
defer close(done)
defer ticker.Stop()
Expand Down Expand Up @@ -306,8 +315,7 @@ func Agents(ctx context.Context, logger slog.Logger, registerer prometheus.Regis

done:
logger.Debug(ctx, "Agent metrics collection is done")
metricsCollectorAgents.Observe(timer.ObserveDuration().Seconds())

timer.ObserveDuration()
ticker.Reset(duration)
}
}()
Expand Down Expand Up @@ -426,7 +434,9 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
done := make(chan struct{})

createdAfter := initialCreateAfter
ticker := time.NewTicker(duration)
// Use time.Nanosecond to force an initial tick. It will be reset to the
// correct duration after executing once.
ticker := time.NewTicker(time.Nanosecond)
go func() {
defer close(done)
defer ticker.Stop()
Expand Down Expand Up @@ -472,8 +482,8 @@ func AgentStats(ctx context.Context, logger slog.Logger, registerer prometheus.R
}
}

logger.Debug(ctx, "Agent metrics collection is done")
metricsCollectorAgentStats.Observe(timer.ObserveDuration().Seconds())
logger.Debug(ctx, "Agent metrics collection is done", slog.F("len", len(stats)))
timer.ObserveDuration()

createdAfter = checkpoint
ticker.Reset(duration)
Expand Down