Skip to content

Commit

Permalink
Add reconcile time histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Sep 17, 2018
1 parent fefc0f7 commit 803aa68
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ func (c *Controller) processNextWorkItem() bool {
// This code copy-pasted from the sample-Controller.

// Update metrics after processing each item
defer c.updateMetrics()
reconcileStartTS := time.Now()
defer c.updateMetrics(time.Now().Sub(reconcileStartTS))

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

// updateMetrics updates prometheus metrics within the controller
func (c *Controller) updateMetrics() {
func (c *Controller) updateMetrics(reconcileTime time.Duration) {
ctrlmetrics.QueueLength.WithLabelValues(c.Name).Set(float64(c.Queue.Len()))
ctrlmetrics.ReconcileTime.WithLabelValues(c.Name).Observe(reconcileTime.Seconds())
}
31 changes: 31 additions & 0 deletions pkg/internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,37 @@ var _ = Describe("controller", func() {

close(done)
}, 2.0)

It("should add a reconcile time to the reconcile time histogram", func(done Done) {
ctrlmetrics.ReconcileTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "controller_runtime_reconcile_time_second",
Help: "Length of time per reconcile per controller",
}, []string{"controller"})

go func() {
defer GinkgoRecover()
Expect(ctrl.Start(stop)).NotTo(HaveOccurred())
}()
ctrl.Queue.Add(request)

By("Invoking Reconciler")
Expect(<-reconciled).To(Equal(request))

By("Removing the item from the queue")
Eventually(ctrl.Queue.Len).Should(Equal(0))
Eventually(func() int { return ctrl.Queue.NumRequeues(request) }).Should(Equal(0))

var reconcileTime dto.Metric
Eventually(func() error {
ctrlmetrics.ReconcileTime.WithLabelValues(ctrl.Name).Write(&reconcileTime)
if reconcileTime.GetHistogram().GetSampleCount() != uint64(1) {
return fmt.Errorf("metrics not updated")
}
return nil
}, 2.0).Should(Succeed())

close(done)
})
})
})
})
Expand Down
6 changes: 6 additions & 0 deletions pkg/internal/controller/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ var (
Name: "controller_runtime_reconcile_errors_total",
Help: "Total number of reconcile errors per controller",
}, []string{"controller"})

ReconcileTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "controller_runtime_reconcile_time_second",
Help: "Length of time per reconcile per controller",
}, []string{"controller"})
)

func init() {
metrics.Registry.MustRegister(QueueLength)
metrics.Registry.MustRegister(ReconcileErrors)
metrics.Registry.MustRegister(ReconcileTime)
}

0 comments on commit 803aa68

Please sign in to comment.