|
| 1 | +// Package metrics provides Prometheus metrics for the model validation operator. |
| 2 | +package metrics |
| 3 | + |
| 4 | +import ( |
| 5 | + "github.com/prometheus/client_golang/prometheus" |
| 6 | + "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + // Metric label names |
| 11 | + labelNamespace = "namespace" |
| 12 | + labelModelValidation = "model_validation" |
| 13 | + labelPodState = "pod_state" |
| 14 | + labelStatusUpdateResult = "result" |
| 15 | + labelDriftType = "drift_type" |
| 16 | + |
| 17 | + // PodStateInjected represents pods with model validation finalizers |
| 18 | + PodStateInjected = "injected" |
| 19 | + // PodStateUninjected represents pods without model validation finalizers |
| 20 | + PodStateUninjected = "uninjected" |
| 21 | + // PodStateOrphaned represents pods with configuration drift |
| 22 | + PodStateOrphaned = "orphaned" |
| 23 | + |
| 24 | + // StatusUpdateSuccess indicates a successful status update |
| 25 | + StatusUpdateSuccess = "success" |
| 26 | + // StatusUpdateFailure indicates a failed status update |
| 27 | + StatusUpdateFailure = "failure" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + // ModelValidationPodCounts tracks the current number of pods in each state per ModelValidation |
| 32 | + ModelValidationPodCounts = prometheus.NewGaugeVec( |
| 33 | + prometheus.GaugeOpts{ |
| 34 | + Namespace: "model_validation_operator", |
| 35 | + Name: "modelvalidation_pod_count", |
| 36 | + Help: "Current number of pods tracked per ModelValidation by state", |
| 37 | + }, |
| 38 | + []string{labelNamespace, labelModelValidation, labelPodState}, |
| 39 | + ) |
| 40 | + |
| 41 | + // PodStateTransitionsTotal tracks pod state transitions |
| 42 | + PodStateTransitionsTotal = prometheus.NewCounterVec( |
| 43 | + prometheus.CounterOpts{ |
| 44 | + Namespace: "model_validation_operator", |
| 45 | + Name: "pod_state_transitions_total", |
| 46 | + Help: "Total number of pod state transitions", |
| 47 | + }, |
| 48 | + []string{labelNamespace, labelModelValidation, "from_state", "to_state"}, |
| 49 | + ) |
| 50 | + |
| 51 | + // StatusUpdatesTotal tracks ModelValidation status updates |
| 52 | + StatusUpdatesTotal = prometheus.NewCounterVec( |
| 53 | + prometheus.CounterOpts{ |
| 54 | + Namespace: "model_validation_operator", |
| 55 | + Name: "status_updates_total", |
| 56 | + Help: "Total number of ModelValidation status updates", |
| 57 | + }, |
| 58 | + []string{labelNamespace, labelModelValidation, labelStatusUpdateResult}, |
| 59 | + ) |
| 60 | + |
| 61 | + // ConfigurationDriftEventsTotal tracks configuration drift events |
| 62 | + ConfigurationDriftEventsTotal = prometheus.NewCounterVec( |
| 63 | + prometheus.CounterOpts{ |
| 64 | + Namespace: "model_validation_operator", |
| 65 | + Name: "configuration_drift_events_total", |
| 66 | + Help: "Total number of configuration drift events detected", |
| 67 | + }, |
| 68 | + []string{labelNamespace, labelModelValidation, labelDriftType}, |
| 69 | + ) |
| 70 | + |
| 71 | + // ModelValidationCRsTotal tracks total number of ModelValidation CRs per namespace |
| 72 | + // Does not include authMethod for namespace-level tracking |
| 73 | + ModelValidationCRsTotal = prometheus.NewGaugeVec( |
| 74 | + prometheus.GaugeOpts{ |
| 75 | + Namespace: "model_validation_operator", |
| 76 | + Name: "modelvalidation_crs_total", |
| 77 | + Help: "Total number of ModelValidation CRs being tracked per namespace", |
| 78 | + }, |
| 79 | + []string{labelNamespace}, |
| 80 | + ) |
| 81 | + |
| 82 | + // StatusUpdateDuration tracks the duration of status update operations |
| 83 | + StatusUpdateDuration = prometheus.NewHistogramVec( |
| 84 | + prometheus.HistogramOpts{ |
| 85 | + Namespace: "model_validation_operator", |
| 86 | + Name: "status_update_duration_seconds", |
| 87 | + Help: "Duration of ModelValidation status update operations", |
| 88 | + Buckets: prometheus.DefBuckets, |
| 89 | + }, |
| 90 | + []string{labelNamespace, labelModelValidation, labelStatusUpdateResult}, |
| 91 | + ) |
| 92 | + |
| 93 | + // QueueSize tracks the current size of the status update queue |
| 94 | + QueueSize = prometheus.NewGauge( |
| 95 | + prometheus.GaugeOpts{ |
| 96 | + Namespace: "model_validation_operator", |
| 97 | + Name: "status_update_queue_size", |
| 98 | + Help: "Current size of the status update queue", |
| 99 | + }, |
| 100 | + ) |
| 101 | + |
| 102 | + // RetryAttemptsTotal tracks retry attempts for status updates |
| 103 | + RetryAttemptsTotal = prometheus.NewCounterVec( |
| 104 | + prometheus.CounterOpts{ |
| 105 | + Namespace: "model_validation_operator", |
| 106 | + Name: "status_update_retry_attempts_total", |
| 107 | + Help: "Total number of status update retry attempts", |
| 108 | + }, |
| 109 | + []string{labelNamespace, labelModelValidation}, |
| 110 | + ) |
| 111 | +) |
| 112 | + |
| 113 | +func init() { |
| 114 | + metrics.Registry.MustRegister( |
| 115 | + ModelValidationPodCounts, |
| 116 | + PodStateTransitionsTotal, |
| 117 | + StatusUpdatesTotal, |
| 118 | + ConfigurationDriftEventsTotal, |
| 119 | + ModelValidationCRsTotal, |
| 120 | + StatusUpdateDuration, |
| 121 | + QueueSize, |
| 122 | + RetryAttemptsTotal, |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +// RecordPodCount records the current pod count for a ModelValidation |
| 127 | +func RecordPodCount(namespace, modelValidation, podState string, count float64) { |
| 128 | + ModelValidationPodCounts.WithLabelValues(namespace, modelValidation, podState).Set(count) |
| 129 | +} |
| 130 | + |
| 131 | +// RecordPodStateTransition records a pod state transition |
| 132 | +func RecordPodStateTransition(namespace, modelValidation, fromState, toState string) { |
| 133 | + PodStateTransitionsTotal.WithLabelValues(namespace, modelValidation, fromState, toState).Inc() |
| 134 | +} |
| 135 | + |
| 136 | +// RecordStatusUpdate records a status update result |
| 137 | +func RecordStatusUpdate(namespace, modelValidation, result string) { |
| 138 | + StatusUpdatesTotal.WithLabelValues(namespace, modelValidation, result).Inc() |
| 139 | +} |
| 140 | + |
| 141 | +// RecordConfigurationDrift records a configuration drift event |
| 142 | +func RecordConfigurationDrift(namespace, modelValidation, driftType string) { |
| 143 | + ConfigurationDriftEventsTotal.WithLabelValues(namespace, modelValidation, driftType).Inc() |
| 144 | +} |
| 145 | + |
| 146 | +// RecordModelValidationCR records the current number of ModelValidation CRs per namespace |
| 147 | +func RecordModelValidationCR(namespace string, count float64) { |
| 148 | + ModelValidationCRsTotal.WithLabelValues(namespace).Set(count) |
| 149 | +} |
| 150 | + |
| 151 | +// RecordStatusUpdateDuration records the duration of a status update |
| 152 | +func RecordStatusUpdateDuration(namespace, modelValidation, result string, duration float64) { |
| 153 | + StatusUpdateDuration.WithLabelValues(namespace, modelValidation, result).Observe(duration) |
| 154 | +} |
| 155 | + |
| 156 | +// SetQueueSize sets the current queue size |
| 157 | +func SetQueueSize(size float64) { |
| 158 | + QueueSize.Set(size) |
| 159 | +} |
| 160 | + |
| 161 | +// RecordRetryAttempt records a retry attempt |
| 162 | +func RecordRetryAttempt(namespace, modelValidation string) { |
| 163 | + RetryAttemptsTotal.WithLabelValues(namespace, modelValidation).Inc() |
| 164 | +} |
| 165 | + |
| 166 | +// RecordMultiplePodStateTransitions records multiple identical pod state transitions |
| 167 | +func RecordMultiplePodStateTransitions(namespace, modelValidation, fromState, toState string, count int) { |
| 168 | + if count > 0 { |
| 169 | + PodStateTransitionsTotal.WithLabelValues(namespace, modelValidation, fromState, toState).Add(float64(count)) |
| 170 | + } |
| 171 | +} |
0 commit comments