@@ -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+ ctrlmetrics "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"
@@ -101,7 +101,7 @@ type Controller[request comparable] struct {
101101func (c * Controller [request ]) Reconcile (ctx context.Context , req request ) (_ reconcile.Result , err error ) {
102102 defer func () {
103103 if r := recover (); r != nil {
104- ctrlmetrics .ReconcilePanics .WithLabelValues ( c .Name ). Inc ( )
104+ ctrlmetrics .ReconcilePanics .Inc ( map [ string ] string { "controller" : c .Name } )
105105
106106 if c .RecoverPanic == nil || * c .RecoverPanic {
107107 for _ , fn := range utilruntime .PanicHandlers {
@@ -294,8 +294,8 @@ func (c *Controller[request]) processNextWorkItem(ctx context.Context) bool {
294294 // period.
295295 defer c .Queue .Done (obj )
296296
297- ctrlmetrics .ActiveWorkers .WithLabelValues ( c .Name ). Add ( 1 )
298- defer ctrlmetrics .ActiveWorkers .WithLabelValues ( c .Name ). Add ( - 1 )
297+ ctrlmetrics .ActiveWorkers .Set ( 1 , map [ string ] string { "controller" : c .Name } )
298+ defer ctrlmetrics .ActiveWorkers .Set ( 0 , map [ string ] string { "controller" : c .Name } )
299299
300300 c .reconcileHandler (ctx , obj , priority )
301301 return true
@@ -309,15 +309,15 @@ const (
309309)
310310
311311func (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 )
312+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelError } )
313+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelRequeueAfter } )
314+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelRequeue } )
315+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelSuccess } )
316+ ctrlmetrics .ReconcileErrors .Add ( 0 , map [ string ] string { "controller" : c .Name } )
317+ ctrlmetrics .TerminalReconcileErrors .Add ( 0 , map [ string ] string { "controller" : c .Name } )
318+ ctrlmetrics .ReconcilePanics .Add ( 0 , map [ string ] string { "controller" : c .Name } )
319+ ctrlmetrics .WorkerCount .Set (float64 (c .MaxConcurrentReconciles ), map [ string ] string { "controller" : c . Name } )
320+ ctrlmetrics .ActiveWorkers .Set ( 0 , map [ string ] string { "controller" : c .Name } )
321321}
322322
323323func (c * Controller [request ]) reconcileHandler (ctx context.Context , req request , priority int ) {
@@ -341,12 +341,12 @@ func (c *Controller[request]) reconcileHandler(ctx context.Context, req request,
341341 switch {
342342 case err != nil :
343343 if errors .Is (err , reconcile .TerminalError (nil )) {
344- ctrlmetrics .TerminalReconcileErrors .WithLabelValues ( c .Name ). Inc ( )
344+ ctrlmetrics .TerminalReconcileErrors .Inc ( map [ string ] string { "controller" : c .Name } )
345345 } else {
346346 c .Queue .AddWithOpts (priorityqueue.AddOpts {RateLimited : true , Priority : priority }, req )
347347 }
348- ctrlmetrics .ReconcileErrors .WithLabelValues ( c .Name ). Inc ( )
349- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelError ). Inc ( )
348+ ctrlmetrics .ReconcileErrors .Inc ( map [ string ] string { "controller" : c .Name } )
349+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelError } )
350350 if ! result .IsZero () {
351351 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" )
352352 }
@@ -359,17 +359,17 @@ func (c *Controller[request]) reconcileHandler(ctx context.Context, req request,
359359 // to result.RequestAfter
360360 c .Queue .Forget (req )
361361 c .Queue .AddWithOpts (priorityqueue.AddOpts {After : result .RequeueAfter , Priority : priority }, req )
362- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelRequeueAfter ). Inc ( )
362+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelRequeueAfter } )
363363 case result .Requeue : //nolint: staticcheck // We have to handle it until it is removed
364364 log .V (5 ).Info ("Reconcile done, requeueing" )
365365 c .Queue .AddWithOpts (priorityqueue.AddOpts {RateLimited : true , Priority : priority }, req )
366- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelRequeue ). Inc ( )
366+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelRequeue } )
367367 default :
368368 log .V (5 ).Info ("Reconcile successful" )
369369 // Finally, if no error occurs we Forget this item so it does not
370370 // get queued again until another change happens.
371371 c .Queue .Forget (req )
372- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelSuccess ). Inc ( )
372+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelSuccess } )
373373 }
374374}
375375
@@ -380,7 +380,7 @@ func (c *Controller[request]) GetLogger() logr.Logger {
380380
381381// updateMetrics updates prometheus metrics within the controller.
382382func (c * Controller [request ]) updateMetrics (reconcileTime time.Duration ) {
383- ctrlmetrics .ReconcileTime .WithLabelValues ( c . Name ). Observe (reconcileTime .Seconds ())
383+ ctrlmetrics .ReconcileTime .Observe (reconcileTime .Seconds (), map [ string ] string { "controller" : c . Name } )
384384}
385385
386386// ReconcileIDFromContext gets the reconcileID from the current context.
0 commit comments