khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 1 | // Copyright 2015 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package mvcc |
| 16 | |
| 17 | import ( |
| 18 | "sync" |
| 19 | |
| 20 | "github.com/prometheus/client_golang/prometheus" |
| 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | rangeCounter = prometheus.NewCounter( |
| 25 | prometheus.CounterOpts{ |
| 26 | Namespace: "etcd_debugging", |
| 27 | Subsystem: "mvcc", |
| 28 | Name: "range_total", |
| 29 | Help: "Total number of ranges seen by this member.", |
| 30 | }) |
| 31 | |
| 32 | putCounter = prometheus.NewCounter( |
| 33 | prometheus.CounterOpts{ |
| 34 | Namespace: "etcd_debugging", |
| 35 | Subsystem: "mvcc", |
| 36 | Name: "put_total", |
| 37 | Help: "Total number of puts seen by this member.", |
| 38 | }) |
| 39 | |
| 40 | deleteCounter = prometheus.NewCounter( |
| 41 | prometheus.CounterOpts{ |
| 42 | Namespace: "etcd_debugging", |
| 43 | Subsystem: "mvcc", |
| 44 | Name: "delete_total", |
| 45 | Help: "Total number of deletes seen by this member.", |
| 46 | }) |
| 47 | |
| 48 | txnCounter = prometheus.NewCounter( |
| 49 | prometheus.CounterOpts{ |
| 50 | Namespace: "etcd_debugging", |
| 51 | Subsystem: "mvcc", |
| 52 | Name: "txn_total", |
| 53 | Help: "Total number of txns seen by this member.", |
| 54 | }) |
| 55 | |
| 56 | keysGauge = prometheus.NewGauge( |
| 57 | prometheus.GaugeOpts{ |
| 58 | Namespace: "etcd_debugging", |
| 59 | Subsystem: "mvcc", |
| 60 | Name: "keys_total", |
| 61 | Help: "Total number of keys.", |
| 62 | }) |
| 63 | |
| 64 | watchStreamGauge = prometheus.NewGauge( |
| 65 | prometheus.GaugeOpts{ |
| 66 | Namespace: "etcd_debugging", |
| 67 | Subsystem: "mvcc", |
| 68 | Name: "watch_stream_total", |
| 69 | Help: "Total number of watch streams.", |
| 70 | }) |
| 71 | |
| 72 | watcherGauge = prometheus.NewGauge( |
| 73 | prometheus.GaugeOpts{ |
| 74 | Namespace: "etcd_debugging", |
| 75 | Subsystem: "mvcc", |
| 76 | Name: "watcher_total", |
| 77 | Help: "Total number of watchers.", |
| 78 | }) |
| 79 | |
| 80 | slowWatcherGauge = prometheus.NewGauge( |
| 81 | prometheus.GaugeOpts{ |
| 82 | Namespace: "etcd_debugging", |
| 83 | Subsystem: "mvcc", |
| 84 | Name: "slow_watcher_total", |
| 85 | Help: "Total number of unsynced slow watchers.", |
| 86 | }) |
| 87 | |
| 88 | totalEventsCounter = prometheus.NewCounter( |
| 89 | prometheus.CounterOpts{ |
| 90 | Namespace: "etcd_debugging", |
| 91 | Subsystem: "mvcc", |
| 92 | Name: "events_total", |
| 93 | Help: "Total number of events sent by this member.", |
| 94 | }) |
| 95 | |
| 96 | pendingEventsGauge = prometheus.NewGauge( |
| 97 | prometheus.GaugeOpts{ |
| 98 | Namespace: "etcd_debugging", |
| 99 | Subsystem: "mvcc", |
| 100 | Name: "pending_events_total", |
| 101 | Help: "Total number of pending events to be sent.", |
| 102 | }) |
| 103 | |
| 104 | indexCompactionPauseDurations = prometheus.NewHistogram( |
| 105 | prometheus.HistogramOpts{ |
| 106 | Namespace: "etcd_debugging", |
| 107 | Subsystem: "mvcc", |
| 108 | Name: "index_compaction_pause_duration_milliseconds", |
| 109 | Help: "Bucketed histogram of index compaction pause duration.", |
| 110 | // 0.5ms -> 1second |
| 111 | Buckets: prometheus.ExponentialBuckets(0.5, 2, 12), |
| 112 | }) |
| 113 | |
| 114 | dbCompactionPauseDurations = prometheus.NewHistogram( |
| 115 | prometheus.HistogramOpts{ |
| 116 | Namespace: "etcd_debugging", |
| 117 | Subsystem: "mvcc", |
| 118 | Name: "db_compaction_pause_duration_milliseconds", |
| 119 | Help: "Bucketed histogram of db compaction pause duration.", |
| 120 | // 1ms -> 4second |
| 121 | Buckets: prometheus.ExponentialBuckets(1, 2, 13), |
| 122 | }) |
| 123 | |
| 124 | dbCompactionTotalDurations = prometheus.NewHistogram( |
| 125 | prometheus.HistogramOpts{ |
| 126 | Namespace: "etcd_debugging", |
| 127 | Subsystem: "mvcc", |
| 128 | Name: "db_compaction_total_duration_milliseconds", |
| 129 | Help: "Bucketed histogram of db compaction total duration.", |
| 130 | // 100ms -> 800second |
| 131 | Buckets: prometheus.ExponentialBuckets(100, 2, 14), |
| 132 | }) |
| 133 | |
| 134 | dbCompactionKeysCounter = prometheus.NewCounter( |
| 135 | prometheus.CounterOpts{ |
| 136 | Namespace: "etcd_debugging", |
| 137 | Subsystem: "mvcc", |
| 138 | Name: "db_compaction_keys_total", |
| 139 | Help: "Total number of db keys compacted.", |
| 140 | }) |
| 141 | |
| 142 | dbTotalSizeDebugging = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ |
| 143 | Namespace: "etcd_debugging", |
| 144 | Subsystem: "mvcc", |
| 145 | Name: "db_total_size_in_bytes", |
| 146 | Help: "Total size of the underlying database physically allocated in bytes. Use etcd_mvcc_db_total_size_in_bytes", |
| 147 | }, |
| 148 | func() float64 { |
| 149 | reportDbTotalSizeInBytesMu.RLock() |
| 150 | defer reportDbTotalSizeInBytesMu.RUnlock() |
| 151 | return reportDbTotalSizeInBytes() |
| 152 | }, |
| 153 | ) |
| 154 | dbTotalSize = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ |
| 155 | Namespace: "etcd", |
| 156 | Subsystem: "mvcc", |
| 157 | Name: "db_total_size_in_bytes", |
| 158 | Help: "Total size of the underlying database physically allocated in bytes.", |
| 159 | }, |
| 160 | func() float64 { |
| 161 | reportDbTotalSizeInBytesMu.RLock() |
| 162 | defer reportDbTotalSizeInBytesMu.RUnlock() |
| 163 | return reportDbTotalSizeInBytes() |
| 164 | }, |
| 165 | ) |
| 166 | // overridden by mvcc initialization |
| 167 | reportDbTotalSizeInBytesMu sync.RWMutex |
| 168 | reportDbTotalSizeInBytes = func() float64 { return 0 } |
| 169 | |
| 170 | dbTotalSizeInUse = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ |
| 171 | Namespace: "etcd", |
| 172 | Subsystem: "mvcc", |
| 173 | Name: "db_total_size_in_use_in_bytes", |
| 174 | Help: "Total size of the underlying database logically in use in bytes.", |
| 175 | }, |
| 176 | func() float64 { |
| 177 | reportDbTotalSizeInUseInBytesMu.RLock() |
| 178 | defer reportDbTotalSizeInUseInBytesMu.RUnlock() |
| 179 | return reportDbTotalSizeInUseInBytes() |
| 180 | }, |
| 181 | ) |
| 182 | // overridden by mvcc initialization |
| 183 | reportDbTotalSizeInUseInBytesMu sync.RWMutex |
| 184 | reportDbTotalSizeInUseInBytes func() float64 = func() float64 { return 0 } |
| 185 | |
| 186 | hashDurations = prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 187 | Namespace: "etcd", |
| 188 | Subsystem: "mvcc", |
| 189 | Name: "hash_duration_seconds", |
| 190 | Help: "The latency distribution of storage hash operation.", |
| 191 | |
| 192 | // 100 MB usually takes 100 ms, so start with 10 MB of 10 ms |
| 193 | // lowest bucket start of upper bound 0.01 sec (10 ms) with factor 2 |
| 194 | // highest bucket start of 0.01 sec * 2^14 == 163.84 sec |
| 195 | Buckets: prometheus.ExponentialBuckets(.01, 2, 15), |
| 196 | }) |
| 197 | |
| 198 | hashRevDurations = prometheus.NewHistogram(prometheus.HistogramOpts{ |
| 199 | Namespace: "etcd", |
| 200 | Subsystem: "mvcc", |
| 201 | Name: "hash_rev_duration_seconds", |
| 202 | Help: "The latency distribution of storage hash by revision operation.", |
| 203 | |
| 204 | // 100 MB usually takes 100 ms, so start with 10 MB of 10 ms |
| 205 | // lowest bucket start of upper bound 0.01 sec (10 ms) with factor 2 |
| 206 | // highest bucket start of 0.01 sec * 2^14 == 163.84 sec |
| 207 | Buckets: prometheus.ExponentialBuckets(.01, 2, 15), |
| 208 | }) |
| 209 | |
| 210 | currentRev = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ |
| 211 | Namespace: "etcd_debugging", |
| 212 | Subsystem: "mvcc", |
| 213 | Name: "current_revision", |
| 214 | Help: "The current revision of store.", |
| 215 | }, |
| 216 | func() float64 { |
| 217 | reportCurrentRevMu.RLock() |
| 218 | defer reportCurrentRevMu.RUnlock() |
| 219 | return reportCurrentRev() |
| 220 | }, |
| 221 | ) |
| 222 | // overridden by mvcc initialization |
| 223 | reportCurrentRevMu sync.RWMutex |
| 224 | reportCurrentRev = func() float64 { return 0 } |
| 225 | |
| 226 | compactRev = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ |
| 227 | Namespace: "etcd_debugging", |
| 228 | Subsystem: "mvcc", |
| 229 | Name: "compact_revision", |
| 230 | Help: "The revision of the last compaction in store.", |
| 231 | }, |
| 232 | func() float64 { |
| 233 | reportCompactRevMu.RLock() |
| 234 | defer reportCompactRevMu.RUnlock() |
| 235 | return reportCompactRev() |
| 236 | }, |
| 237 | ) |
| 238 | // overridden by mvcc initialization |
| 239 | reportCompactRevMu sync.RWMutex |
| 240 | reportCompactRev = func() float64 { return 0 } |
| 241 | |
| 242 | totalPutSizeGauge = prometheus.NewGauge( |
| 243 | prometheus.GaugeOpts{ |
| 244 | Namespace: "etcd_debugging", |
| 245 | Subsystem: "mvcc", |
| 246 | Name: "total_put_size_in_bytes", |
| 247 | Help: "The total size of put kv pairs seen by this member.", |
| 248 | }) |
| 249 | ) |
| 250 | |
| 251 | func init() { |
| 252 | prometheus.MustRegister(rangeCounter) |
| 253 | prometheus.MustRegister(putCounter) |
| 254 | prometheus.MustRegister(deleteCounter) |
| 255 | prometheus.MustRegister(txnCounter) |
| 256 | prometheus.MustRegister(keysGauge) |
| 257 | prometheus.MustRegister(watchStreamGauge) |
| 258 | prometheus.MustRegister(watcherGauge) |
| 259 | prometheus.MustRegister(slowWatcherGauge) |
| 260 | prometheus.MustRegister(totalEventsCounter) |
| 261 | prometheus.MustRegister(pendingEventsGauge) |
| 262 | prometheus.MustRegister(indexCompactionPauseDurations) |
| 263 | prometheus.MustRegister(dbCompactionPauseDurations) |
| 264 | prometheus.MustRegister(dbCompactionTotalDurations) |
| 265 | prometheus.MustRegister(dbCompactionKeysCounter) |
| 266 | prometheus.MustRegister(dbTotalSizeDebugging) |
| 267 | prometheus.MustRegister(dbTotalSize) |
| 268 | prometheus.MustRegister(dbTotalSizeInUse) |
| 269 | prometheus.MustRegister(hashDurations) |
| 270 | prometheus.MustRegister(hashRevDurations) |
| 271 | prometheus.MustRegister(currentRev) |
| 272 | prometheus.MustRegister(compactRev) |
| 273 | prometheus.MustRegister(totalPutSizeGauge) |
| 274 | } |
| 275 | |
| 276 | // ReportEventReceived reports that an event is received. |
| 277 | // This function should be called when the external systems received an |
| 278 | // event from mvcc.Watcher. |
| 279 | func ReportEventReceived(n int) { |
| 280 | pendingEventsGauge.Sub(float64(n)) |
| 281 | totalEventsCounter.Add(float64(n)) |
| 282 | } |