File tree Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Expand file tree Collapse file tree 3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -173,7 +173,8 @@ func (c *Controller) processNextWorkItem() bool {
173
173
// This code copy-pasted from the sample-Controller.
174
174
175
175
// Update metrics after processing each item
176
- defer c .updateMetrics ()
176
+ reconcileStartTS := time .Now ()
177
+ defer c .updateMetrics (time .Now ().Sub (reconcileStartTS ))
177
178
178
179
obj , shutdown := c .Queue .Get ()
179
180
if obj == nil {
@@ -240,6 +241,7 @@ func (c *Controller) InjectFunc(f inject.Func) error {
240
241
}
241
242
242
243
// updateMetrics updates prometheus metrics within the controller
243
- func (c * Controller ) updateMetrics () {
244
+ func (c * Controller ) updateMetrics (reconcileTime time. Duration ) {
244
245
ctrlmetrics .QueueLength .WithLabelValues (c .Name ).Set (float64 (c .Queue .Len ()))
246
+ ctrlmetrics .ReconcileTime .WithLabelValues (c .Name ).Observe (reconcileTime .Seconds ())
245
247
}
Original file line number Diff line number Diff line change @@ -457,6 +457,37 @@ var _ = Describe("controller", func() {
457
457
458
458
close (done )
459
459
}, 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 )
460
491
})
461
492
})
462
493
})
Original file line number Diff line number Diff line change 35
35
Name : "controller_runtime_reconcile_errors_total" ,
36
36
Help : "Total number of reconcile errors per controller" ,
37
37
}, []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" })
38
45
)
39
46
40
47
func init () {
41
48
metrics .Registry .MustRegister (QueueLength )
42
49
metrics .Registry .MustRegister (ReconcileErrors )
50
+ metrics .Registry .MustRegister (ReconcileTime )
43
51
}
You can’t perform that action at this time.
0 commit comments