Skip to content

Commit acbdf64

Browse files
committed
feat: initialize reconciler metrics when controller is started
1 parent 66537ca commit acbdf64

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

pkg/internal/controller/controller.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ func (c *Controller) Start(ctx context.Context) error {
143143
return errors.New("controller was started more than once. This is likely to be caused by being added to a manager multiple times")
144144
}
145145

146+
c.initMetrics()
147+
146148
// Set the internal context.
147149
c.ctx = ctx
148150

@@ -202,7 +204,6 @@ func (c *Controller) Start(ctx context.Context) error {
202204

203205
// Launch workers to process resources
204206
c.Log.Info("Starting workers", "worker count", c.MaxConcurrentReconciles)
205-
ctrlmetrics.WorkerCount.WithLabelValues(c.Name).Set(float64(c.MaxConcurrentReconciles))
206207
for i := 0; i < c.MaxConcurrentReconciles; i++ {
207208
go wait.UntilWithContext(ctx, func(ctx context.Context) {
208209
// 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 {
248249
return true
249250
}
250251

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+
251269
func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
252270
// Update metrics after processing each item
253271
reconcileStartTS := time.Now()
@@ -275,7 +293,7 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
275293
if result, err := c.Do.Reconcile(ctx, req); err != nil {
276294
c.Queue.AddRateLimited(req)
277295
ctrlmetrics.ReconcileErrors.WithLabelValues(c.Name).Inc()
278-
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, "error").Inc()
296+
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, labelError).Inc()
279297
log.Error(err, "Reconciler error")
280298
return
281299
} else if result.RequeueAfter > 0 {
@@ -285,19 +303,19 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
285303
// to result.RequestAfter
286304
c.Queue.Forget(obj)
287305
c.Queue.AddAfter(req, result.RequeueAfter)
288-
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, "requeue_after").Inc()
306+
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, labelRequeueAfter).Inc()
289307
return
290308
} else if result.Requeue {
291309
c.Queue.AddRateLimited(req)
292-
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, "requeue").Inc()
310+
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, labelRequeue).Inc()
293311
return
294312
}
295313

296314
// Finally, if no error occurs we Forget this item so it does not
297315
// get queued again until another change happens.
298316
c.Queue.Forget(obj)
299317

300-
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, "success").Inc()
318+
ctrlmetrics.ReconcileTotal.WithLabelValues(c.Name, labelSuccess).Inc()
301319
}
302320

303321
// GetLogger returns this controller's logger.

0 commit comments

Comments
 (0)