Skip to content

Commit 09d6cd0

Browse files
committed
gaugevec metrics for pkg repo, app, pkg install
Signed-off-by: sethiyash <[email protected]>
1 parent f0e42c0 commit 09d6cd0

23 files changed

+116
-55
lines changed

cmd/controller/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ func Run(opts Options, runLog logr.Logger) error {
231231
pkgToPkgInstallHandler := pkginstall.NewPackageInstallVersionHandler(
232232
kcClient, opts.PackagingGlobalNS, runLog.WithName("handler"))
233233

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

236237
ctrl, err := controller.New("pkgi", mgr, controller.Options{
237238
Reconciler: reconciler,
@@ -258,6 +259,7 @@ func Run(opts Options, runLog logr.Logger) error {
258259
CoreClient: coreClient,
259260
AppClient: kcClient,
260261
KcConfig: kcConfig,
262+
AppMetrics: appMetrics,
261263
TimeMetrics: reconcileTimeMetrics,
262264
CmdRunner: sidecarCmdExec,
263265
Kubeconf: kubeconf,

config/config/agg-api.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,10 @@ spec:
2323
- port: 443
2424
protocol: TCP
2525
targetPort: api
26+
name: main
27+
- port: 8080
28+
protocol: TCP
29+
targetPort: metrics
30+
name: metrics
2631
selector:
2732
app: kapp-controller

config/config/deployment.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ spec:
5252
- containerPort: #@ data.values.apiPort
5353
name: api
5454
protocol: TCP
55+
- containerPort: 8080
56+
name: metrics
57+
protocol: TCP
5558
securityContext:
5659
allowPrivilegeEscalation: false
5760
readOnlyRootFilesystem: true

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
github.com/cppforlife/go-cli-ui v0.0.0-20220425131040-94f26b16bc14
3131
github.com/go-logr/logr v1.2.4
3232
github.com/k14s/semver/v4 v4.0.1-0.20210701191048-266d47ac6115
33+
github.com/prometheus/client_model v0.4.0
3334
github.com/spf13/cobra v1.6.1
3435
golang.org/x/sync v0.2.0
3536
gopkg.in/yaml.v2 v2.4.0
@@ -86,7 +87,6 @@ require (
8687
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
8788
github.com/pkg/errors v0.9.1 // indirect
8889
github.com/pmezard/go-difflib v1.0.0 // indirect
89-
github.com/prometheus/client_model v0.4.0 // indirect
9090
github.com/prometheus/common v0.42.0 // indirect
9191
github.com/prometheus/procfs v0.9.0 // indirect
9292
github.com/spf13/pflag v1.0.5 // indirect

pkg/app/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type App struct {
6060
appMetrics *metrics.AppMetrics
6161
timeMetrics *metrics.ReconcileTimeMetrics
6262

63+
isFirstReconcile string
6364
pendingStatusUpdate bool
6465
flushAllStatusUpdates bool
6566
metadata *deploy.Meta

pkg/app/app_deploy.go

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

66
import (
77
"fmt"
8-
"time"
9-
108
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
119
ctldep "github.com/vmware-tanzu/carvel-kapp-controller/pkg/deploy"
1210
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
1311
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/kubeconfig"
1412
)
1513

1614
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-
2215
err := a.blockDeletion()
2316
if err != nil {
2417
return exec.NewCmdRunResultWithErr(fmt.Errorf("Blocking for deploy: %s", err))

pkg/app/app_reconcile.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ func (a *App) reconcileDeploy() error {
104104

105105
func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
106106
reconcileStartTS := time.Now()
107+
a.isFirstReconcile = "false"
108+
if a.appMetrics.GetReconcileAttemptCounterValue(a.app.Name, a.app.Namespace) == 1 {
109+
a.isFirstReconcile = "true"
110+
}
111+
107112
defer func() {
108-
a.timeMetrics.RegisterOverallTime(a.app.Kind, a.app.Name, a.app.Namespace, "", time.Since(reconcileStartTS))
113+
a.timeMetrics.RegisterOverallTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
114+
time.Since(reconcileStartTS))
109115
}()
110116

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

143+
a.timeMetrics.RegisterFetchTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
144+
a.app.Status.Fetch.UpdatedAt.Sub(a.app.Status.Fetch.StartedAt.Time))
145+
137146
err := a.updateStatus("marking fetch completed")
138147
if err != nil {
139148
return exec.NewCmdRunResultWithErr(err)
@@ -144,6 +153,8 @@ func (a *App) reconcileFetchTemplateDeploy() exec.CmdRunResult {
144153
}
145154
}
146155

156+
templateStartTime := time.Now()
157+
147158
tplResult := a.template(assetsPath)
148159

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

167+
a.timeMetrics.RegisterTemplateTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
168+
a.app.Status.Template.UpdatedAt.Sub(templateStartTime))
169+
156170
err = a.updateStatus("marking template completed")
157171
if err != nil {
158172
return exec.NewCmdRunResultWithErr(err)
@@ -201,6 +215,9 @@ func (a *App) updateLastDeploy(result exec.CmdRunResult) exec.CmdRunResult {
201215
},
202216
}
203217

218+
a.timeMetrics.RegisterDeployTime(a.app.Kind, a.app.Name, a.app.Namespace, a.isFirstReconcile,
219+
a.Status().Deploy.UpdatedAt.Sub(a.Status().Deploy.StartedAt.Time))
220+
204221
return result
205222
}
206223

pkg/app/app_template.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@ package app
55

66
import (
77
"fmt"
8-
"strings"
9-
"time"
10-
118
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
129
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
1310
ctltpl "github.com/vmware-tanzu/carvel-kapp-controller/pkg/template"
11+
"strings"
1412
)
1513

1614
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-
2215
if len(a.app.Spec.Template) == 0 {
2316
return exec.NewCmdRunResultWithErr(fmt.Errorf("Expected at least one template option"))
2417
}

pkg/metrics/app_metrics.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88

99
"github.com/prometheus/client_golang/prometheus"
10+
dto "github.com/prometheus/client_model/go"
1011
"sigs.k8s.io/controller-runtime/pkg/metrics"
1112
)
1213

@@ -131,3 +132,11 @@ func (am *AppMetrics) RegisterReconcileDeleteAttempt(appName string, namespace s
131132
func (am *AppMetrics) RegisterReconcileDeleteFailed(appName string, namespace string) {
132133
am.reconcileDeleteFailedTotal.WithLabelValues(appName, namespace).Inc()
133134
}
135+
136+
func (am *AppMetrics) GetReconcileAttemptCounterValue(appName, namespace string) int64 {
137+
var m = &dto.Metric{}
138+
if err := am.reconcileAttemptTotal.WithLabelValues(appName, namespace).Write(m); err != nil {
139+
return 0
140+
}
141+
return int64(m.Counter.GetValue())
142+
}

pkg/metrics/reconcile_time_metrics.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package metrics
33
import (
44
"github.com/prometheus/client_golang/prometheus"
55
"sigs.k8s.io/controller-runtime/pkg/metrics"
6+
"sync"
67
"time"
78
)
89

@@ -13,6 +14,10 @@ type ReconcileTimeMetrics struct {
1314
reconcileTemplateTimeSeconds *prometheus.GaugeVec
1415
}
1516

17+
var (
18+
timeMetricsOnce sync.Once
19+
)
20+
1621
func NewReconcileTimeMetrics() *ReconcileTimeMetrics {
1722
const (
1823
metricNamespace = "kappctrl_reconcile_time_seconds"
@@ -58,7 +63,7 @@ func NewReconcileTimeMetrics() *ReconcileTimeMetrics {
5863
}
5964

6065
func (tm *ReconcileTimeMetrics) RegisterAllMetrics() {
61-
once.Do(func() {
66+
timeMetricsOnce.Do(func() {
6267
metrics.Registry.MustRegister(
6368
tm.reconcileTimeSeconds,
6469
tm.reconcileFetchTimeSeconds,

0 commit comments

Comments
 (0)