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

Add metric for Application deletion #383

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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()
johnmcollier marked this conversation as resolved.
Show resolved Hide resolved
}

}
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
8 changes: 8 additions & 0 deletions controllers/application_controller_finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/prometheus/client_golang/prometheus"
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,6 +53,8 @@ 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() {
// Create a simple Application CR and get its devfile
Expand Down Expand Up @@ -90,6 +94,10 @@ var _ = Describe("Application controller finalizer counter tests", func() {
f := &appstudiov1alpha1.Application{}
return k8sClient.Get(context.Background(), hasAppLookupKey, f)
}, timeout, interval).ShouldNot(Succeed())

Expect(metrics.ApplicationDeletionTotalReqs).To(Equal(1))
Expect(metrics.ApplicationDeletionSucceeded).To(Equal(1))
Expect(metrics.ApplicationDeletionFailed).To(Equal(0))
})
})

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
Loading