Skip to content

Commit fbdda3b

Browse files
committed
Add reconcile errors counter
1 parent ba1bb59 commit fbdda3b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

pkg/internal/controller/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func (c *Controller) processNextWorkItem() bool {
211211
if result, err := c.Do.Reconcile(req); err != nil {
212212
c.Queue.AddRateLimited(req)
213213
log.Error(err, "Reconciler error", "Controller", c.Name, "Request", req)
214+
ctrlmetrics.ReconcileErrors.WithLabelValues(c.Name).Inc()
214215

215216
return false
216217
} else if result.RequeueAfter > 0 {

pkg/internal/controller/controller_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ var _ = Describe("controller", func() {
414414
Name: "controller_runtime_reconcile_queue_length",
415415
Help: "Length of reconcile queue per controller",
416416
}, []string{"controller"})
417+
ctrlmetrics.ReconcileErrors = prometheus.NewCounterVec(prometheus.CounterOpts{
418+
Name: "controller_runtime_reconcile_errors_total",
419+
Help: "Total number of reconcile errors per controller",
420+
}, []string{"controller"})
417421

418422
fakeReconcile.Err = fmt.Errorf("expected error: reconcile")
419423
go func() {
@@ -427,14 +431,21 @@ var _ = Describe("controller", func() {
427431

428432
By("Invoking Reconciler which will give an error")
429433
Expect(<-reconciled).To(Equal(request))
430-
var queueLength dto.Metric
434+
var queueLength, reconcileErrs dto.Metric
431435
Eventually(func() error {
432436
ctrlmetrics.QueueLength.WithLabelValues(ctrl.Name).Write(&queueLength)
433437
if queueLength.GetGauge().GetValue() != 1.0 {
434438
return fmt.Errorf("metrics not updated")
435439
}
436440
return nil
437441
}, 2.0).Should(Succeed())
442+
Eventually(func() error {
443+
ctrlmetrics.ReconcileErrors.WithLabelValues(ctrl.Name).Write(&reconcileErrs)
444+
if reconcileErrs.GetCounter().GetValue() != 1.0 {
445+
return fmt.Errorf("metrics not updated")
446+
}
447+
return nil
448+
}, 2.0).Should(Succeed())
438449

439450
By("Invoking Reconciler a second time without error")
440451
fakeReconcile.Err = nil

pkg/internal/controller/metrics/metrics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ var (
2828
Name: "controller_runtime_reconcile_queue_length",
2929
Help: "Length of reconcile queue per controller",
3030
}, []string{"controller"})
31+
32+
// ReconcileErrors is a prometheus counter metrics which holds the total
33+
// number of errors from the Reconciler
34+
ReconcileErrors = prometheus.NewCounterVec(prometheus.CounterOpts{
35+
Name: "controller_runtime_reconcile_errors_total",
36+
Help: "Total number of reconcile errors per controller",
37+
}, []string{"controller"})
3138
)
3239

3340
func init() {
3441
metrics.Registry.MustRegister(QueueLength)
42+
metrics.Registry.MustRegister(ReconcileErrors)
3543
}

0 commit comments

Comments
 (0)