Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
chore: review feedback
Signed-off-by: Danny Kopping <[email protected]>
  • Loading branch information
dannykopping committed May 13, 2025
commit 35cebf6c5fc314d26de89459a9308f2e1c2452ca
11 changes: 6 additions & 5 deletions enterprise/coderd/prebuilds/metricscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cdr.dev/slog"

Comment thread
dannykopping marked this conversation as resolved.
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/prebuilds"
)

Expand Down Expand Up @@ -75,7 +76,7 @@ type MetricsCollector struct {
logger slog.Logger
snapshotter prebuilds.StateSnapshotter

latestState atomic.Pointer[state]
latestState atomic.Pointer[metricsState]
}

var _ prometheus.Collector = new(MetricsCollector)
Expand Down Expand Up @@ -106,6 +107,7 @@ func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
currentState := mc.latestState.Load() // Grab a copy; it's ok if it goes stale during the course of this func.
if currentState == nil {
mc.logger.Warn(context.Background(), "failed to set prebuilds metrics; state not set")
Comment thread
dannykopping marked this conversation as resolved.
metricsCh <- prometheus.MustNewConstMetric(lastUpdateDesc, prometheus.GaugeValue, 0)
return
}

Expand Down Expand Up @@ -135,7 +137,7 @@ func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
metricsCh <- prometheus.MustNewConstMetric(lastUpdateDesc, prometheus.GaugeValue, float64(currentState.createdAt.Unix()))
}

type state struct {
type metricsState struct {
prebuildMetrics []database.GetPrebuildMetricsRow
snapshot *prebuilds.GlobalSnapshot
createdAt time.Time
Expand Down Expand Up @@ -164,7 +166,6 @@ func (mc *MetricsCollector) BackgroundFetch(ctx context.Context, updateInterval,
// UpdateState builds the current metrics state.
func (mc *MetricsCollector) UpdateState(ctx context.Context, timeout time.Duration) error {
start := time.Now()
mc.logger.Debug(ctx, "fetching prebuilds metrics state")
fetchCtx, fetchCancel := context.WithTimeout(ctx, timeout)
defer fetchCancel()

Expand All @@ -179,10 +180,10 @@ func (mc *MetricsCollector) UpdateState(ctx context.Context, timeout time.Durati
}
mc.logger.Debug(ctx, "fetched prebuilds metrics state", slog.F("duration_secs", fmt.Sprintf("%.2f", time.Since(start).Seconds())))

mc.latestState.Store(&state{
mc.latestState.Store(&metricsState{
prebuildMetrics: prebuildMetrics,
snapshot: snapshot,
createdAt: time.Now(),
createdAt: dbtime.Now(),
})
return nil
}
12 changes: 7 additions & 5 deletions enterprise/coderd/prebuilds/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ func NewStoreReconciler(store database.Store,
provisionNotifyCh: make(chan database.ProvisionerJob, 10),
}

reconciler.metrics = NewMetricsCollector(store, logger, reconciler)
if err := registerer.Register(reconciler.metrics); err != nil {
// If the registerer fails to register the metrics collector, it's not fatal.
logger.Error(context.Background(), "failed to register prometheus metrics", slog.Error(err))
if registerer != nil {
reconciler.metrics = NewMetricsCollector(store, logger, reconciler)
if err := registerer.Register(reconciler.metrics); err != nil {
// If the registerer fails to register the metrics collector, it's not fatal.
logger.Error(context.Background(), "failed to register prometheus metrics", slog.Error(err))
}
}

return reconciler
Expand All @@ -92,8 +94,8 @@ func (c *StoreReconciler) Run(ctx context.Context) {
ticker := c.clock.NewTicker(reconciliationInterval)
defer ticker.Stop()
defer func() {
c.done <- struct{}{}
wg.Wait()
c.done <- struct{}{}
}()

// nolint:gocritic // Reconciliation Loop needs Prebuilds Orchestrator permissions.
Expand Down