Skip to content

Commit

Permalink
Added E2E testcases for exposing prometheus metrics
Browse files Browse the repository at this point in the history
Signed-off-by: sethiyash <[email protected]>
Signed-off-by: Yash Sethiya <[email protected]>
  • Loading branch information
sethiyash authored and Yash Sethiya committed Jan 17, 2024
1 parent 68d3e5d commit f9b21d3
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 30 deletions.
3 changes: 3 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func NewApp(app v1alpha1.App, hooks Hooks,
func (a *App) Name() string { return a.app.Name }
func (a *App) Namespace() string { return a.app.Namespace }

// Kind return kind of App
func (a *App) Kind() string { return "app" }

func (a *App) Status() v1alpha1.AppStatus { return a.app.Status }

func (a *App) StatusAsYAMLBytes() ([]byte, error) {
Expand Down
26 changes: 12 additions & 14 deletions pkg/app/app_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const appResourceType = "app"

// Reconcile is not expected to be called concurrently
func (a *App) Reconcile(force bool) (reconcile.Result, error) {
defer a.flushUpdateStatus("app reconciled")

var err error

a.appMetrics.ReconcileCountMetrics.InitMetrics(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.InitMetrics(a.Kind(), a.Name(), a.Namespace())

timerOpts := ReconcileTimerOpts{
DefaultSyncPeriod: a.opts.DefaultSyncPeriod,
Expand Down Expand Up @@ -106,9 +104,9 @@ func (a *App) reconcileDeploy() error {

func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
reconcileStartTime := time.Now()
a.appMetrics.IsFirstReconcile = a.appMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue(appResourceType, a.Name(), a.Namespace()) == 1
a.appMetrics.IsFirstReconcile = a.appMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue(a.Kind(), a.Name(), a.Namespace()) == 1
defer func() {
a.appMetrics.ReconcileTimeMetrics.RegisterOverallTime(appResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterOverallTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
time.Since(reconcileStartTime))
}()

Expand Down Expand Up @@ -138,7 +136,7 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.appMetrics.ReconcileTimeMetrics.RegisterFetchTime(appResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterFetchTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.app.Status.Fetch.UpdatedAt.Sub(a.app.Status.Fetch.StartedAt.Time))

err := a.updateStatus("marking fetch completed")
Expand All @@ -162,7 +160,7 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.appMetrics.ReconcileTimeMetrics.RegisterTemplateTime(appResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterTemplateTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.app.Status.Template.UpdatedAt.Sub(templateStartTime))

err = a.updateStatus("marking template completed")
Expand Down Expand Up @@ -213,7 +211,7 @@ func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
},
}

a.appMetrics.ReconcileTimeMetrics.RegisterDeployTime(appResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterDeployTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.Status().Deploy.UpdatedAt.Sub(a.Status().Deploy.StartedAt.Time))

return result
Expand Down Expand Up @@ -267,7 +265,7 @@ func (a *App) setReconciling() {
Status: corev1.ConditionTrue,
})

a.appMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(a.Kind(), a.Name(), a.Namespace())
a.app.Status.FriendlyDescription = "Reconciling"
}

Expand All @@ -283,7 +281,7 @@ func (a *App) setReconcileCompleted(result exec.CmdRunResult) {
a.app.Status.ConsecutiveReconcileFailures++
a.app.Status.ConsecutiveReconcileSuccesses = 0
a.app.Status.FriendlyDescription = fmt.Sprintf("Reconcile failed: %s", result.ErrorStr())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileFailure(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileFailure(a.Kind(), a.Name(), a.Namespace())
a.setUsefulErrorMessage(result)
} else {
a.app.Status.Conditions = append(a.app.Status.Conditions, v1alpha1.Condition{
Expand All @@ -294,7 +292,7 @@ func (a *App) setReconcileCompleted(result exec.CmdRunResult) {
a.app.Status.ConsecutiveReconcileSuccesses++
a.app.Status.ConsecutiveReconcileFailures = 0
a.app.Status.FriendlyDescription = "Reconcile succeeded"
a.appMetrics.ReconcileCountMetrics.RegisterReconcileSuccess(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileSuccess(a.Kind(), a.Name(), a.Namespace())
a.app.Status.UsefulErrorMessage = ""
}
}
Expand All @@ -307,7 +305,7 @@ func (a *App) setDeleting() {
Status: corev1.ConditionTrue,
})

a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteAttempt(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteAttempt(a.Kind(), a.Name(), a.Namespace())
a.app.Status.FriendlyDescription = "Deleting"
}

Expand All @@ -323,10 +321,10 @@ func (a *App) setDeleteCompleted(result exec.CmdRunResult) {
a.app.Status.ConsecutiveReconcileFailures++
a.app.Status.ConsecutiveReconcileSuccesses = 0
a.app.Status.FriendlyDescription = fmt.Sprintf("Delete failed: %s", result.ErrorStr())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteFailed(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileDeleteFailed(a.Kind(), a.Name(), a.Namespace())
a.setUsefulErrorMessage(result)
} else {
a.appMetrics.ReconcileCountMetrics.DeleteMetrics(appResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.DeleteMetrics(a.Kind(), a.Name(), a.Namespace())
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/metrics/reconcile_count_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func NewCountMetrics() *ReconcileCountMetrics {
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "app_reconcile_delete_attempt_total",
Help: "Total number of attempted reconcile deletion",
Help: "Total number of attempted reconcile deletions",
},
[]string{resourceTypeLabel, kappNameLabel, kappNamespaceLabel},
),
reconcileDeleteFailedTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "app_reconcile_delete_failed_total",
Help: "Total number of failed reconcile deletion",
Help: "Total number of failed reconcile deletions",
},
[]string{resourceTypeLabel, kappNameLabel, kappNamespaceLabel},
),
Expand Down
14 changes: 9 additions & 5 deletions pkg/packageinstall/packageinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const (
// PackageInstall to indicate that lower version of the package
// can be selected vs whats currently installed.
DowngradableAnnKey = "packaging.carvel.dev/downgradable"
packageInstallType = "pkgi"
)

// nolint: revive
Expand All @@ -60,6 +59,11 @@ type PackageInstallCR struct {
pkgMetrics *metrics.Metrics
}

// Kind return kind of pkg install
func (pi *PackageInstallCR) Kind() string {
return "pkgi"
}

// nolint: revive
type Opts struct {
DefaultSyncPeriod time.Duration
Expand All @@ -79,7 +83,7 @@ func (pi *PackageInstallCR) Reconcile() (reconcile.Result, error) {
func(st kcv1alpha1.GenericStatus) { pi.model.Status.GenericStatus = st },
}

pi.pkgMetrics.ReconcileCountMetrics.InitMetrics(packageInstallType, pi.model.Name, pi.model.Namespace)
pi.pkgMetrics.ReconcileCountMetrics.InitMetrics(pi.Kind(), pi.model.Name, pi.model.Namespace)

var result reconcile.Result
var err error
Expand Down Expand Up @@ -107,12 +111,12 @@ func (pi *PackageInstallCR) Reconcile() (reconcile.Result, error) {

func (pi *PackageInstallCR) reconcile(modelStatus *reconciler.Status) (reconcile.Result, error) {
pi.log.Info("Reconciling")
pi.pkgMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(packageInstallType, pi.model.Name, pi.model.Namespace)
pi.pkgMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(pi.Kind(), pi.model.Name, pi.model.Namespace)

reconcileStartTime := time.Now()
pi.pkgMetrics.IsFirstReconcile = pi.pkgMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue(packageInstallType, pi.model.Name, pi.model.Namespace) == 1
pi.pkgMetrics.IsFirstReconcile = pi.pkgMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue(pi.Kind(), pi.model.Name, pi.model.Namespace) == 1
defer func() {
pi.pkgMetrics.ReconcileTimeMetrics.RegisterOverallTime(packageInstallType, pi.model.Name, pi.model.Namespace,
pi.pkgMetrics.ReconcileTimeMetrics.RegisterOverallTime(pi.Kind(), pi.model.Name, pi.model.Namespace,
pi.pkgMetrics.IsFirstReconcile, time.Since(reconcileStartTime))
}()

Expand Down
3 changes: 3 additions & 0 deletions pkg/pkgrepository/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func NewApp(app v1alpha1.App, hooks Hooks, fetchFactory fetch.Factory, templateF
func (a *App) Name() string { return a.app.Name }
func (a *App) Namespace() string { return a.app.Namespace }

// Kind return kind of pkg repo
func (a *App) Kind() string { return "pkgr" }

func (a *App) Status() v1alpha1.AppStatus { return a.app.Status }

func (a *App) blockDeletion() error { return a.hooks.BlockDeletion() }
Expand Down
16 changes: 7 additions & 9 deletions pkg/pkgrepository/app_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const packageRepoResourceType = "pkgr"

// Reconcile is not expected to be called concurrently
func (a *App) Reconcile(force bool) (reconcile.Result, error) {
defer a.flushUpdateStatus("packagerepository reconciled")

var err error

a.appMetrics.ReconcileCountMetrics.InitMetrics(packageRepoResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.InitMetrics(a.Kind(), a.Name(), a.Namespace())

switch {
case a.app.Spec.Paused:
Expand Down Expand Up @@ -95,9 +93,9 @@ func (a *App) reconcileDeploy() error {

func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
reconcileStartTime := time.Now()
a.appMetrics.IsFirstReconcile = a.appMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue(packageRepoResourceType, a.Name(), a.Namespace()) == 1
a.appMetrics.IsFirstReconcile = a.appMetrics.ReconcileCountMetrics.GetReconcileAttemptCounterValue(a.Kind(), a.Name(), a.Namespace()) == 1
defer func() {
a.appMetrics.ReconcileTimeMetrics.RegisterOverallTime(packageRepoResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterOverallTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
time.Since(reconcileStartTime))
}()

Expand Down Expand Up @@ -127,7 +125,7 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.appMetrics.ReconcileTimeMetrics.RegisterFetchTime(packageRepoResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterFetchTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.app.Status.Fetch.UpdatedAt.Sub(a.app.Status.Fetch.StartedAt.Time))

err := a.updateStatus("marking fetch completed")
Expand All @@ -151,7 +149,7 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.appMetrics.ReconcileTimeMetrics.RegisterTemplateTime(packageRepoResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterTemplateTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.app.Status.Template.UpdatedAt.Sub(templateStartTime))

err = a.updateStatus("marking template completed")
Expand Down Expand Up @@ -181,7 +179,7 @@ func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.appMetrics.ReconcileTimeMetrics.RegisterDeployTime(packageRepoResourceType, a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.appMetrics.ReconcileTimeMetrics.RegisterDeployTime(a.Kind(), a.Name(), a.Namespace(), a.appMetrics.IsFirstReconcile,
a.Status().Deploy.UpdatedAt.Sub(a.Status().Deploy.StartedAt.Time))

a.updateStatus("marking last deploy")
Expand Down Expand Up @@ -215,7 +213,7 @@ func (a *App) setReconciling() {
Status: corev1.ConditionTrue,
})

a.appMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(packageRepoResourceType, a.Name(), a.Namespace())
a.appMetrics.ReconcileCountMetrics.RegisterReconcileAttempt(a.Kind(), a.Name(), a.Namespace())

a.app.Status.FriendlyDescription = "Reconciling"
}
Expand Down
Loading

0 comments on commit f9b21d3

Please sign in to comment.