Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor metrics tests #200

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions pkg/internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,24 @@ var _ = Describe("controller", func() {

Context("should update prometheus metrics", func() {
It("should requeue a Request if there is an error and continue processing items", func(done Done) {
ctrlmetrics.QueueLength = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "controller_runtime_reconcile_queue_length",
Help: "Length of reconcile queue per controller",
}, []string{"controller"})
ctrlmetrics.ReconcileErrors = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "controller_runtime_reconcile_errors_total",
Help: "Total number of reconcile errors per controller",
}, []string{"controller"})
var queueLength, reconcileErrs dto.Metric
ctrlmetrics.QueueLength.Reset()
Expect(func() error {
ctrlmetrics.QueueLength.WithLabelValues(ctrl.Name).Write(&queueLength)
if queueLength.GetGauge().GetValue() != 0.0 {
return fmt.Errorf("metrics not reset")
}
return nil
}()).Should(Succeed())

ctrlmetrics.ReconcileErrors.Reset()
Expect(func() error {
ctrlmetrics.ReconcileErrors.WithLabelValues(ctrl.Name).Write(&reconcileErrs)
if reconcileErrs.GetCounter().GetValue() != 0.0 {
return fmt.Errorf("metrics not reset")
}
return nil
}()).Should(Succeed())

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

By("Invoking Reconciler which will give an error")
Expect(<-reconciled).To(Equal(request))
var queueLength, reconcileErrs dto.Metric
Eventually(func() error {
ctrlmetrics.QueueLength.WithLabelValues(ctrl.Name).Write(&queueLength)
if queueLength.GetGauge().GetValue() != 1.0 {
Expand Down Expand Up @@ -459,10 +468,18 @@ var _ = Describe("controller", func() {
}, 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"})
var reconcileTime dto.Metric
ctrlmetrics.ReconcileTime.Reset()

Expect(func() error {
histObserver := ctrlmetrics.ReconcileTime.WithLabelValues(ctrl.Name)
hist := histObserver.(prometheus.Histogram)
hist.Write(&reconcileTime)
if reconcileTime.GetHistogram().GetSampleCount() != uint64(0) {
return fmt.Errorf("metrics not reset")
}
return nil
}()).Should(Succeed())

go func() {
defer GinkgoRecover()
Expand All @@ -477,12 +494,11 @@ var _ = Describe("controller", func() {
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 {
histObserver := ctrlmetrics.ReconcileTime.WithLabelValues(ctrl.Name)
hist := histObserver.(prometheus.Histogram)
hist.Write(&reconcileTime)
if reconcileTime.GetHistogram().GetSampleCount() != uint64(1) {
if reconcileTime.GetHistogram().GetSampleCount() == uint64(0) {
return fmt.Errorf("metrics not updated")
}
return nil
Expand Down