@@ -12,6 +12,7 @@ import (
12
12
"cdr.dev/slog"
13
13
14
14
"github.com/coder/coder/v2/coderd/database"
15
+ "github.com/coder/coder/v2/coderd/database/dbtime"
15
16
"github.com/coder/coder/v2/coderd/prebuilds"
16
17
)
17
18
@@ -75,7 +76,7 @@ type MetricsCollector struct {
75
76
logger slog.Logger
76
77
snapshotter prebuilds.StateSnapshotter
77
78
78
- latestState atomic.Pointer [state ]
79
+ latestState atomic.Pointer [metricsState ]
79
80
}
80
81
81
82
var _ prometheus.Collector = new (MetricsCollector )
@@ -106,6 +107,7 @@ func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
106
107
currentState := mc .latestState .Load () // Grab a copy; it's ok if it goes stale during the course of this func.
107
108
if currentState == nil {
108
109
mc .logger .Warn (context .Background (), "failed to set prebuilds metrics; state not set" )
110
+ metricsCh <- prometheus .MustNewConstMetric (lastUpdateDesc , prometheus .GaugeValue , 0 )
109
111
return
110
112
}
111
113
@@ -135,7 +137,7 @@ func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
135
137
metricsCh <- prometheus .MustNewConstMetric (lastUpdateDesc , prometheus .GaugeValue , float64 (currentState .createdAt .Unix ()))
136
138
}
137
139
138
- type state struct {
140
+ type metricsState struct {
139
141
prebuildMetrics []database.GetPrebuildMetricsRow
140
142
snapshot * prebuilds.GlobalSnapshot
141
143
createdAt time.Time
@@ -164,7 +166,6 @@ func (mc *MetricsCollector) BackgroundFetch(ctx context.Context, updateInterval,
164
166
// UpdateState builds the current metrics state.
165
167
func (mc * MetricsCollector ) UpdateState (ctx context.Context , timeout time.Duration ) error {
166
168
start := time .Now ()
167
- mc .logger .Debug (ctx , "fetching prebuilds metrics state" )
168
169
fetchCtx , fetchCancel := context .WithTimeout (ctx , timeout )
169
170
defer fetchCancel ()
170
171
@@ -179,10 +180,10 @@ func (mc *MetricsCollector) UpdateState(ctx context.Context, timeout time.Durati
179
180
}
180
181
mc .logger .Debug (ctx , "fetched prebuilds metrics state" , slog .F ("duration_secs" , fmt .Sprintf ("%.2f" , time .Since (start ).Seconds ())))
181
182
182
- mc .latestState .Store (& state {
183
+ mc .latestState .Store (& metricsState {
183
184
prebuildMetrics : prebuildMetrics ,
184
185
snapshot : snapshot ,
185
- createdAt : time .Now (),
186
+ createdAt : dbtime .Now (),
186
187
})
187
188
return nil
188
189
}
0 commit comments