@@ -143,6 +143,8 @@ func (c *Controller) Start(ctx context.Context) error {
143
143
return errors .New ("controller was started more than once. This is likely to be caused by being added to a manager multiple times" )
144
144
}
145
145
146
+ c .initMetrics ()
147
+
146
148
// Set the internal context.
147
149
c .ctx = ctx
148
150
@@ -202,7 +204,6 @@ func (c *Controller) Start(ctx context.Context) error {
202
204
203
205
// Launch workers to process resources
204
206
c .Log .Info ("Starting workers" , "worker count" , c .MaxConcurrentReconciles )
205
- ctrlmetrics .WorkerCount .WithLabelValues (c .Name ).Set (float64 (c .MaxConcurrentReconciles ))
206
207
for i := 0 ; i < c .MaxConcurrentReconciles ; i ++ {
207
208
go wait .UntilWithContext (ctx , func (ctx context.Context ) {
208
209
// Run a worker thread that just dequeues items, processes them, and marks them done.
@@ -248,6 +249,23 @@ func (c *Controller) processNextWorkItem(ctx context.Context) bool {
248
249
return true
249
250
}
250
251
252
+ const (
253
+ labelError = "error"
254
+ labelRequeueAfter = "requeue_after"
255
+ labelRequeue = "requeue"
256
+ labelSuccess = "success"
257
+ )
258
+
259
+ func (c * Controller ) initMetrics () {
260
+ ctrlmetrics .ActiveWorkers .WithLabelValues (c .Name ).Set (0 )
261
+ ctrlmetrics .ReconcileErrors .WithLabelValues (c .Name ).Add (0 )
262
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelError ).Add (0 )
263
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelRequeueAfter ).Add (0 )
264
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelRequeue ).Add (0 )
265
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelSuccess ).Add (0 )
266
+ ctrlmetrics .WorkerCount .WithLabelValues (c .Name ).Set (float64 (c .MaxConcurrentReconciles ))
267
+ }
268
+
251
269
func (c * Controller ) reconcileHandler (ctx context.Context , obj interface {}) {
252
270
// Update metrics after processing each item
253
271
reconcileStartTS := time .Now ()
@@ -275,7 +293,7 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
275
293
if result , err := c .Do .Reconcile (ctx , req ); err != nil {
276
294
c .Queue .AddRateLimited (req )
277
295
ctrlmetrics .ReconcileErrors .WithLabelValues (c .Name ).Inc ()
278
- ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "error" ).Inc ()
296
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelError ).Inc ()
279
297
log .Error (err , "Reconciler error" )
280
298
return
281
299
} else if result .RequeueAfter > 0 {
@@ -285,19 +303,19 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
285
303
// to result.RequestAfter
286
304
c .Queue .Forget (obj )
287
305
c .Queue .AddAfter (req , result .RequeueAfter )
288
- ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "requeue_after" ).Inc ()
306
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelRequeueAfter ).Inc ()
289
307
return
290
308
} else if result .Requeue {
291
309
c .Queue .AddRateLimited (req )
292
- ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "requeue" ).Inc ()
310
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelRequeue ).Inc ()
293
311
return
294
312
}
295
313
296
314
// Finally, if no error occurs we Forget this item so it does not
297
315
// get queued again until another change happens.
298
316
c .Queue .Forget (obj )
299
317
300
- ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , "success" ).Inc ()
318
+ ctrlmetrics .ReconcileTotal .WithLabelValues (c .Name , labelSuccess ).Inc ()
301
319
}
302
320
303
321
// GetLogger returns this controller's logger.
0 commit comments