Skip to content

Commit e6ba3be

Browse files
committed
feat: add metrics for sub-reconciler calls
1 parent 47d7d55 commit e6ba3be

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

controllers/acl_controller.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (r *ACLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
250250
}
251251

252252
if statusNeedsUpdate {
253-
err = r.Client.Status().Update(ctx, acl)
253+
err = r.Status().Update(ctx, acl)
254254
if err != nil {
255255
l.Error(err, "could not update status for ACL object")
256256
return ctrl.Result{}, err
@@ -269,7 +269,7 @@ func (r *ACLReconciler) setUnreadyStatus(ctx context.Context, acl *v1alpha1.ACL,
269269
acl.Status.Ready = false
270270
acl.Status.Reason = reason
271271

272-
err := r.Client.Status().Update(ctx, acl)
272+
err := r.Status().Update(ctx, acl)
273273
if err != nil {
274274
l.Error(err, "could not update acl status")
275275
}
@@ -556,7 +556,7 @@ func (r *ACLReconciler) ensureDNSEntry(ctx context.Context, host string) (*v1alp
556556
existingDNSEntry := &v1alpha1.ACLDNSEntry{}
557557

558558
resourceName := validResourceName(host)
559-
err := r.Client.Get(ctx, types.NamespacedName{
559+
err := r.Get(ctx, types.NamespacedName{
560560
Name: resourceName,
561561
}, existingDNSEntry)
562562

@@ -570,7 +570,7 @@ func (r *ACLReconciler) ensureDNSEntry(ctx context.Context, host string) (*v1alp
570570
},
571571
}
572572

573-
err = r.Client.Create(ctx, dnsEntry)
573+
err = r.Create(ctx, dnsEntry)
574574
if err != nil {
575575
l.Error(err, "could not create ACLDNSEntry object")
576576
return nil, err
@@ -582,11 +582,16 @@ func (r *ACLReconciler) ensureDNSEntry(ctx context.Context, host string) (*v1alp
582582
Resolver: r.Resolver,
583583
}
584584

585+
operationStart := time.Now()
585586
err = subReconciler.FillStatus(ctx, dnsEntry)
587+
operationDuration := time.Since(operationStart)
588+
subReconcilerTime.WithLabelValues("acl", "acldnsentry").Observe(operationDuration.Seconds())
586589
if err != nil {
590+
subReconcilerTotal.WithLabelValues("acl", "acldnsentry", "error").Inc()
587591
l.Error(err, "could not fill status for DNSEntry", "dnsEntryName", resourceName)
588592
return nil, err
589593
}
594+
subReconcilerTotal.WithLabelValues("acl", "acldnsentry", "success").Inc()
590595

591596
return dnsEntry, nil
592597
} else if err != nil {
@@ -602,7 +607,7 @@ func (r *ACLReconciler) ensureTsuruAppAddress(ctx context.Context, appName strin
602607

603608
existingTsuruAppAddress := &v1alpha1.TsuruAppAddress{}
604609
resourceName := validResourceName(appName)
605-
err := r.Client.Get(ctx, types.NamespacedName{
610+
err := r.Get(ctx, types.NamespacedName{
606611
Name: resourceName,
607612
}, existingTsuruAppAddress)
608613

@@ -616,7 +621,7 @@ func (r *ACLReconciler) ensureTsuruAppAddress(ctx context.Context, appName strin
616621
},
617622
}
618623

619-
err = r.Client.Create(ctx, tsuruAppAddress)
624+
err = r.Create(ctx, tsuruAppAddress)
620625
if err != nil {
621626
l.Error(err, "could not create ACLDNSEntry object")
622627
return nil, err
@@ -629,11 +634,16 @@ func (r *ACLReconciler) ensureTsuruAppAddress(ctx context.Context, appName strin
629634
TsuruAPI: r.TsuruAPI,
630635
}
631636

637+
operationStart := time.Now()
632638
err = subReconciler.FillStatus(ctx, tsuruAppAddress)
639+
operationDuration := time.Since(operationStart)
640+
subReconcilerTime.WithLabelValues("acl", "tsuruappaddress").Observe(operationDuration.Seconds())
633641
if err != nil {
642+
subReconcilerTotal.WithLabelValues("acl", "tsuruappaddress", "error").Inc()
634643
l.Error(err, "could not fill status of TsuruAppAddress", "tsuruAppName", resourceName)
635644
return nil, err
636645
}
646+
subReconcilerTotal.WithLabelValues("acl", "tsuruappaddress", "success").Inc()
637647

638648
return tsuruAppAddress, nil
639649
} else if err != nil {
@@ -649,7 +659,7 @@ func (r *ACLReconciler) ensureRpaasInstanceAddress(ctx context.Context, rpaasIns
649659

650660
existingRpaasInstanceAddress := &v1alpha1.RpaasInstanceAddress{}
651661
resourceName := validResourceName(rpaasInstance.ServiceName + "-" + rpaasInstance.Instance)
652-
err := r.Client.Get(ctx, types.NamespacedName{
662+
err := r.Get(ctx, types.NamespacedName{
653663
Name: resourceName,
654664
}, existingRpaasInstanceAddress)
655665

@@ -664,7 +674,7 @@ func (r *ACLReconciler) ensureRpaasInstanceAddress(ctx context.Context, rpaasIns
664674
},
665675
}
666676

667-
err = r.Client.Create(ctx, rpaasInstanceAddress)
677+
err = r.Create(ctx, rpaasInstanceAddress)
668678
if err != nil {
669679
l.Error(err, "could not create RpaasInstanceAddress object")
670680
return nil, err
@@ -677,11 +687,16 @@ func (r *ACLReconciler) ensureRpaasInstanceAddress(ctx context.Context, rpaasIns
677687
TsuruAPI: r.TsuruAPI,
678688
}
679689

690+
operationStart := time.Now()
680691
err = subReconciler.FillStatus(ctx, rpaasInstanceAddress)
692+
operationDuration := time.Since(operationStart)
693+
subReconcilerTime.WithLabelValues("acl", "rpaasinstanceaddress").Observe(operationDuration.Seconds())
681694
if err != nil {
695+
subReconcilerTotal.WithLabelValues("acl", "rpaasinstanceaddress", "error").Inc()
682696
l.Error(err, "could not fill status of RpaasInstanceAddress", "name", resourceName)
683697
return nil, err
684698
}
699+
subReconcilerTotal.WithLabelValues("acl", "rpaasinstanceaddress", "success").Inc()
685700
return rpaasInstanceAddress, err
686701
} else if err != nil {
687702
l.Error(err, "could not get RpaasInstanceAddress", "name", resourceName)
@@ -919,7 +934,7 @@ func (r *ACLReconciler) setupWatchers(ctrl controller.Controller) error {
919934

920935
func (r *ACLReconciler) reconcileRequestsForIndex(index, value string) []reconcile.Request {
921936
list := &v1alpha1.ACLList{}
922-
err := r.Client.List(context.Background(), list, &client.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{
937+
err := r.List(context.Background(), list, &client.ListOptions{FieldSelector: fields.SelectorFromSet(fields.Set{
923938
index: value,
924939
})})
925940
if err != nil {

controllers/acl_garbage_collector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type appACLKey struct {
2525
App string
2626
Namespace string
2727
}
28+
2829
type jobACLKey struct {
2930
Job string
3031
Namespace string

controllers/metrics.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package controllers
2+
3+
import (
4+
"github.com/prometheus/client_golang/prometheus"
5+
"sigs.k8s.io/controller-runtime/pkg/metrics"
6+
)
7+
8+
var subReconcilerTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
9+
Name: "subreconciler_reconcile_total",
10+
Help: "Total number of reconciliations per subcontroller",
11+
}, []string{"controller", "subcontroller", "result"})
12+
13+
var subReconcilerTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
14+
Name: "subreconciler_reconcile_time_seconds",
15+
Help: "Length of time per reconciliation per subcontroller per controller",
16+
Buckets: prometheus.ExponentialBuckets(0.001, 2, 20),
17+
}, []string{"controller", "subccontroller"})
18+
19+
func init() {
20+
metrics.Registry.MustRegister(subReconcilerTotal)
21+
metrics.Registry.MustRegister(subReconcilerTime)
22+
}

0 commit comments

Comments
 (0)