Skip to content

Commit

Permalink
Add metric for Application deletion (#383)
Browse files Browse the repository at this point in the history
* Add metric for Application deletion

Signed-off-by: John Collier <[email protected]>

* Fix test

Signed-off-by: John Collier <[email protected]>

---------

Signed-off-by: John Collier <[email protected]>
  • Loading branch information
johnmcollier authored Aug 25, 2023
1 parent 1f9eb46 commit 1c302a6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions controllers/application_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
} else {
if containsString(application.GetFinalizers(), appFinalizerName) {
metrics.ApplicationDeletionTotalReqs.Inc()
// A finalizer is present for the Application CR, so make sure we do the necessary cleanup steps
if err := r.Finalize(ctx, &application, ghClient); err != nil {
finalizeCounter, err := getCounterAnnotation(finalizeCount, &application)
Expand All @@ -126,6 +127,9 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// if fail to delete the external dependency here, log the error, but don't return error
// Don't want to get stuck in a cycle of repeatedly trying to delete the repository and failing
log.Error(err, "Unable to delete GitOps repository for application %v in namespace %v", application.GetName(), application.GetNamespace())

// Increment the Application deletion failed metric as the application delete did not fully succeed
metrics.ApplicationDeletionFailed.Inc()
}

}
Expand All @@ -134,6 +138,8 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
controllerutil.RemoveFinalizer(&application, appFinalizerName)
if err := r.Update(ctx, &application); err != nil {
return ctrl.Result{}, err
} else {
metrics.ApplicationDeletionSucceeded.Inc()
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions controllers/application_controller_finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
appstudiov1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
cdqanalysis "github.com/redhat-appstudio/application-service/cdq-analysis/pkg"
"github.com/redhat-appstudio/application-service/pkg/metrics"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -51,8 +54,13 @@ var _ = Describe("Application controller finalizer counter tests", func() {
Description = "Simple petclinic app"
)

prometheus.MustRegister(metrics.ApplicationDeletionTotalReqs, metrics.ApplicationDeletionFailed, metrics.ApplicationDeletionSucceeded)

Context("Delete Application CR fields with invalid devfile", func() {
It("Should delete successfully even when finalizer fails after 5 times", func() {
beforeDeleteTotalReqs := testutil.ToFloat64(metrics.ApplicationDeletionTotalReqs)
beforeDeleteSucceedReqs := testutil.ToFloat64(metrics.ApplicationDeletionSucceeded)
beforeDeleteFailedReqs := testutil.ToFloat64(metrics.ApplicationDeletionFailed)
// Create a simple Application CR and get its devfile
fetchedApp := createAndFetchSimpleApp(AppName, AppNamespace, DisplayName, Description)
devfileSrc := cdqanalysis.DevfileSrc{
Expand Down Expand Up @@ -90,6 +98,10 @@ var _ = Describe("Application controller finalizer counter tests", func() {
f := &appstudiov1alpha1.Application{}
return k8sClient.Get(context.Background(), hasAppLookupKey, f)
}, timeout, interval).ShouldNot(Succeed())

Expect(testutil.ToFloat64(metrics.ApplicationDeletionTotalReqs) > beforeDeleteTotalReqs).To(BeTrue())
Expect(testutil.ToFloat64(metrics.ApplicationDeletionSucceeded) > beforeDeleteSucceedReqs).To(BeTrue())
Expect(testutil.ToFloat64(metrics.ApplicationDeletionFailed) > beforeDeleteFailedReqs).To(BeTrue())
})
})

Expand Down
22 changes: 21 additions & 1 deletion pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,31 @@ var (
//tokenName - the name of the token being rate limited
[]string{"rateLimited", "tokenName"},
)

ApplicationDeletionTotalReqs = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "has_application_deletion_total",
Help: "Number of application deletion requests processed",
},
)
ApplicationDeletionFailed = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "has_application_failed_deletion_total",
Help: "Number of failed application deletion requests",
},
)

ApplicationDeletionSucceeded = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "has_application_successful_deletion_total",
Help: "Number of successful application deletion requests",
},
)
)

func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(GitOpsRepoCreationTotalReqs, GitOpsRepoCreationFailed, GitOpsRepoCreationSucceeded, ControllerGitRequest, SecondaryRateLimitCounter, PrimaryRateLimitCounter, TokenPoolGauge)
metrics.Registry.MustRegister(GitOpsRepoCreationTotalReqs, GitOpsRepoCreationFailed, GitOpsRepoCreationSucceeded, ControllerGitRequest, SecondaryRateLimitCounter, PrimaryRateLimitCounter, TokenPoolGauge, ApplicationDeletionTotalReqs, ApplicationDeletionSucceeded, ApplicationDeletionFailed)
}

// HandleRateLimitMetrics checks the error type to verify a primary or secondary rate limit has been encountered
Expand Down

0 comments on commit 1c302a6

Please sign in to comment.