@@ -30,9 +30,9 @@ import (
30
30
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
31
31
"k8s.io/apimachinery/pkg/util/uuid"
32
32
"k8s.io/client-go/util/workqueue"
33
+ ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
33
34
34
35
"sigs.k8s.io/controller-runtime/pkg/controller/priorityqueue"
35
- ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
36
36
logf "sigs.k8s.io/controller-runtime/pkg/log"
37
37
"sigs.k8s.io/controller-runtime/pkg/reconcile"
38
38
"sigs.k8s.io/controller-runtime/pkg/source"
@@ -101,7 +101,7 @@ type Controller[request comparable] struct {
101
101
func (c * Controller [request ]) Reconcile (ctx context.Context , req request ) (_ reconcile.Result , err error ) {
102
102
defer func () {
103
103
if r := recover (); r != nil {
104
- ctrlmetrics .ReconcilePanics .WithLabelValues ( c .Name ). Inc ( )
104
+ ctrlmetrics .ReconcilePanics .Inc ( map [ string ] string { "controller" : c .Name } )
105
105
106
106
if c .RecoverPanic == nil || * c .RecoverPanic {
107
107
for _ , fn := range utilruntime .PanicHandlers {
@@ -288,8 +288,8 @@ func (c *Controller[request]) processNextWorkItem(ctx context.Context) bool {
288
288
// period.
289
289
defer c .Queue .Done (obj )
290
290
291
- ctrlmetrics .ActiveWorkers .WithLabelValues ( c .Name ). Add ( 1 )
292
- defer ctrlmetrics .ActiveWorkers .WithLabelValues ( c .Name ). Add ( - 1 )
291
+ ctrlmetrics .ActiveWorkers .Set ( 1 , map [ string ] string { "controller" : c .Name } )
292
+ defer ctrlmetrics .ActiveWorkers .Set ( 0 , map [ string ] string { "controller" : c .Name } )
293
293
294
294
c .reconcileHandler (ctx , obj , priority )
295
295
return true
@@ -303,15 +303,15 @@ const (
303
303
)
304
304
305
305
func (c * Controller [request ]) initMetrics () {
306
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelError ). Add ( 0 )
307
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelRequeueAfter ). Add ( 0 )
308
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelRequeue ). Add ( 0 )
309
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelSuccess ). Add ( 0 )
310
- ctrlmetrics .ReconcileErrors .WithLabelValues ( c .Name ). Add ( 0 )
311
- ctrlmetrics .TerminalReconcileErrors .WithLabelValues ( c .Name ). Add ( 0 )
312
- ctrlmetrics .ReconcilePanics .WithLabelValues ( c .Name ). Add ( 0 )
313
- ctrlmetrics .WorkerCount .WithLabelValues ( c . Name ). Set (float64 (c .MaxConcurrentReconciles ))
314
- ctrlmetrics .ActiveWorkers .WithLabelValues ( c .Name ). Set ( 0 )
306
+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelError } )
307
+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelRequeueAfter } )
308
+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelRequeue } )
309
+ ctrlmetrics .ReconcileTotal .Add ( 0 , map [ string ] string { "controller" : c .Name , "result" : labelSuccess } )
310
+ ctrlmetrics .ReconcileErrors .Add ( 0 , map [ string ] string { "controller" : c .Name } )
311
+ ctrlmetrics .TerminalReconcileErrors .Add ( 0 , map [ string ] string { "controller" : c .Name } )
312
+ ctrlmetrics .ReconcilePanics .Add ( 0 , map [ string ] string { "controller" : c .Name } )
313
+ ctrlmetrics .WorkerCount .Set (float64 (c .MaxConcurrentReconciles ), map [ string ] string { "controller" : c . Name } )
314
+ ctrlmetrics .ActiveWorkers .Set ( 0 , map [ string ] string { "controller" : c .Name } )
315
315
}
316
316
317
317
func (c * Controller [request ]) reconcileHandler (ctx context.Context , req request , priority int ) {
@@ -335,12 +335,12 @@ func (c *Controller[request]) reconcileHandler(ctx context.Context, req request,
335
335
switch {
336
336
case err != nil :
337
337
if errors .Is (err , reconcile .TerminalError (nil )) {
338
- ctrlmetrics .TerminalReconcileErrors .WithLabelValues ( c .Name ). Inc ( )
338
+ ctrlmetrics .TerminalReconcileErrors .Inc ( map [ string ] string { "controller" : c .Name } )
339
339
} else {
340
340
c .Queue .AddWithOpts (priorityqueue.AddOpts {RateLimited : true , Priority : priority }, req )
341
341
}
342
- ctrlmetrics .ReconcileErrors .WithLabelValues ( c .Name ). Inc ( )
343
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelError ). Inc ( )
342
+ ctrlmetrics .ReconcileErrors .Inc ( map [ string ] string { "controller" : c .Name } )
343
+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelError } )
344
344
if ! result .IsZero () {
345
345
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" )
346
346
}
@@ -353,17 +353,17 @@ func (c *Controller[request]) reconcileHandler(ctx context.Context, req request,
353
353
// to result.RequestAfter
354
354
c .Queue .Forget (req )
355
355
c .Queue .AddWithOpts (priorityqueue.AddOpts {After : result .RequeueAfter , Priority : priority }, req )
356
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelRequeueAfter ). Inc ( )
356
+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelRequeueAfter } )
357
357
case result .Requeue :
358
358
log .V (5 ).Info ("Reconcile done, requeueing" )
359
359
c .Queue .AddWithOpts (priorityqueue.AddOpts {RateLimited : true , Priority : priority }, req )
360
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelRequeue ). Inc ( )
360
+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelRequeue } )
361
361
default :
362
362
log .V (5 ).Info ("Reconcile successful" )
363
363
// Finally, if no error occurs we Forget this item so it does not
364
364
// get queued again until another change happens.
365
365
c .Queue .Forget (req )
366
- ctrlmetrics .ReconcileTotal .WithLabelValues ( c .Name , labelSuccess ). Inc ( )
366
+ ctrlmetrics .ReconcileTotal .Inc ( map [ string ] string { "controller" : c .Name , "result" : labelSuccess } )
367
367
}
368
368
}
369
369
@@ -374,7 +374,7 @@ func (c *Controller[request]) GetLogger() logr.Logger {
374
374
375
375
// updateMetrics updates prometheus metrics within the controller.
376
376
func (c * Controller [request ]) updateMetrics (reconcileTime time.Duration ) {
377
- ctrlmetrics .ReconcileTime .WithLabelValues ( c . Name ). Observe (reconcileTime .Seconds ())
377
+ ctrlmetrics .ReconcileTime .Observe (reconcileTime .Seconds (), map [ string ] string { "controller" : c . Name } )
378
378
}
379
379
380
380
// ReconcileIDFromContext gets the reconcileID from the current context.
0 commit comments