Skip to content

Commit

Permalink
metrics: tracking workflows with waiting status (#41)
Browse files Browse the repository at this point in the history
* metrics: tracking workflows with waiting status

* metrics: tracking workflows with waiting status

* fix unit tests

Signed-off-by: ibraheem Al Saady <ibraheemalsaady@ibraheems-MacBook-Pro-2.local>

Signed-off-by: ibraheem Al Saady <ibraheemalsaady@ibraheems-MacBook-Pro-2.local>
Co-authored-by: ibraheem Al Saady <ibraheemalsaady@ibraheems-MacBook-Pro-2.local>
  • Loading branch information
IbraheemAlSaady and ibraheem Al Saady authored Aug 14, 2022
1 parent dd9dfb2 commit a9a0f2d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
6 changes: 0 additions & 6 deletions config/samples/role-terraform-runner.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: terraform-runner
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
Expand Down
5 changes: 5 additions & 0 deletions controllers/terraform_controller_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ import (
func (r *TerraformReconciler) updateRunStatus(ctx context.Context, run *v1alpha1.Terraform, status v1alpha1.TerraformRunStatus) {
run.Status.RunStatus = status

// set completion time of the run only if status is completed/failed
if status == v1alpha1.RunCompleted || status == v1alpha1.RunFailed {
run.Status.CompletionTime = time.Now().Format(time.UnixDate)
}

// record the status only if completed/failed/waiting
if status == v1alpha1.RunCompleted || status == v1alpha1.RunFailed || status == v1alpha1.RunWaitingForDependency {
r.MetricsRecorder.RecordStatus(run.Name, run.Namespace, status)
}

Expand Down
4 changes: 4 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (r *Recorder) RecordTotal(name string, namespace string) {
func (r *Recorder) RecordStatus(name string, namespace string, status v1alpha1.TerraformRunStatus) {
var value float64

if status == v1alpha1.RunWaitingForDependency {
value = -1
}

if status == v1alpha1.RunFailed {
value = 1
}
Expand Down
117 changes: 68 additions & 49 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,61 +39,80 @@ var _ = Describe("Metrics Recorder", func() {
Expect(metricFamilies[0].Metric[0].Counter).ToNot(BeNil())
Expect(metricFamilies[0].Metric[0].Counter.Value).To(Equal(&value))
})
})

Context("Recording Status", func() {
It("should record the waitingForDependency status", func() {
rec.RecordStatus(name, namespace, v1alpha1.RunWaitingForDependency)

var (
value float64 = -1.0
metricName string = "tfo_workflow_status"
)

Context("Recording Status", func() {
It("should record the completed status", func() {
rec.RecordStatus(name, namespace, v1alpha1.RunCompleted)

var (
value float64 = 0.0
metricName string = "tfo_workflow_status"
)

metricFamilies, err := reg.Gather()

Expect(err).ToNot(HaveOccurred())
Expect(metricFamilies).To(HaveLen(2))
Expect(metricFamilies[0].Name).To(Equal(&metricName))
Expect(metricFamilies[0].Metric).To(HaveLen(1))
Expect(metricFamilies[0].Metric[0].Gauge).ToNot(BeNil())
Expect(metricFamilies[0].Metric[0].Gauge.Value).To(Equal(&value))
})

It("should record the failed status", func() {
rec.RecordStatus(name+"failed", namespace, v1alpha1.RunFailed)

var (
value float64 = 1.0
metricName string = "tfo_workflow_status"
)

metricFamilies, err := reg.Gather()

Expect(err).ToNot(HaveOccurred())
Expect(metricFamilies).To(HaveLen(2))
Expect(metricFamilies[0].Name).To(Equal(&metricName))
Expect(metricFamilies[0].Metric).To(HaveLen(2))
Expect(metricFamilies[0].Metric[1].Gauge).ToNot(BeNil())
Expect(metricFamilies[0].Metric[1].Gauge.Value).To(Equal(&value))
})
metricFamilies, err := reg.Gather()

Expect(err).ToNot(HaveOccurred())
Expect(metricFamilies).To(HaveLen(2))
Expect(metricFamilies[0].Name).To(Equal(&metricName))
Expect(metricFamilies[0].Metric).To(HaveLen(1))
Expect(metricFamilies[0].Metric[0].Gauge).ToNot(BeNil())
Expect(metricFamilies[0].Metric[0].Gauge.Value).To(Equal(&value))
})

Context("Recording Duration", func() {
It("should record the duration metric", func() {
rec.RecordDuration(name, namespace, time.Now())
It("should record the failed status", func() {
rec.RecordStatus(name, namespace, v1alpha1.RunFailed)

var (
metricName string = "tfo_workflow_duration_seconds"
)
var (
value float64 = 1.0
metricName string = "tfo_workflow_status"
)

metricFamilies, err := reg.Gather()

Expect(err).ToNot(HaveOccurred())
Expect(metricFamilies).To(HaveLen(2))
Expect(metricFamilies[0].Name).To(Equal(&metricName))
Expect(metricFamilies[0].Metric).To(HaveLen(1))
Expect(metricFamilies[0].Metric[0].Gauge).ToNot(BeNil())
Expect(metricFamilies[0].Metric[0].Gauge.Value).To(Equal(&value))
})

metricFamilies, err := reg.Gather()
It("should record the completed status", func() {
rec.RecordStatus(name, namespace, v1alpha1.RunCompleted)

Expect(err).ToNot(HaveOccurred())
Expect(metricFamilies).To(HaveLen(3))
Expect(metricFamilies[0].Name).To(Equal(&metricName))
Expect(metricFamilies[0].Metric).To(HaveLen(1))
Expect(metricFamilies[0].Metric[0].Histogram).ToNot(BeNil())
})
var (
value float64 = 0.0
metricName string = "tfo_workflow_status"
)

metricFamilies, err := reg.Gather()

Expect(err).ToNot(HaveOccurred())
Expect(metricFamilies).To(HaveLen(2))
Expect(metricFamilies[0].Name).To(Equal(&metricName))
Expect(metricFamilies[0].Metric).To(HaveLen(1))
Expect(metricFamilies[0].Metric[0].Gauge).ToNot(BeNil())
Expect(metricFamilies[0].Metric[0].Gauge.Value).To(Equal(&value))
})

})

Context("Recording Duration", func() {
It("should record the duration metric", func() {
rec.RecordDuration(name, namespace, time.Now())

var (
metricName string = "tfo_workflow_duration_seconds"
)

metricFamilies, err := reg.Gather()

Expect(err).ToNot(HaveOccurred())
Expect(metricFamilies).To(HaveLen(3))
Expect(metricFamilies[0].Name).To(Equal(&metricName))
Expect(metricFamilies[0].Metric).To(HaveLen(1))
Expect(metricFamilies[0].Metric[0].Histogram).ToNot(BeNil())
})
})
})

0 comments on commit a9a0f2d

Please sign in to comment.