@@ -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
+ "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"
@@ -89,6 +89,9 @@ type Controller[request comparable] struct {
89
89
// outside the context of a reconciliation.
90
90
LogConstructor func (request * request ) logr.Logger
91
91
92
+ // MetricsProvider is used to route metrics that are fired due to controller reconciles
93
+ MetricsProvider metrics.ControllerMetricsProvider
94
+
92
95
// RecoverPanic indicates whether the panic caused by reconcile should be recovered.
93
96
// Defaults to true.
94
97
RecoverPanic * bool
@@ -101,7 +104,7 @@ type Controller[request comparable] struct {
101
104
func (c * Controller [request ]) Reconcile (ctx context.Context , req request ) (_ reconcile.Result , err error ) {
102
105
defer func () {
103
106
if r := recover (); r != nil {
104
- ctrlmetrics . ReconcilePanics . WithLabelValues ( c . Name ).Inc ()
107
+ c . MetricsProvider . ReconcilePanics ( ).Inc (map [ string ] string { labelKeyController : c . Name } )
105
108
106
109
if c .RecoverPanic == nil || * c .RecoverPanic {
107
110
for _ , fn := range utilruntime .PanicHandlers {
@@ -294,30 +297,32 @@ func (c *Controller[request]) processNextWorkItem(ctx context.Context) bool {
294
297
// period.
295
298
defer c .Queue .Done (obj )
296
299
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 )
299
302
300
303
c .reconcileHandler (ctx , obj , priority )
301
304
return true
302
305
}
303
306
304
307
const (
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"
309
314
)
310
315
311
316
func (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 )
321
326
}
322
327
323
328
func (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,
341
346
switch {
342
347
case err != nil :
343
348
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 } )
345
350
} else {
346
351
c .Queue .AddWithOpts (priorityqueue.AddOpts {RateLimited : true , Priority : priority }, req )
347
352
}
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 } )
350
355
if ! result .IsZero () {
351
356
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" )
352
357
}
@@ -359,17 +364,17 @@ func (c *Controller[request]) reconcileHandler(ctx context.Context, req request,
359
364
// to result.RequestAfter
360
365
c .Queue .Forget (req )
361
366
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 } )
363
368
case result .Requeue : //nolint: staticcheck // We have to handle it until it is removed
364
369
log .V (5 ).Info ("Reconcile done, requeueing" )
365
370
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 } )
367
372
default :
368
373
log .V (5 ).Info ("Reconcile successful" )
369
374
// Finally, if no error occurs we Forget this item so it does not
370
375
// get queued again until another change happens.
371
376
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 } )
373
378
}
374
379
}
375
380
@@ -380,7 +385,7 @@ func (c *Controller[request]) GetLogger() logr.Logger {
380
385
381
386
// updateMetrics updates prometheus metrics within the controller.
382
387
func (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 ())
384
389
}
385
390
386
391
// ReconcileIDFromContext gets the reconcileID from the current context.
0 commit comments