|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | +"time" |
| 5 | + |
| 6 | +"github.com/prometheus/client_golang/prometheus" |
| 7 | +internalmetrics "sigs.k8s.io/controller-runtime/pkg/internal/metrics" |
| 8 | +"sigs.k8s.io/controller-runtime/pkg/metrics" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | +// reconcileTotal is a prometheus counter metrics which holds the total |
| 13 | +// number of reconciliations per controller. It has two labels. controller label refers |
| 14 | +// to the controller name and result label refers to the reconcile result i.e |
| 15 | +// success, error, requeue, requeue_after. |
| 16 | +reconcileTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 17 | +Name: "controller_runtime_reconcile_total", |
| 18 | +Help: "Total number of reconciliations per controller", |
| 19 | +}, []string{"controller", "result"}) |
| 20 | + |
| 21 | +// reconcileErrors is a prometheus counter metrics which holds the total |
| 22 | +// number of errors from the Reconciler. |
| 23 | +reconcileErrors = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 24 | +Name: "controller_runtime_reconcile_errors_total", |
| 25 | +Help: "Total number of reconciliation errors per controller", |
| 26 | +}, []string{"controller"}) |
| 27 | + |
| 28 | +// terminalReconcileErrors is a prometheus counter metrics which holds the total |
| 29 | +// number of terminal errors from the Reconciler. |
| 30 | +terminalReconcileErrors = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 31 | +Name: "controller_runtime_terminal_reconcile_errors_total", |
| 32 | +Help: "Total number of terminal reconciliation errors per controller", |
| 33 | +}, []string{"controller"}) |
| 34 | + |
| 35 | +// reconcilePanics is a prometheus counter metrics which holds the total |
| 36 | +// number of panics from the Reconciler. |
| 37 | +reconcilePanics = prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 38 | +Name: "controller_runtime_reconcile_panics_total", |
| 39 | +Help: "Total number of reconciliation panics per controller", |
| 40 | +}, []string{"controller"}) |
| 41 | + |
| 42 | +// reconcileTime is a prometheus metric which keeps track of the duration |
| 43 | +// of reconciliations. |
| 44 | +reconcileTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 45 | +Name: "controller_runtime_reconcile_time_seconds", |
| 46 | +Help: "Length of time per reconciliation per controller", |
| 47 | +Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, |
| 48 | +1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40, 50, 60}, |
| 49 | +NativeHistogramBucketFactor: 1.1, |
| 50 | +NativeHistogramMaxBucketNumber: 100, |
| 51 | +NativeHistogramMinResetDuration: 1 * time.Hour, |
| 52 | +}, []string{"controller"}) |
| 53 | + |
| 54 | +// workerCount is a prometheus metric which holds the number of |
| 55 | +// concurrent reconciles per controller. |
| 56 | +workerCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 57 | +Name: "controller_runtime_max_concurrent_reconciles", |
| 58 | +Help: "Maximum number of concurrent reconciles per controller", |
| 59 | +}, []string{"controller"}) |
| 60 | + |
| 61 | +// activeWorkers is a prometheus metric which holds the number |
| 62 | +// of active workers per controller. |
| 63 | +activeWorkers = prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 64 | +Name: "controller_runtime_active_workers", |
| 65 | +Help: "Number of currently used workers per controller", |
| 66 | +}, []string{"controller"}) |
| 67 | +) |
| 68 | + |
| 69 | +// ControllerMetricsProvider is an interface that provides methods for firing controller metrics |
| 70 | +type ControllerMetricsProvider interface { |
| 71 | +// ReconcileTotal is a prometheus counter metrics which holds the total |
| 72 | +// number of reconciliations per controller. It has two labels. controller label refers |
| 73 | +// to the controller name and result label refers to the reconcile result i.e |
| 74 | +// success, error, requeue, requeue_after. |
| 75 | +ReconcileTotal() internalmetrics.CounterMetric |
| 76 | +// ReconcileErrors is a prometheus counter metrics which holds the total |
| 77 | +// number of errors from the Reconciler. |
| 78 | +ReconcileErrors() internalmetrics.CounterMetric |
| 79 | +// TerminalReconcileErrors is a prometheus counter metrics which holds the total |
| 80 | +// number of terminal errors from the Reconciler. |
| 81 | +TerminalReconcileErrors() internalmetrics.CounterMetric |
| 82 | +// ReconcilePanics is a prometheus counter metrics which holds the total |
| 83 | +// number of panics from the Reconciler. |
| 84 | +ReconcilePanics() internalmetrics.CounterMetric |
| 85 | +// ReconcileTime is a prometheus metric which keeps track of the duration |
| 86 | +// of reconciliations. |
| 87 | +ReconcileTime() internalmetrics.HistogramMetric |
| 88 | +// WorkerCount is a prometheus metric which holds the number of |
| 89 | +// concurrent reconciles per controller. |
| 90 | +WorkerCount() internalmetrics.GaugeMetric |
| 91 | +// ActiveWorkers is a prometheus metric which holds the number |
| 92 | +// of active workers per controller. |
| 93 | +ActiveWorkers() internalmetrics.GaugeMetric |
| 94 | +} |
| 95 | + |
| 96 | +// PrometheusProvider is a metrics.ControllerMetricsProvider and a metrics.LeaderElectionMetricsProvider |
| 97 | +// that registers and fires prometheus metrics in response to leader election and controller events |
| 98 | +type PrometheusProvider struct { |
| 99 | +reconcileTotal *prometheus.CounterVec |
| 100 | +reconcileErrors *prometheus.CounterVec |
| 101 | +terminalReconcileErrors *prometheus.CounterVec |
| 102 | +reconcilePanics *prometheus.CounterVec |
| 103 | +reconcileTime *prometheus.HistogramVec |
| 104 | +workerCount *prometheus.GaugeVec |
| 105 | +activeWorkers *prometheus.GaugeVec |
| 106 | +} |
| 107 | + |
| 108 | +// NewPrometheusProvider creates a PrometheusProvider |
| 109 | +func NewPrometheusProvider() *PrometheusProvider { |
| 110 | +return &PrometheusProvider{ |
| 111 | +reconcileTotal: reconcileTotal, |
| 112 | +reconcileErrors: reconcileErrors, |
| 113 | +terminalReconcileErrors: terminalReconcileErrors, |
| 114 | +reconcilePanics: reconcilePanics, |
| 115 | +reconcileTime: reconcileTime, |
| 116 | +workerCount: workerCount, |
| 117 | +activeWorkers: activeWorkers, |
| 118 | +} |
| 119 | +} |
| 120 | + |
| 121 | +// ReconcileTotal returns a Prometheus counter that fulfills the CounterMetric interface |
| 122 | +func (p PrometheusProvider) ReconcileTotal() internalmetrics.CounterMetric { |
| 123 | +return &internalmetrics.PrometheusCounterAdapter{CounterVec: p.reconcileTotal} |
| 124 | +} |
| 125 | + |
| 126 | +// ReconcileErrors returns a Prometheus counter that fulfills the CounterMetric interface |
| 127 | +func (p PrometheusProvider) ReconcileErrors() internalmetrics.CounterMetric { |
| 128 | +return &internalmetrics.PrometheusCounterAdapter{CounterVec: p.reconcileErrors} |
| 129 | +} |
| 130 | + |
| 131 | +// TerminalReconcileErrors returns a Prometheus counter that fulfills the CounterMetric interface |
| 132 | +func (p PrometheusProvider) TerminalReconcileErrors() internalmetrics.CounterMetric { |
| 133 | +return &internalmetrics.PrometheusCounterAdapter{CounterVec: p.terminalReconcileErrors} |
| 134 | +} |
| 135 | + |
| 136 | +// ReconcilePanics returns a Prometheus counter that fulfills the CounterMetric interface |
| 137 | +func (p PrometheusProvider) ReconcilePanics() internalmetrics.CounterMetric { |
| 138 | +return &internalmetrics.PrometheusCounterAdapter{CounterVec: p.reconcilePanics} |
| 139 | +} |
| 140 | + |
| 141 | +// ReconcileTime returns a Prometheus histogram that fulfills the ObservationMetric interface |
| 142 | +func (p PrometheusProvider) ReconcileTime() internalmetrics.HistogramMetric { |
| 143 | +return &internalmetrics.PrometheusHistogramAdapter{HistogramVec: p.reconcileTime} |
| 144 | +} |
| 145 | + |
| 146 | +// WorkerCount returns a Prometheus gauge that fulfills the GaugeMetric interface |
| 147 | +func (p PrometheusProvider) WorkerCount() internalmetrics.GaugeMetric { |
| 148 | +return &internalmetrics.PrometheusGaugeAdapter{GaugeVec: p.workerCount} |
| 149 | +} |
| 150 | + |
| 151 | +// ActiveWorkers returns a Prometheus gauge that fulfills the GaugeMetric interface |
| 152 | +func (p PrometheusProvider) ActiveWorkers() internalmetrics.GaugeMetric { |
| 153 | +return &internalmetrics.PrometheusGaugeAdapter{GaugeVec: p.activeWorkers} |
| 154 | +} |
| 155 | + |
| 156 | +func init() { |
| 157 | +metrics.Registry.MustRegister( |
| 158 | +reconcileTotal, |
| 159 | +reconcileErrors, |
| 160 | +terminalReconcileErrors, |
| 161 | +reconcilePanics, |
| 162 | +reconcileTime, |
| 163 | +workerCount, |
| 164 | +activeWorkers, |
| 165 | +) |
| 166 | +} |
| 167 | + |
| 168 | +var controllerMetricsProvider ControllerMetricsProvider = NewPrometheusProvider() |
| 169 | + |
| 170 | +// SetControllerMetricsProvider assigns a provider to the ControllerMetricsProvider for exposing controller metrics. |
| 171 | +// The PrometheusProvider will be used by default if the provider is not overridden |
| 172 | +func SetControllerMetricsProvider(provider ControllerMetricsProvider) { |
| 173 | +controllerMetricsProvider = provider |
| 174 | +} |
| 175 | + |
| 176 | +// GetControllerMetricsProvider returns the controller metrics provider being used by the controller reconciliation |
| 177 | +func GetControllerMetricsProvider() ControllerMetricsProvider { |
| 178 | +return controllerMetricsProvider |
| 179 | +} |
0 commit comments