Skip to content

Commit cb7fc8e

Browse files
committed
Add reconcile time histogram
1 parent fbdda3b commit cb7fc8e

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

pkg/internal/controller/controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ func (c *Controller) processNextWorkItem() bool {
173173
// This code copy-pasted from the sample-Controller.
174174

175175
// Update metrics after processing each item
176-
defer c.updateMetrics()
176+
reconcileStartTS := time.Now()
177+
defer c.updateMetrics(time.Now().Sub(reconcileStartTS))
177178

178179
obj, shutdown := c.Queue.Get()
179180
if obj == nil {
@@ -240,6 +241,7 @@ func (c *Controller) InjectFunc(f inject.Func) error {
240241
}
241242

242243
// updateMetrics updates prometheus metrics within the controller
243-
func (c *Controller) updateMetrics() {
244+
func (c *Controller) updateMetrics(reconcileTime time.Duration) {
244245
ctrlmetrics.QueueLength.WithLabelValues(c.Name).Set(float64(c.Queue.Len()))
246+
ctrlmetrics.ReconcileTime.WithLabelValues(c.Name).Observe(reconcileTime.Seconds())
245247
}

pkg/internal/controller/controller_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,37 @@ var _ = Describe("controller", func() {
457457

458458
close(done)
459459
}, 2.0)
460+
461+
It("should add a reconcile time to the reconcile time histogram", func(done Done) {
462+
ctrlmetrics.ReconcileTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
463+
Name: "controller_runtime_reconcile_time_second",
464+
Help: "Length of time per reconcile per controller",
465+
}, []string{"controller"})
466+
467+
go func() {
468+
defer GinkgoRecover()
469+
Expect(ctrl.Start(stop)).NotTo(HaveOccurred())
470+
}()
471+
ctrl.Queue.Add(request)
472+
473+
By("Invoking Reconciler")
474+
Expect(<-reconciled).To(Equal(request))
475+
476+
By("Removing the item from the queue")
477+
Eventually(ctrl.Queue.Len).Should(Equal(0))
478+
Eventually(func() int { return ctrl.Queue.NumRequeues(request) }).Should(Equal(0))
479+
480+
var reconcileTime dto.Metric
481+
Eventually(func() error {
482+
ctrlmetrics.ReconcileTime.WithLabelValues(ctrl.Name).Write(&reconcileTime)
483+
if reconcileTime.GetHistogram().GetSampleCount() != uint64(1) {
484+
return fmt.Errorf("metrics not updated")
485+
}
486+
return nil
487+
}, 2.0).Should(Succeed())
488+
489+
close(done)
490+
}, 4.0)
460491
})
461492
})
462493
})

pkg/internal/controller/metrics/metrics.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@ var (
3535
Name: "controller_runtime_reconcile_errors_total",
3636
Help: "Total number of reconcile errors per controller",
3737
}, []string{"controller"})
38+
39+
// ReconcileTime is a prometheus metric which keeps track of the duration
40+
// of reconciles
41+
ReconcileTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
42+
Name: "controller_runtime_reconcile_time_seconds",
43+
Help: "Length of time per reconcile per controller",
44+
}, []string{"controller"})
3845
)
3946

4047
func init() {
4148
metrics.Registry.MustRegister(QueueLength)
4249
metrics.Registry.MustRegister(ReconcileErrors)
50+
metrics.Registry.MustRegister(ReconcileTime)
4351
}

0 commit comments

Comments
 (0)