Skip to content

Commit e6570a9

Browse files
authored
Merge pull request #570 from fluxcd/stale-metrics
Delete stale metrics on object delete
2 parents d52002a + e27a9e2 commit e6570a9

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/fluxcd/pkg/git v0.12.4
2424
github.com/fluxcd/pkg/git/gogit v0.12.1
2525
github.com/fluxcd/pkg/gittestserver v0.8.5
26-
github.com/fluxcd/pkg/runtime v0.41.0
26+
github.com/fluxcd/pkg/runtime v0.42.0
2727
github.com/fluxcd/pkg/ssh v0.8.1
2828
github.com/fluxcd/source-controller/api v1.0.1
2929
github.com/go-git/go-billy/v5 v5.4.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ github.com/fluxcd/pkg/git/gogit v0.12.1 h1:06jzHOTntYN5xCSQvyFXtLXdqoP8crLh7VYgt
9595
github.com/fluxcd/pkg/git/gogit v0.12.1/go.mod h1:Z4Ysp8VifKTvWpjJMKncJsgb2iBqHuIeK80VGjlU41Y=
9696
github.com/fluxcd/pkg/gittestserver v0.8.5 h1:EGqDF4240xPRgW1FFrQAs0Du7fZb8OGXC5qKDIqyXD8=
9797
github.com/fluxcd/pkg/gittestserver v0.8.5/go.mod h1:SyGEh+OBzFpdlTWWqv3XBkiLB42Iu+mijfIQ4hPlEZQ=
98-
github.com/fluxcd/pkg/runtime v0.41.0 h1:hjWUwVRCKDuGEUhovWrygt/6PRry4p278yKuJNgTfv8=
99-
github.com/fluxcd/pkg/runtime v0.41.0/go.mod h1:1GN+nxoQ7LmSsLJwjH8JW8pA27tBSO+KLH43HpywCDM=
98+
github.com/fluxcd/pkg/runtime v0.42.0 h1:a5DQ/f90YjoHBmiXZUpnp4bDSLORjInbmqP7K11L4uY=
99+
github.com/fluxcd/pkg/runtime v0.42.0/go.mod h1:p6A3xWVV8cKLLQW0N90GehKgGMMmbNYv+OSJ/0qB0vg=
100100
github.com/fluxcd/pkg/ssh v0.8.1 h1:v35y7Ks/+ABWce8RcnrC7psVIhf3EdCUNFJi5+tYOps=
101101
github.com/fluxcd/pkg/ssh v0.8.1/go.mod h1:M1ouDXuDG+QuhGB4JYEjCNCykNytLJGDhwKn9y4DEOE=
102102
github.com/fluxcd/pkg/version v0.2.2 h1:ZpVXECeLA5hIQMft11iLp6gN3cKcz6UNuVTQPw/bRdI=

internal/controller/imageupdateautomation_controller.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
110110
return ctrl.Result{}, client.IgnoreNotFound(err)
111111
}
112112

113+
defer func() {
114+
// Always record suspend, readiness and duration metrics.
115+
r.Metrics.RecordSuspend(ctx, &auto, auto.Spec.Suspend)
116+
r.Metrics.RecordReadiness(ctx, &auto)
117+
r.Metrics.RecordDuration(ctx, &auto, start)
118+
}()
119+
113120
// If the object is under deletion, record the readiness, and remove our finalizer.
114121
if !auto.ObjectMeta.DeletionTimestamp.IsZero() {
115122
controllerutil.RemoveFinalizer(&auto, imagev1.ImageUpdateAutomationFinalizer)
@@ -131,22 +138,13 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
131138
}
132139
}
133140

134-
// record suspension metrics
135-
r.RecordSuspend(ctx, &auto, auto.Spec.Suspend)
136-
137141
if auto.Spec.Suspend {
138142
log.Info("ImageUpdateAutomation is suspended, skipping automation run")
139143
return ctrl.Result{}, nil
140144
}
141145

142146
templateValues.AutomationObject = req.NamespacedName
143147

144-
defer func() {
145-
// Always record readiness and duration metrics
146-
r.Metrics.RecordReadiness(ctx, &auto)
147-
r.Metrics.RecordDuration(ctx, &auto, start)
148-
}()
149-
150148
// whatever else happens, we've now "seen" the reconcile
151149
// annotation if it's there
152150
if token, ok := meta.ReconcileAnnotationValue(auto.GetAnnotations()); ok {

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
feathelper "github.com/fluxcd/pkg/runtime/features"
4141
"github.com/fluxcd/pkg/runtime/leaderelection"
4242
"github.com/fluxcd/pkg/runtime/logger"
43+
"github.com/fluxcd/pkg/runtime/metrics"
4344
"github.com/fluxcd/pkg/runtime/pprof"
4445
"github.com/fluxcd/pkg/runtime/probes"
4546
sourcev1 "github.com/fluxcd/source-controller/api/v1"
@@ -184,7 +185,7 @@ func main() {
184185
os.Exit(1)
185186
}
186187

187-
metricsH := helper.MustMakeMetrics(mgr)
188+
metricsH := helper.NewMetrics(mgr, metrics.MustMakeRecorder(), imagev1.ImageUpdateAutomationFinalizer)
188189

189190
ctx := ctrl.SetupSignalHandler()
190191

0 commit comments

Comments
 (0)