@@ -30,9 +30,9 @@ import (
3030 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3131 "k8s.io/apimachinery/pkg/util/uuid"
3232 "k8s.io/client-go/util/workqueue"
33+ "sigs.k8s.io/controller-runtime/pkg/metrics"
3334
3435 "sigs.k8s.io/controller-runtime/pkg/controller/priorityqueue"
35- ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
3636 logf "sigs.k8s.io/controller-runtime/pkg/log"
3737 "sigs.k8s.io/controller-runtime/pkg/reconcile"
3838 "sigs.k8s.io/controller-runtime/pkg/source"
@@ -89,6 +89,9 @@ type Controller[request comparable] struct {
8989 // outside the context of a reconciliation.
9090 LogConstructor func (request * request ) logr.Logger
9191
92+ // MetricsProvider is used to route metrics that are fired due to controller reconciles
93+ MetricsProvider metrics.ControllerMetricsProvider
94+
9295 // RecoverPanic indicates whether the panic caused by reconcile should be recovered.
9396 // Defaults to true.
9497 RecoverPanic * bool
@@ -101,7 +104,7 @@ type Controller[request comparable] struct {
101104func (c * Controller [request ]) Reconcile (ctx context.Context , req request ) (_ reconcile.Result , err error ) {
102105 defer func () {
103106 if r := recover (); r != nil {
104- ctrlmetrics . ReconcilePanics . WithLabelValues ( c . Name ).Inc ()
107+ c . MetricsProvider . ReconcilePanics ( ).Inc (map [ string ] string { labelKeyController : c . Name } )
105108
106109 if c .RecoverPanic == nil || * c .RecoverPanic {
107110 for _ , fn := range utilruntime .PanicHandlers {
@@ -294,30 +297,32 @@ func (c *Controller[request]) processNextWorkItem(ctx context.Context) bool {
294297 // period.
295298 defer c .Queue .Done (obj )
296299
297- ctrlmetrics . ActiveWorkers . WithLabelValues ( c . Name ).Add (1 )
298- defer ctrlmetrics . ActiveWorkers . WithLabelValues ( c . Name ).Add (- 1 )
300+ c . MetricsProvider . ActiveWorkers ( ).Add (map [ string ] string { labelKeyController : c . Name }, 1 )
301+ defer c . MetricsProvider . ActiveWorkers ( ).Add (map [ string ] string { labelKeyController : c . Name }, - 1 )
299302
300303 c .reconcileHandler (ctx , obj , priority )
301304 return true
302305}
303306
304307const (
305- labelError = "error"
306- labelRequeueAfter = "requeue_after"
307- labelRequeue = "requeue"
308- labelSuccess = "success"
308+ labelKeyController = "controller"
309+ labelKeyResult = "result"
310+ labelError = "error"
311+ labelRequeueAfter = "requeue_after"
312+ labelRequeue = "requeue"
313+ labelSuccess = "success"
309314)
310315
311316func (c * Controller [request ]) initMetrics () {
312- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelError ). Add ( 0 )
313- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelRequeueAfter ). Add ( 0 )
314- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelRequeue ). Add ( 0 )
315- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelSuccess ). Add ( 0 )
316- ctrlmetrics . ReconcileErrors . WithLabelValues ( c . Name ).Add (0 )
317- ctrlmetrics . TerminalReconcileErrors . WithLabelValues ( c . Name ).Add (0 )
318- ctrlmetrics . ReconcilePanics . WithLabelValues ( c . Name ).Add (0 )
319- ctrlmetrics . WorkerCount . WithLabelValues ( c . Name ).Set (float64 (c .MaxConcurrentReconciles ))
320- ctrlmetrics . ActiveWorkers . WithLabelValues ( c . Name ).Set (0 )
317+ c . MetricsProvider . ReconcileTotal (). Add ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelError }, 0 )
318+ c . MetricsProvider . ReconcileTotal (). Add ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelRequeueAfter }, 0 )
319+ c . MetricsProvider . ReconcileTotal (). Add ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelRequeue }, 0 )
320+ c . MetricsProvider . ReconcileTotal (). Add ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelSuccess }, 0 )
321+ c . MetricsProvider . ReconcileErrors ( ).Add (map [ string ] string { labelKeyController : c . Name }, 0 )
322+ c . MetricsProvider . TerminalReconcileErrors ( ).Add (map [ string ] string { labelKeyController : c . Name }, 0 )
323+ c . MetricsProvider . ReconcilePanics ( ).Add (map [ string ] string { labelKeyController : c . Name }, 0 )
324+ c . MetricsProvider . WorkerCount ( ).Set (map [ string ] string { labelKeyController : c . Name }, float64 (c .MaxConcurrentReconciles ))
325+ c . MetricsProvider . ActiveWorkers ( ).Set (map [ string ] string { labelKeyController : c . Name }, 0 )
321326}
322327
323328func (c * Controller [request ]) reconcileHandler (ctx context.Context , req request , priority int ) {
@@ -341,12 +346,12 @@ func (c *Controller[request]) reconcileHandler(ctx context.Context, req request,
341346 switch {
342347 case err != nil :
343348 if errors .Is (err , reconcile .TerminalError (nil )) {
344- ctrlmetrics . TerminalReconcileErrors . WithLabelValues ( c . Name ).Inc ()
349+ c . MetricsProvider . TerminalReconcileErrors ( ).Inc (map [ string ] string { "controller" : c . Name } )
345350 } else {
346351 c .Queue .AddWithOpts (priorityqueue.AddOpts {RateLimited : true , Priority : priority }, req )
347352 }
348- ctrlmetrics . ReconcileErrors . WithLabelValues ( c . Name ).Inc ()
349- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelError ). Inc ( )
353+ c . MetricsProvider . ReconcileErrors ( ).Inc (map [ string ] string { labelKeyController : c . Name } )
354+ c . MetricsProvider . ReconcileTotal (). Inc ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelError } )
350355 if ! result .IsZero () {
351356 log .Info ("Warning: Reconciler returned both a non-zero result and a non-nil error. The result will always be ignored if the error is non-nil and the non-nil error causes requeuing with exponential backoff. For more details, see: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/reconcile#Reconciler" )
352357 }
@@ -359,17 +364,17 @@ func (c *Controller[request]) reconcileHandler(ctx context.Context, req request,
359364 // to result.RequestAfter
360365 c .Queue .Forget (req )
361366 c .Queue .AddWithOpts (priorityqueue.AddOpts {After : result .RequeueAfter , Priority : priority }, req )
362- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelRequeueAfter ). Inc ( )
367+ c . MetricsProvider . ReconcileTotal (). Inc ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelRequeueAfter } )
363368 case result .Requeue : //nolint: staticcheck // We have to handle it until it is removed
364369 log .V (5 ).Info ("Reconcile done, requeueing" )
365370 c .Queue .AddWithOpts (priorityqueue.AddOpts {RateLimited : true , Priority : priority }, req )
366- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelRequeue ). Inc ( )
371+ c . MetricsProvider . ReconcileTotal (). Inc ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelRequeue } )
367372 default :
368373 log .V (5 ).Info ("Reconcile successful" )
369374 // Finally, if no error occurs we Forget this item so it does not
370375 // get queued again until another change happens.
371376 c .Queue .Forget (req )
372- ctrlmetrics . ReconcileTotal . WithLabelValues ( c .Name , labelSuccess ). Inc ( )
377+ c . MetricsProvider . ReconcileTotal (). Inc ( map [ string ] string { labelKeyController : c .Name , labelKeyResult : labelSuccess } )
373378 }
374379}
375380
@@ -380,7 +385,7 @@ func (c *Controller[request]) GetLogger() logr.Logger {
380385
381386// updateMetrics updates prometheus metrics within the controller.
382387func (c * Controller [request ]) updateMetrics (reconcileTime time.Duration ) {
383- ctrlmetrics . ReconcileTime . WithLabelValues ( c . Name ).Observe (reconcileTime .Seconds ())
388+ c . MetricsProvider . ReconcileTime ( ).Observe (map [ string ] string { labelKeyController : c . Name }, reconcileTime .Seconds ())
384389}
385390
386391// ReconcileIDFromContext gets the reconcileID from the current context.
0 commit comments