Skip to content

Commit f0e42c0

Browse files
committed
Added gauge metrics to record reconcile time for app and pkg repo
Signed-off-by: sethiyash <[email protected]>
1 parent 59d4642 commit f0e42c0

20 files changed

+190
-32
lines changed

cmd/controller/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func Run(opts Options, runLog logr.Logger) error {
9999
appMetrics := metrics.NewAppMetrics()
100100
appMetrics.RegisterAllMetrics()
101101

102+
reconcileTimeMetrics := metrics.NewReconcileTimeMetrics()
103+
reconcileTimeMetrics.RegisterAllMetrics()
104+
102105
var server *apiserver.APIServer
103106
if opts.StartAPIServer {
104107
// assign bindPort to env var KAPPCTRL_API_PORT if available
@@ -198,6 +201,7 @@ func Run(opts Options, runLog logr.Logger) error {
198201
AppClient: kcClient,
199202
KcConfig: kcConfig,
200203
AppMetrics: appMetrics,
204+
TimeMetrics: reconcileTimeMetrics,
201205
CmdRunner: sidecarCmdExec,
202206
Kubeconf: kubeconf,
203207
CompInfo: compInfo,
@@ -254,6 +258,7 @@ func Run(opts Options, runLog logr.Logger) error {
254258
CoreClient: coreClient,
255259
AppClient: kcClient,
256260
KcConfig: kcConfig,
261+
TimeMetrics: reconcileTimeMetrics,
257262
CmdRunner: sidecarCmdExec,
258263
Kubeconf: kubeconf,
259264
CacheFolder: cacheFolderPkgRepoApps,

pkg/app/app.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ type App struct {
5555
memoizedKubernetesVersion string
5656
memoizedKubernetesAPIs []string
5757

58-
log logr.Logger
59-
opts Opts
60-
appMetrics *metrics.AppMetrics
58+
log logr.Logger
59+
opts Opts
60+
appMetrics *metrics.AppMetrics
61+
timeMetrics *metrics.ReconcileTimeMetrics
6162

6263
pendingStatusUpdate bool
6364
flushAllStatusUpdates bool
@@ -66,11 +67,11 @@ type App struct {
6667

6768
func NewApp(app v1alpha1.App, hooks Hooks,
6869
fetchFactory fetch.Factory, templateFactory template.Factory,
69-
deployFactory deploy.Factory, log logr.Logger, opts Opts, appMetrics *metrics.AppMetrics, compInfo ComponentInfo) *App {
70+
deployFactory deploy.Factory, log logr.Logger, opts Opts, appMetrics *metrics.AppMetrics, timeMetrics *metrics.ReconcileTimeMetrics, compInfo ComponentInfo) *App {
7071

7172
return &App{app: app, appPrev: *(app.DeepCopy()), hooks: hooks,
7273
fetchFactory: fetchFactory, templateFactory: templateFactory,
73-
deployFactory: deployFactory, log: log, opts: opts, appMetrics: appMetrics, compInfo: compInfo}
74+
deployFactory: deployFactory, log: log, opts: opts, appMetrics: appMetrics, timeMetrics: timeMetrics, compInfo: compInfo}
7475
}
7576

7677
func (a *App) Name() string { return a.app.Name }

pkg/app/app_deploy.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package app
55

66
import (
77
"fmt"
8+
"time"
89

910
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
1011
ctldep "github.com/vmware-tanzu/carvel-kapp-controller/pkg/deploy"
@@ -13,6 +14,11 @@ import (
1314
)
1415

1516
func (a *App) deploy(tplOutput string, changedFunc func(exec.CmdRunResult)) exec.CmdRunResult {
17+
reconcileStartTS := time.Now()
18+
defer func() {
19+
a.timeMetrics.RegisterDeployTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
20+
}()
21+
1622
err := a.blockDeletion()
1723
if err != nil {
1824
return exec.NewCmdRunResultWithErr(fmt.Errorf("Blocking for deploy: %s", err))

pkg/app/app_factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type CRDAppFactory struct {
2727
AppClient kcclient.Interface
2828
KcConfig *config.Config
2929
AppMetrics *metrics.AppMetrics
30+
TimeMetrics *metrics.ReconcileTimeMetrics
3031
VendirConfigHook func(vendirconf.Config) vendirconf.Config
3132
KbldAllowBuild bool
3233
CmdRunner exec.CmdRunner
@@ -48,7 +49,7 @@ func (f *CRDAppFactory) NewCRDApp(app *kcv1alpha1.App, log logr.Logger) *CRDApp
4849
templateFactory := template.NewFactory(f.CoreClient, fetchFactory, f.KbldAllowBuild, f.CmdRunner)
4950
deployFactory := deploy.NewFactory(f.CoreClient, f.Kubeconf, f.KcConfig, f.CmdRunner, log)
5051

51-
return NewCRDApp(app, log, f.AppMetrics, f.AppClient, fetchFactory, templateFactory, deployFactory, f.CompInfo, Opts{
52+
return NewCRDApp(app, log, f.AppMetrics, f.TimeMetrics, f.AppClient, fetchFactory, templateFactory, deployFactory, f.CompInfo, Opts{
5253
DefaultSyncPeriod: f.KcConfig.AppDefaultSyncPeriod(),
5354
MinimumSyncPeriod: f.KcConfig.AppMinimumSyncPeriod(),
5455
})

pkg/app/app_fetch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ const (
2020
)
2121

2222
func (a *App) fetch(dstPath string) (string, exec.CmdRunResult) {
23+
// update metrics with the total fetch time
24+
reconcileStartTS := time.Now()
25+
defer func() {
26+
a.timeMetrics.RegisterFetchTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
27+
}()
28+
29+
// fetch init stage
2330
if len(a.app.Spec.Fetch) == 0 {
2431
return "", exec.NewCmdRunResultWithErr(fmt.Errorf("Expected at least one fetch option"))
2532
}

pkg/app/app_reconcile.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ func (a *App) reconcileDeploy() error {
103103
}
104104

105105
func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
106+
reconcileStartTS := time.Now()
107+
defer func() {
108+
a.timeMetrics.RegisterOverallTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
109+
}()
110+
106111
tmpDir := memdir.NewTmpDir("fetch-template-deploy")
107112

108113
err := tmpDir.Create()

pkg/app/app_reconcile_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import (
2929

3030
func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
3131
log := logf.Log.WithName("kc")
32-
var appMetrics = metrics.NewAppMetrics()
32+
var (
33+
appMetrics = metrics.NewAppMetrics()
34+
timeMetrics = metrics.NewReconcileTimeMetrics()
35+
)
3336

3437
// The url under fetch is invalid, which will cause this
3538
// app to fail before deploy.
@@ -52,7 +55,7 @@ func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
5255
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
5356
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
5457

55-
crdApp := NewCRDApp(&app, log, appMetrics, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
58+
crdApp := NewCRDApp(&app, log, appMetrics, timeMetrics, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
5659
_, err := crdApp.Reconcile(false)
5760
assert.Nil(t, err, "unexpected error with reconciling", err)
5861

@@ -86,7 +89,10 @@ func Test_NoInspectReconcile_IfNoDeployAttempted(t *testing.T) {
8689

8790
func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
8891
log := logf.Log.WithName("kc")
89-
var appMetrics = metrics.NewAppMetrics()
92+
var (
93+
appMetrics = metrics.NewAppMetrics()
94+
timeMetrics = metrics.NewReconcileTimeMetrics()
95+
)
9096

9197
app := v1alpha1.App{
9298
ObjectMeta: metav1.ObjectMeta{
@@ -119,7 +125,7 @@ func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
119125
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
120126
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
121127

122-
crdApp := NewCRDApp(&app, log, appMetrics, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
128+
crdApp := NewCRDApp(&app, log, appMetrics, timeMetrics, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
123129
_, err := crdApp.Reconcile(false)
124130
assert.Nil(t, err, "unexpected error with reconciling", err)
125131

@@ -164,7 +170,10 @@ func Test_NoInspectReconcile_IfInspectNotEnabled(t *testing.T) {
164170

165171
func Test_TemplateError_DisplayedInStatus_UsefulErrorMessageProperty(t *testing.T) {
166172
log := logf.Log.WithName("kc")
167-
var appMetrics = metrics.NewAppMetrics()
173+
var (
174+
appMetrics = metrics.NewAppMetrics()
175+
timeMetrics = metrics.NewReconcileTimeMetrics()
176+
)
168177

169178
fetchInline := map[string]string{
170179
"file.yml": `foo: #@ data.values.nothere`,
@@ -191,7 +200,7 @@ func Test_TemplateError_DisplayedInStatus_UsefulErrorMessageProperty(t *testing.
191200
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
192201
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
193202

194-
crdApp := NewCRDApp(&app, log, appMetrics, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
203+
crdApp := NewCRDApp(&app, log, appMetrics, timeMetrics, kappcs, fetchFac, tmpFac, deployFac, FakeComponentInfo{}, Opts{MinimumSyncPeriod: 30 * time.Second})
195204
_, err := crdApp.Reconcile(false)
196205
assert.Nil(t, err, "Unexpected error with reconciling", err)
197206

pkg/app/app_template.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ package app
66
import (
77
"fmt"
88
"strings"
9+
"time"
910

1011
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
1112
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
1213
ctltpl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/template"
1314
)
1415

1516
func (a *App) template(dirPath string) exec.CmdRunResult {
17+
reconcileStartTS := time.Now()
18+
defer func() {
19+
a.timeMetrics.RegisterTemplateTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
20+
}()
21+
1622
if len(a.app.Spec.Template) == 0 {
1723
return exec.NewCmdRunResultWithErr(fmt.Errorf("Expected at least one template option"))
1824
}

pkg/app/app_template_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Test_BuildAdditionalDownwardAPIValues_MemoizedCallCount(t *testing.T) {
6161
K8sAPIsCount: &k8sAPIsCallCount,
6262
KCVersionCount: &kcVersionCallCount,
6363
}
64-
app := NewApp(appEmpty, Hooks{}, fetchFac, tmpFac, deployFac, log, Opts{}, metrics.NewAppMetrics(), fakeInfo)
64+
app := NewApp(appEmpty, Hooks{}, fetchFac, tmpFac, deployFac, log, Opts{}, metrics.NewAppMetrics(), metrics.NewReconcileTimeMetrics(), fakeInfo)
6565

6666
dir, err := os.MkdirTemp("", "temp")
6767
assert.NoError(t, err)

pkg/app/app_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Test_SecretRefs_RetrievesAllSecretRefs(t *testing.T) {
6464
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
6565
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
6666

67-
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, FakeComponentInfo{})
67+
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, nil, FakeComponentInfo{})
6868

6969
out := app.SecretRefs()
7070
assert.Truef(t, reflect.DeepEqual(out, expected), "Expected: %s\nGot: %s\n", expected, out)
@@ -88,7 +88,7 @@ func Test_SecretRefs_RetrievesNoSecretRefs_WhenNonePresent(t *testing.T) {
8888
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
8989
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
9090

91-
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, FakeComponentInfo{})
91+
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, nil, FakeComponentInfo{})
9292

9393
out := app.SecretRefs()
9494
assert.Equal(t, 0, len(out), "No SecretRefs to be returned")
@@ -126,7 +126,7 @@ func Test_ConfigMapRefs_RetrievesAllConfigMapRefs(t *testing.T) {
126126
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
127127
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
128128

129-
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, FakeComponentInfo{})
129+
app := apppkg.NewApp(appWithRefs, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, nil, FakeComponentInfo{})
130130

131131
out := app.ConfigMapRefs()
132132
assert.Truef(t, reflect.DeepEqual(out, expected), "Expected: %s\nGot: %s\n", expected, out)
@@ -150,7 +150,7 @@ func Test_ConfigMapRefs_RetrievesNoConfigMapRefs_WhenNonePresent(t *testing.T) {
150150
tmpFac := template.NewFactory(k8scs, fetchFac, false, exec.NewPlainCmdRunner())
151151
deployFac := deploy.NewFactory(k8scs, kubeconfig.NewKubeconfig(k8scs, log), nil, exec.NewPlainCmdRunner(), log)
152152

153-
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, FakeComponentInfo{})
153+
app := apppkg.NewApp(appEmpty, apppkg.Hooks{}, fetchFac, tmpFac, deployFac, log, apppkg.Opts{}, nil, nil, FakeComponentInfo{})
154154

155155
out := app.ConfigMapRefs()
156156
assert.Lenf(t, out, 0, "Expected: %s\nGot: %s\n", "No ConfigMapRefs to be returned", out)

0 commit comments

Comments
 (0)