Skip to content

Commit 902ff11

Browse files
authored
Merge pull request #200 from pusher/flakey-metrics-tests
Refactor metrics tests
2 parents 81f67a0 + f30c3f8 commit 902ff11

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

pkg/internal/controller/controller_test.go

+31-15
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,24 @@ var _ = Describe("controller", func() {
410410

411411
Context("should update prometheus metrics", func() {
412412
It("should requeue a Request if there is an error and continue processing items", func(done Done) {
413-
ctrlmetrics.QueueLength = prometheus.NewGaugeVec(prometheus.GaugeOpts{
414-
Name: "controller_runtime_reconcile_queue_length",
415-
Help: "Length of reconcile queue per controller",
416-
}, []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"})
413+
var queueLength, reconcileErrs dto.Metric
414+
ctrlmetrics.QueueLength.Reset()
415+
Expect(func() error {
416+
ctrlmetrics.QueueLength.WithLabelValues(ctrl.Name).Write(&queueLength)
417+
if queueLength.GetGauge().GetValue() != 0.0 {
418+
return fmt.Errorf("metrics not reset")
419+
}
420+
return nil
421+
}()).Should(Succeed())
422+
423+
ctrlmetrics.ReconcileErrors.Reset()
424+
Expect(func() error {
425+
ctrlmetrics.ReconcileErrors.WithLabelValues(ctrl.Name).Write(&reconcileErrs)
426+
if reconcileErrs.GetCounter().GetValue() != 0.0 {
427+
return fmt.Errorf("metrics not reset")
428+
}
429+
return nil
430+
}()).Should(Succeed())
421431

422432
fakeReconcile.Err = fmt.Errorf("expected error: reconcile")
423433
go func() {
@@ -431,7 +441,6 @@ var _ = Describe("controller", func() {
431441

432442
By("Invoking Reconciler which will give an error")
433443
Expect(<-reconciled).To(Equal(request))
434-
var queueLength, reconcileErrs dto.Metric
435444
Eventually(func() error {
436445
ctrlmetrics.QueueLength.WithLabelValues(ctrl.Name).Write(&queueLength)
437446
if queueLength.GetGauge().GetValue() != 1.0 {
@@ -459,10 +468,18 @@ var _ = Describe("controller", func() {
459468
}, 2.0)
460469

461470
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"})
471+
var reconcileTime dto.Metric
472+
ctrlmetrics.ReconcileTime.Reset()
473+
474+
Expect(func() error {
475+
histObserver := ctrlmetrics.ReconcileTime.WithLabelValues(ctrl.Name)
476+
hist := histObserver.(prometheus.Histogram)
477+
hist.Write(&reconcileTime)
478+
if reconcileTime.GetHistogram().GetSampleCount() != uint64(0) {
479+
return fmt.Errorf("metrics not reset")
480+
}
481+
return nil
482+
}()).Should(Succeed())
466483

467484
go func() {
468485
defer GinkgoRecover()
@@ -477,12 +494,11 @@ var _ = Describe("controller", func() {
477494
Eventually(ctrl.Queue.Len).Should(Equal(0))
478495
Eventually(func() int { return ctrl.Queue.NumRequeues(request) }).Should(Equal(0))
479496

480-
var reconcileTime dto.Metric
481497
Eventually(func() error {
482498
histObserver := ctrlmetrics.ReconcileTime.WithLabelValues(ctrl.Name)
483499
hist := histObserver.(prometheus.Histogram)
484500
hist.Write(&reconcileTime)
485-
if reconcileTime.GetHistogram().GetSampleCount() != uint64(1) {
501+
if reconcileTime.GetHistogram().GetSampleCount() == uint64(0) {
486502
return fmt.Errorf("metrics not updated")
487503
}
488504
return nil

0 commit comments

Comments
 (0)