Skip to content

Commit

Permalink
Merge pull request #422 from vshn/fix-prom-label
Browse files Browse the repository at this point in the history
Fix missing jobType label in the metrics
  • Loading branch information
ccremer authored Apr 15, 2021
2 parents a7e17bb + 164bafa commit 03bd378
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion controllers/backup_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (ts *BackupTestSuite) Test_GivenFinishedPreBackupDeployment_WhenReconciling
ts.expectABackupJobEventually()
ts.markBackupAsFinished(ts.BackupResource)

suceeded := observer.Suceeded
suceeded := observer.Succeeded
ts.notifyObserverOfBackupJobStatusChange(suceeded)

ts.assertDeploymentIsDeleted(ts.newPreBackupDeployment())
Expand Down
2 changes: 1 addition & 1 deletion controllers/check_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (ts *CheckTestSuite) whenJobCallbackIsInvoked(checkName string) {

o := observer.GetObserver()
observableJob := o.GetJobByName(checkNSName.String())
observableJob.Event = observer.Suceeded
observableJob.Event = observer.Succeeded
observableJob.Job = check

eventChannel := o.GetUpdateChannel()
Expand Down
2 changes: 1 addition & 1 deletion executor/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (g *generic) RegisterJobSucceededConditionCallback() {
name := g.GetJobNamespacedName()
observer.GetObserver().RegisterCallback(name.String(), func(event observer.ObservableJob) {
switch event.Event {
case observer.Suceeded:
case observer.Succeeded:
g.SetFinished(event.Job.Namespace, event.Job.Name)
g.SetConditionTrueWithMessage(k8upv1alpha1.ConditionCompleted,
k8upv1alpha1.ReasonSucceeded,
Expand Down
10 changes: 5 additions & 5 deletions handler/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (j *JobHandler) Handle() error {
}

if j.job.Status.Succeeded > 0 {
jobEvent = observer.Suceeded
jobEvent = observer.Succeeded
}

if j.job.Status.Failed > 0 {
Expand All @@ -69,14 +69,14 @@ func (j *JobHandler) Handle() error {
exclusive = false
}

jobType := v1alpha1.ScheduleType
if j.Config.Obj != nil {
jobType = j.Config.Obj.GetType()
jobType, exists := j.job.GetLabels()[v1alpha1.LabelK8upType]
if !exists {
jobType = v1alpha1.ScheduleType.String()
}

oj := observer.ObservableJob{
Job: j.job,
JobType: jobType,
JobType: v1alpha1.JobType(jobType),
Exclusive: exclusive,
Event: jobEvent,
}
Expand Down
35 changes: 18 additions & 17 deletions observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,30 @@ import (
)

const (
Update EventType = "update"
Delete EventType = "delete"
Create EventType = "create"
Failed EventType = "failed"
Suceeded EventType = "suceeded"
Running EventType = "running"
Update EventType = "update"
Delete EventType = "delete"
Create EventType = "create"
Failed EventType = "failed"
Succeeded EventType = "succeeded"
Running EventType = "running"
)

var (
observer *Observer

promLabels = []string{
"namespace",
"jobType",
}

metricsFailureCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "k8up_jobs_failed_counter",
Help: "The total number of backups that failed",
Help: "The total number of jobs that failed",
}, promLabels)

metricsSuccessCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "k8up_jobs_successful_counter",
Help: "The total number of backups that went through cleanly",
Help: "The total number of jobs that went through cleanly",
}, promLabels)

metricsTotalCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Expand Down Expand Up @@ -120,15 +121,15 @@ func (o *Observer) handleEvent(event ObservableJob) {

switch event.Event {
case Failed:
incFailureCounters(event.Job.Namespace)
incFailureCounters(event.Job.Namespace, event.JobType)
invokeCallbacks(event)
case Suceeded:
case Succeeded:
// Only report succeeded jobs we've already seen to prevent
// reporting succeeded jobs on operator restart
if exists {
o.log.Info("job succeeded", "jobName", jobName)
o.observedJobs[jobName] = event
incSuccessCounters(event.Job.Namespace)
incSuccessCounters(event.Job.Namespace, event.JobType)
invokeCallbacks(event)
}
case Delete:
Expand Down Expand Up @@ -254,12 +255,12 @@ func (o *Observer) RegisterCallback(name string, callback ObservableJobCallback)
}
}

func incFailureCounters(namespace string) {
metricsFailureCounter.WithLabelValues(namespace).Inc()
metricsTotalCounter.WithLabelValues(namespace).Inc()
func incFailureCounters(namespace string, jobType v1alpha1.JobType) {
metricsFailureCounter.WithLabelValues(namespace, jobType.String()).Inc()
metricsTotalCounter.WithLabelValues(namespace, jobType.String()).Inc()
}

func incSuccessCounters(namespace string) {
metricsSuccessCounter.WithLabelValues(namespace).Inc()
metricsTotalCounter.WithLabelValues(namespace).Inc()
func incSuccessCounters(namespace string, jobType v1alpha1.JobType) {
metricsSuccessCounter.WithLabelValues(namespace, jobType.String()).Inc()
metricsTotalCounter.WithLabelValues(namespace, jobType.String()).Inc()
}
4 changes: 2 additions & 2 deletions observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestObserver_AreAllCallbacksInvoked(t *testing.T) {
},
},
JobType: v1alpha1.BackupType,
Event: Suceeded,
Event: Succeeded,
Exclusive: true,
Repository: "some-repo",
callbacks: callbacks,
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestObserver_AreOnlyExpectedCallbacksInvoked(t *testing.T) {
expectInvocation bool
}{
"GivenStatusSucceeded_WhenHandleEvent_ThenExpectInvocation": {
givenEventType: Suceeded,
givenEventType: Succeeded,
expectInvocation: true,
},
"GivenStatusFailed_WhenHandleEvent_ThenExpectInvocation": {
Expand Down

0 comments on commit 03bd378

Please sign in to comment.