Skip to content

Commit

Permalink
gaugevec metrics for pkg repo, app, pkg install
Browse files Browse the repository at this point in the history
  • Loading branch information
sethiyash committed Dec 5, 2023
1 parent f0e42c0 commit d657d7e
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 55 deletions.
4 changes: 3 additions & 1 deletion cmd/controller/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ func Run(opts Options, runLog logr.Logger) error {
pkgToPkgInstallHandler := pkginstall.NewPackageInstallVersionHandler(
kcClient, opts.PackagingGlobalNS, runLog.WithName("handler"))

reconciler := pkginstall.NewReconciler(kcClient, pkgClient, coreClient, pkgToPkgInstallHandler, runLog.WithName("pkgi"), compInfo, kcConfig)
reconciler := pkginstall.NewReconciler(kcClient, pkgClient, coreClient, pkgToPkgInstallHandler,
runLog.WithName("pkgi"), compInfo, kcConfig, reconcileTimeMetrics)

ctrl, err := controller.New("pkgi", mgr, controller.Options{
Reconciler: reconciler,
Expand All @@ -258,6 +259,7 @@ func Run(opts Options, runLog logr.Logger) error {
CoreClient: coreClient,
AppClient: kcClient,
KcConfig: kcConfig,
AppMetrics: appMetrics,
TimeMetrics: reconcileTimeMetrics,
CmdRunner: sidecarCmdExec,
Kubeconf: kubeconf,
Expand Down
5 changes: 5 additions & 0 deletions config/config/agg-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ spec:
- port: 443
protocol: TCP
targetPort: api
name: main
- port: 8080
protocol: TCP
targetPort: metrics
name: metrics
selector:
app: kapp-controller
3 changes: 3 additions & 0 deletions config/config/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ spec:
- containerPort: #@ data.values.apiPort
name: api
protocol: TCP
- containerPort: 8080
name: metrics
protocol: TCP
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/cppforlife/go-cli-ui v0.0.0-20220425131040-94f26b16bc14
github.com/go-logr/logr v1.2.4
github.com/k14s/semver/v4 v4.0.1-0.20210701191048-266d47ac6115
github.com/prometheus/client_model v0.4.0
github.com/spf13/cobra v1.6.1
golang.org/x/sync v0.2.0
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -86,7 +87,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
1 change: 1 addition & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type App struct {
appMetrics *metrics.AppMetrics
timeMetrics *metrics.ReconcileTimeMetrics

isFirstReconcile string
pendingStatusUpdate bool
flushAllStatusUpdates bool
metadata *deploy.Meta
Expand Down
7 changes: 0 additions & 7 deletions pkg/app/app_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ package app

import (
"fmt"
"time"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
ctldep "github.com/vmware-tanzu/carvel-kapp-controller/pkg/deploy"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/kubeconfig"
)

func (a *App) deploy(tplOutput string, changedFunc func(exec.CmdRunResult)) exec.CmdRunResult {
reconcileStartTS := time.Now()
defer func() {
a.timeMetrics.RegisterDeployTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
}()

err := a.blockDeletion()
if err != nil {
return exec.NewCmdRunResultWithErr(fmt.Errorf("Blocking for deploy: %s", err))
Expand Down
19 changes: 18 additions & 1 deletion pkg/app/app_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ func (a *App) reconcileDeploy() error {

func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
reconcileStartTS := time.Now()
a.isFirstReconcile = "false"
if a.appMetrics.GetReconcileAttemptCounterValue(a.app.Name, a.app.Namespace) == 1 {
a.isFirstReconcile = "true"
}

defer func() {
a.timeMetrics.RegisterOverallTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
a.timeMetrics.RegisterOverallTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
time.Since(reconcileStartTS))
}()

tmpDir := memdir.NewTmpDir("fetch-template-deploy")
Expand Down Expand Up @@ -134,6 +140,9 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.timeMetrics.RegisterFetchTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
a.app.Status.Fetch.UpdatedAt.Sub(a.app.Status.Fetch.StartedAt.Time))

err := a.updateStatus("marking fetch completed")
if err != nil {
return exec.NewCmdRunResultWithErr(err)
Expand All @@ -144,6 +153,8 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
}
}

templateStartTime := time.Now()

tplResult := a.template(assetsPath)

a.app.Status.Template = &v1alpha1.AppStatusTemplate{
Expand All @@ -153,6 +164,9 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
UpdatedAt: metav1.NewTime(time.Now().UTC()),
}

a.timeMetrics.RegisterTemplateTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
a.app.Status.Template.UpdatedAt.Sub(templateStartTime))

err = a.updateStatus("marking template completed")
if err != nil {
return exec.NewCmdRunResultWithErr(err)
Expand Down Expand Up @@ -201,6 +215,9 @@ func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
},
}

a.timeMetrics.RegisterDeployTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
a.Status().Deploy.UpdatedAt.Sub(a.Status().Deploy.StartedAt.Time))

return result
}

Expand Down
9 changes: 1 addition & 8 deletions pkg/app/app_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ package app

import (
"fmt"
"strings"
"time"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
ctltpl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/template"
"strings"
)

func (a *App) template(dirPath string) exec.CmdRunResult {
reconcileStartTS := time.Now()
defer func() {
a.timeMetrics.RegisterTemplateTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
}()

if len(a.app.Spec.Template) == 0 {
return exec.NewCmdRunResultWithErr(fmt.Errorf("Expected at least one template option"))
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/metrics/app_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

Expand Down Expand Up @@ -131,3 +132,11 @@ func (am *AppMetrics) RegisterReconcileDeleteAttempt(appName string, namespace s
func (am *AppMetrics) RegisterReconcileDeleteFailed(appName string, namespace string) {
am.reconcileDeleteFailedTotal.WithLabelValues(appName, namespace).Inc()
}

func (am *AppMetrics) GetReconcileAttemptCounterValue(appName, namespace string) int64 {

Check warning on line 136 in pkg/metrics/app_metrics.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method AppMetrics.GetReconcileAttemptCounterValue should have comment or be unexported (revive)
var m = &dto.Metric{}
if err := am.reconcileAttemptTotal.WithLabelValues(appName, namespace).Write(m); err != nil {
return 0
}
return int64(m.Counter.GetValue())
}
7 changes: 6 additions & 1 deletion pkg/metrics/reconcile_time_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
"sync"
"time"
)

Expand All @@ -13,6 +14,10 @@ type ReconcileTimeMetrics struct {
reconcileTemplateTimeSeconds *prometheus.GaugeVec
}

var (
timeMetricsOnce sync.Once
)

func NewReconcileTimeMetrics() *ReconcileTimeMetrics {

Check warning on line 21 in pkg/metrics/reconcile_time_metrics.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function NewReconcileTimeMetrics should have comment or be unexported (revive)
const (
metricNamespace = "kappctrl_reconcile_time_seconds"
Expand Down Expand Up @@ -58,7 +63,7 @@ func NewReconcileTimeMetrics() *ReconcileTimeMetrics {
}

func (tm *ReconcileTimeMetrics) RegisterAllMetrics() {

Check warning on line 65 in pkg/metrics/reconcile_time_metrics.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method ReconcileTimeMetrics.RegisterAllMetrics should have comment or be unexported (revive)
once.Do(func() {
timeMetricsOnce.Do(func() {
metrics.Registry.MustRegister(
tm.reconcileTimeSeconds,
tm.reconcileFetchTimeSeconds,
Expand Down
5 changes: 2 additions & 3 deletions pkg/packageinstall/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ package packageinstall

import (
"fmt"
"sort"
"strings"

"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
pkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
datapkgingv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apiserver/apis/datapackaging/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned/scheme"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sort"
"strings"
)

const (
Expand Down
13 changes: 11 additions & 2 deletions pkg/packageinstall/packageinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package packageinstall
import (
"context"
"fmt"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -54,6 +55,8 @@ type PackageInstallCR struct {
coreClient kubernetes.Interface
compInfo ComponentInfo
opts Opts

timeMetrics *metrics.ReconcileTimeMetrics
}

// nolint: revive
Expand All @@ -62,10 +65,11 @@ type Opts struct {
}

func NewPackageInstallCR(model *pkgingv1alpha1.PackageInstall, log logr.Logger,
kcclient kcclient.Interface, pkgclient pkgclient.Interface, coreClient kubernetes.Interface, compInfo ComponentInfo, opts Opts) *PackageInstallCR {
kcclient kcclient.Interface, pkgclient pkgclient.Interface, coreClient kubernetes.Interface,
compInfo ComponentInfo, opts Opts, timeMetrics *metrics.ReconcileTimeMetrics) *PackageInstallCR {

return &PackageInstallCR{model: model, unmodifiedModel: model.DeepCopy(), log: log,
kcclient: kcclient, pkgclient: pkgclient, coreClient: coreClient, compInfo: compInfo, opts: opts}
kcclient: kcclient, pkgclient: pkgclient, coreClient: coreClient, compInfo: compInfo, opts: opts, timeMetrics: timeMetrics}
}

func (pi *PackageInstallCR) Reconcile() (reconcile.Result, error) {
Expand Down Expand Up @@ -101,6 +105,11 @@ func (pi *PackageInstallCR) Reconcile() (reconcile.Result, error) {
func (pi *PackageInstallCR) reconcile(modelStatus *reconciler.Status) (reconcile.Result, error) {
pi.log.Info("Reconciling")

reconcileStartTS := time.Now()
defer func() {
pi.timeMetrics.RegisterOverallTime("pkgi", pi.model.Name, pi.model.Namespace, "", time.Since(reconcileStartTS))
}()

err := pi.blockDeletion()
if err != nil {
return reconcile.Result{Requeue: true}, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/packageinstall/packageinstall_deletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package packageinstall

import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"testing"

"github.com/k14s/semver/v4"
Expand Down Expand Up @@ -65,7 +66,7 @@ func Test_PackageInstallDeletion(t *testing.T) {
appClient := fakekappctrl.NewSimpleClientset(pkgInstall, existingApp)
coreClient := fake.NewSimpleClientset()

ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, coreClient, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{})
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, coreClient, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{}, &metrics.ReconcileTimeMetrics{})
_, err := ip.Reconcile()
assert.Nil(t, err)

Expand Down
10 changes: 6 additions & 4 deletions pkg/packageinstall/packageinstall_downgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package packageinstall

import (
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/metrics"
"testing"

"github.com/k14s/semver/v4"
Expand All @@ -25,6 +26,7 @@ import (

func Test_PackageInstallVersionDowngrades(t *testing.T) {
log := logf.Log.WithName("kc")
var reconcileTimeMetrics = metrics.NewReconcileTimeMetrics()

pkg1 := datapkgingv1alpha1.Package{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -98,7 +100,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
GitVersion: "v0.20.0",
}

ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{})
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{}, reconcileTimeMetrics)
_, err := ip.Reconcile()
assert.Nil(t, err)

Expand Down Expand Up @@ -147,7 +149,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
GitVersion: "v0.20.0",
}

ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{})
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{}, reconcileTimeMetrics)
_, err := ip.Reconcile()
assert.Nil(t, err)

Expand Down Expand Up @@ -196,7 +198,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
GitVersion: "v0.20.0",
}

ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{})
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{}, reconcileTimeMetrics)
_, err := ip.Reconcile()
assert.Nil(t, err)

Expand Down Expand Up @@ -251,7 +253,7 @@ func Test_PackageInstallVersionDowngrades(t *testing.T) {
GitVersion: "v0.20.0",
}

ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{})
ip := NewPackageInstallCR(pkgInstall, log, appClient, pkgClient, fakek8s, FakeComponentInfo{KCVersion: semver.MustParse("0.42.31337")}, Opts{}, reconcileTimeMetrics)
_, err := ip.Reconcile()
assert.Nil(t, err)

Expand Down
Loading

0 comments on commit d657d7e

Please sign in to comment.