|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/prometheus/client_golang/prometheus" |
| 5 | + "github.com/prometheus/client_golang/prometheus/collectors" |
| 6 | + ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 7 | +) |
| 8 | + |
| 9 | +// runtimeMetrics defines the controller-runtime, workqueue, REST client and |
| 10 | +// leader election series an operator exposes, with the same names, label sets |
| 11 | +// and bucket layouts as the real ones. The real vectors live in |
| 12 | +// controller-runtime's internal packages; runtime_test.go guards this copy |
| 13 | +// against drift. |
| 14 | +type runtimeMetrics struct { |
| 15 | + reconcileTotal *prometheus.CounterVec |
| 16 | + reconcileErrors *prometheus.CounterVec |
| 17 | + reconcilePanics *prometheus.CounterVec |
| 18 | + reconcileTime *prometheus.HistogramVec |
| 19 | + maxConcurrent *prometheus.GaugeVec |
| 20 | + activeWorkers *prometheus.GaugeVec |
| 21 | + queueDepth *prometheus.GaugeVec |
| 22 | + queueAdds *prometheus.CounterVec |
| 23 | + queueDuration *prometheus.HistogramVec |
| 24 | + workDuration *prometheus.HistogramVec |
| 25 | + queueUnfinished *prometheus.GaugeVec |
| 26 | + queueLongestRunning *prometheus.GaugeVec |
| 27 | + queueRetries *prometheus.CounterVec |
| 28 | + restRequests *prometheus.CounterVec |
| 29 | + leader *prometheus.GaugeVec |
| 30 | +} |
| 31 | + |
| 32 | +// Label names and the reconcile result value the lookalike series share with |
| 33 | +// controller-runtime. |
| 34 | +const ( |
| 35 | + labelController = "controller" |
| 36 | + labelName = "name" |
| 37 | + resultError = "error" |
| 38 | +) |
| 39 | + |
| 40 | +// reconcileTimeBuckets mirrors controller-runtime's ReconcileTime histogram. |
| 41 | +var reconcileTimeBuckets = []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, |
| 42 | + 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} |
| 43 | + |
| 44 | +func newRuntimeMetrics(reg prometheus.Registerer) *runtimeMetrics { |
| 45 | + m := &runtimeMetrics{ |
| 46 | + reconcileTotal: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 47 | + Name: "controller_runtime_reconcile_total", |
| 48 | + Help: "Total number of reconciliations per controller", |
| 49 | + }, []string{labelController, "result"}), |
| 50 | + reconcileErrors: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 51 | + Name: "controller_runtime_reconcile_errors_total", |
| 52 | + Help: "Total number of reconciliation errors per controller", |
| 53 | + }, []string{labelController}), |
| 54 | + reconcilePanics: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 55 | + Name: "controller_runtime_reconcile_panics_total", |
| 56 | + Help: "Total number of reconciliation panics per controller", |
| 57 | + }, []string{labelController}), |
| 58 | + reconcileTime: prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 59 | + Name: "controller_runtime_reconcile_time_seconds", |
| 60 | + Help: "Length of time per reconciliation per controller", |
| 61 | + Buckets: reconcileTimeBuckets, |
| 62 | + }, []string{labelController}), |
| 63 | + maxConcurrent: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 64 | + Name: "controller_runtime_max_concurrent_reconciles", |
| 65 | + Help: "Maximum number of concurrent reconciles per controller", |
| 66 | + }, []string{labelController}), |
| 67 | + activeWorkers: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 68 | + Name: "controller_runtime_active_workers", |
| 69 | + Help: "Number of currently used workers per controller", |
| 70 | + }, []string{labelController}), |
| 71 | + queueDepth: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 72 | + Subsystem: ctrlmetrics.WorkQueueSubsystem, Name: ctrlmetrics.DepthKey, |
| 73 | + Help: "Current depth of workqueue by workqueue and priority", |
| 74 | + }, []string{labelName, labelController, "priority"}), |
| 75 | + queueAdds: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 76 | + Subsystem: ctrlmetrics.WorkQueueSubsystem, Name: ctrlmetrics.AddsKey, |
| 77 | + Help: "Total number of adds handled by workqueue", |
| 78 | + }, []string{labelName, labelController}), |
| 79 | + queueDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 80 | + Subsystem: ctrlmetrics.WorkQueueSubsystem, Name: ctrlmetrics.QueueLatencyKey, |
| 81 | + Help: "How long in seconds an item stays in workqueue before being requested", |
| 82 | + Buckets: prometheus.ExponentialBuckets(10e-9, 10, 12), |
| 83 | + }, []string{labelName, labelController}), |
| 84 | + workDuration: prometheus.NewHistogramVec(prometheus.HistogramOpts{ |
| 85 | + Subsystem: ctrlmetrics.WorkQueueSubsystem, Name: ctrlmetrics.WorkDurationKey, |
| 86 | + Help: "How long in seconds processing an item from workqueue takes.", |
| 87 | + Buckets: prometheus.ExponentialBuckets(10e-9, 10, 12), |
| 88 | + }, []string{labelName, labelController}), |
| 89 | + queueUnfinished: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 90 | + Subsystem: ctrlmetrics.WorkQueueSubsystem, Name: ctrlmetrics.UnfinishedWorkKey, |
| 91 | + Help: "How many seconds of work has been done that " + |
| 92 | + "is in progress and hasn't been observed by work_duration. Large " + |
| 93 | + "values indicate stuck threads. One can deduce the number of stuck " + |
| 94 | + "threads by observing the rate at which this increases.", |
| 95 | + }, []string{labelName, labelController}), |
| 96 | + queueLongestRunning: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 97 | + Subsystem: ctrlmetrics.WorkQueueSubsystem, Name: ctrlmetrics.LongestRunningProcessorKey, |
| 98 | + Help: "How many seconds has the longest running " + |
| 99 | + "processor for workqueue been running.", |
| 100 | + }, []string{labelName, labelController}), |
| 101 | + queueRetries: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 102 | + Subsystem: ctrlmetrics.WorkQueueSubsystem, Name: ctrlmetrics.RetriesKey, |
| 103 | + Help: "Total number of items added to the workqueue with a non-zero delay (rate-limited requeues, explicit RequeueAfter or AddAfter calls)", |
| 104 | + }, []string{labelName, labelController}), |
| 105 | + restRequests: prometheus.NewCounterVec(prometheus.CounterOpts{ |
| 106 | + Name: "rest_client_requests_total", |
| 107 | + Help: "Number of HTTP requests, partitioned by status code, method, and host.", |
| 108 | + }, []string{"code", "method", "host"}), |
| 109 | + leader: prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 110 | + Name: "leader_election_master_status", |
| 111 | + Help: "Gauge of if the reporting system is master of the relevant lease, 0 indicates backup, 1 indicates master. 'name' is the string used to identify the lease. Please make sure to group by name.", |
| 112 | + }, []string{labelName}), |
| 113 | + } |
| 114 | + reg.MustRegister( |
| 115 | + m.reconcileTotal, m.reconcileErrors, m.reconcilePanics, m.reconcileTime, m.maxConcurrent, m.activeWorkers, |
| 116 | + m.queueDepth, m.queueAdds, m.queueDuration, m.workDuration, m.queueUnfinished, m.queueLongestRunning, m.queueRetries, |
| 117 | + m.restRequests, m.leader, |
| 118 | + collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), |
| 119 | + collectors.NewGoCollector(), |
| 120 | + ) |
| 121 | + return m |
| 122 | +} |
| 123 | + |
| 124 | +// initController creates the zero-valued series controller-runtime creates |
| 125 | +// when a controller starts, so panels show a flat line instead of no data. |
| 126 | +func (m *runtimeMetrics) initController(name string, maxConcurrent int) { |
| 127 | + for _, result := range []string{resultError, "requeue_after", "requeue", "success"} { |
| 128 | + m.reconcileTotal.WithLabelValues(name, result).Add(0) |
| 129 | + } |
| 130 | + m.reconcileErrors.WithLabelValues(name).Add(0) |
| 131 | + m.reconcilePanics.WithLabelValues(name).Add(0) |
| 132 | + m.reconcileTime.WithLabelValues(name) |
| 133 | + m.maxConcurrent.WithLabelValues(name).Set(float64(maxConcurrent)) |
| 134 | + m.activeWorkers.WithLabelValues(name).Set(0) |
| 135 | + m.queueDepth.WithLabelValues(name, name, "0").Set(0) |
| 136 | + m.queueAdds.WithLabelValues(name, name).Add(0) |
| 137 | + m.queueDuration.WithLabelValues(name, name) |
| 138 | + m.workDuration.WithLabelValues(name, name) |
| 139 | + m.queueUnfinished.WithLabelValues(name, name).Set(0) |
| 140 | + m.queueLongestRunning.WithLabelValues(name, name).Set(0) |
| 141 | + m.queueRetries.WithLabelValues(name, name).Add(0) |
| 142 | +} |
0 commit comments