metrics.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. package mvcc
  15. import (
  16. "sync"
  17. "github.com/prometheus/client_golang/prometheus"
  18. )
  19. var (
  20. rangeCounter = prometheus.NewCounter(
  21. prometheus.CounterOpts{
  22. Namespace: "etcd",
  23. Subsystem: "mvcc",
  24. Name: "range_total",
  25. Help: "Total number of ranges seen by this member.",
  26. })
  27. rangeCounterDebug = prometheus.NewCounter(
  28. prometheus.CounterOpts{
  29. Namespace: "etcd_debugging",
  30. Subsystem: "mvcc",
  31. Name: "range_total",
  32. Help: "Total number of ranges seen by this member.",
  33. })
  34. putCounter = prometheus.NewCounter(
  35. prometheus.CounterOpts{
  36. Namespace: "etcd",
  37. Subsystem: "mvcc",
  38. Name: "put_total",
  39. Help: "Total number of puts seen by this member.",
  40. })
  41. // TODO: remove in 3.5 release
  42. putCounterDebug = prometheus.NewCounter(
  43. prometheus.CounterOpts{
  44. Namespace: "etcd_debugging",
  45. Subsystem: "mvcc",
  46. Name: "put_total",
  47. Help: "Total number of puts seen by this member.",
  48. })
  49. deleteCounter = prometheus.NewCounter(
  50. prometheus.CounterOpts{
  51. Namespace: "etcd",
  52. Subsystem: "mvcc",
  53. Name: "delete_total",
  54. Help: "Total number of deletes seen by this member.",
  55. })
  56. // TODO: remove in 3.5 release
  57. deleteCounterDebug = prometheus.NewCounter(
  58. prometheus.CounterOpts{
  59. Namespace: "etcd_debugging",
  60. Subsystem: "mvcc",
  61. Name: "delete_total",
  62. Help: "Total number of deletes seen by this member.",
  63. })
  64. txnCounter = prometheus.NewCounter(
  65. prometheus.CounterOpts{
  66. Namespace: "etcd",
  67. Subsystem: "mvcc",
  68. Name: "txn_total",
  69. Help: "Total number of txns seen by this member.",
  70. })
  71. txnCounterDebug = prometheus.NewCounter(
  72. prometheus.CounterOpts{
  73. Namespace: "etcd_debugging",
  74. Subsystem: "mvcc",
  75. Name: "txn_total",
  76. Help: "Total number of txns seen by this member.",
  77. })
  78. keysGauge = prometheus.NewGauge(
  79. prometheus.GaugeOpts{
  80. Namespace: "etcd_debugging",
  81. Subsystem: "mvcc",
  82. Name: "keys_total",
  83. Help: "Total number of keys.",
  84. })
  85. watchStreamGauge = prometheus.NewGauge(
  86. prometheus.GaugeOpts{
  87. Namespace: "etcd_debugging",
  88. Subsystem: "mvcc",
  89. Name: "watch_stream_total",
  90. Help: "Total number of watch streams.",
  91. })
  92. watcherGauge = prometheus.NewGauge(
  93. prometheus.GaugeOpts{
  94. Namespace: "etcd_debugging",
  95. Subsystem: "mvcc",
  96. Name: "watcher_total",
  97. Help: "Total number of watchers.",
  98. })
  99. slowWatcherGauge = prometheus.NewGauge(
  100. prometheus.GaugeOpts{
  101. Namespace: "etcd_debugging",
  102. Subsystem: "mvcc",
  103. Name: "slow_watcher_total",
  104. Help: "Total number of unsynced slow watchers.",
  105. })
  106. totalEventsCounter = prometheus.NewCounter(
  107. prometheus.CounterOpts{
  108. Namespace: "etcd_debugging",
  109. Subsystem: "mvcc",
  110. Name: "events_total",
  111. Help: "Total number of events sent by this member.",
  112. })
  113. pendingEventsGauge = prometheus.NewGauge(
  114. prometheus.GaugeOpts{
  115. Namespace: "etcd_debugging",
  116. Subsystem: "mvcc",
  117. Name: "pending_events_total",
  118. Help: "Total number of pending events to be sent.",
  119. })
  120. indexCompactionPauseMs = prometheus.NewHistogram(
  121. prometheus.HistogramOpts{
  122. Namespace: "etcd_debugging",
  123. Subsystem: "mvcc",
  124. Name: "index_compaction_pause_duration_milliseconds",
  125. Help: "Bucketed histogram of index compaction pause duration.",
  126. // lowest bucket start of upper bound 0.5 ms with factor 2
  127. // highest bucket start of 0.5 ms * 2^13 == 4.096 sec
  128. Buckets: prometheus.ExponentialBuckets(0.5, 2, 14),
  129. })
  130. dbCompactionPauseMs = prometheus.NewHistogram(
  131. prometheus.HistogramOpts{
  132. Namespace: "etcd_debugging",
  133. Subsystem: "mvcc",
  134. Name: "db_compaction_pause_duration_milliseconds",
  135. Help: "Bucketed histogram of db compaction pause duration.",
  136. // lowest bucket start of upper bound 1 ms with factor 2
  137. // highest bucket start of 1 ms * 2^12 == 4.096 sec
  138. Buckets: prometheus.ExponentialBuckets(1, 2, 13),
  139. })
  140. dbCompactionTotalMs = prometheus.NewHistogram(
  141. prometheus.HistogramOpts{
  142. Namespace: "etcd_debugging",
  143. Subsystem: "mvcc",
  144. Name: "db_compaction_total_duration_milliseconds",
  145. Help: "Bucketed histogram of db compaction total duration.",
  146. // lowest bucket start of upper bound 100 ms with factor 2
  147. // highest bucket start of 100 ms * 2^13 == 8.192 sec
  148. Buckets: prometheus.ExponentialBuckets(100, 2, 14),
  149. })
  150. dbCompactionKeysCounter = prometheus.NewCounter(
  151. prometheus.CounterOpts{
  152. Namespace: "etcd_debugging",
  153. Subsystem: "mvcc",
  154. Name: "db_compaction_keys_total",
  155. Help: "Total number of db keys compacted.",
  156. })
  157. dbTotalSize = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  158. Namespace: "etcd",
  159. Subsystem: "mvcc",
  160. Name: "db_total_size_in_bytes",
  161. Help: "Total size of the underlying database physically allocated in bytes.",
  162. },
  163. func() float64 {
  164. reportDbTotalSizeInBytesMu.RLock()
  165. defer reportDbTotalSizeInBytesMu.RUnlock()
  166. return reportDbTotalSizeInBytes()
  167. },
  168. )
  169. // overridden by mvcc initialization
  170. reportDbTotalSizeInBytesMu sync.RWMutex
  171. reportDbTotalSizeInBytes = func() float64 { return 0 }
  172. // TODO: remove this in v3.5
  173. dbTotalSizeDebug = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  174. Namespace: "etcd_debugging",
  175. Subsystem: "mvcc",
  176. Name: "db_total_size_in_bytes",
  177. Help: "Total size of the underlying database physically allocated in bytes.",
  178. },
  179. func() float64 {
  180. reportDbTotalSizeInBytesDebugMu.RLock()
  181. defer reportDbTotalSizeInBytesDebugMu.RUnlock()
  182. return reportDbTotalSizeInBytesDebug()
  183. },
  184. )
  185. // overridden by mvcc initialization
  186. reportDbTotalSizeInBytesDebugMu sync.RWMutex
  187. reportDbTotalSizeInBytesDebug = func() float64 { return 0 }
  188. dbTotalSizeInUse = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  189. Namespace: "etcd",
  190. Subsystem: "mvcc",
  191. Name: "db_total_size_in_use_in_bytes",
  192. Help: "Total size of the underlying database logically in use in bytes.",
  193. },
  194. func() float64 {
  195. reportDbTotalSizeInUseInBytesMu.RLock()
  196. defer reportDbTotalSizeInUseInBytesMu.RUnlock()
  197. return reportDbTotalSizeInUseInBytes()
  198. },
  199. )
  200. // overridden by mvcc initialization
  201. reportDbTotalSizeInUseInBytesMu sync.RWMutex
  202. reportDbTotalSizeInUseInBytes = func() float64 { return 0 }
  203. dbOpenReadTxN = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  204. Namespace: "etcd",
  205. Subsystem: "mvcc",
  206. Name: "db_open_read_transactions",
  207. Help: "The number of currently open read transactions",
  208. },
  209. func() float64 {
  210. reportDbOpenReadTxNMu.RLock()
  211. defer reportDbOpenReadTxNMu.RUnlock()
  212. return reportDbOpenReadTxN()
  213. },
  214. )
  215. // overridden by mvcc initialization
  216. reportDbOpenReadTxNMu sync.RWMutex
  217. reportDbOpenReadTxN = func() float64 { return 0 }
  218. hashSec = prometheus.NewHistogram(prometheus.HistogramOpts{
  219. Namespace: "etcd",
  220. Subsystem: "mvcc",
  221. Name: "hash_duration_seconds",
  222. Help: "The latency distribution of storage hash operation.",
  223. // 100 MB usually takes 100 ms, so start with 10 MB of 10 ms
  224. // lowest bucket start of upper bound 0.01 sec (10 ms) with factor 2
  225. // highest bucket start of 0.01 sec * 2^14 == 163.84 sec
  226. Buckets: prometheus.ExponentialBuckets(.01, 2, 15),
  227. })
  228. hashRevSec = prometheus.NewHistogram(prometheus.HistogramOpts{
  229. Namespace: "etcd",
  230. Subsystem: "mvcc",
  231. Name: "hash_rev_duration_seconds",
  232. Help: "The latency distribution of storage hash by revision operation.",
  233. // 100 MB usually takes 100 ms, so start with 10 MB of 10 ms
  234. // lowest bucket start of upper bound 0.01 sec (10 ms) with factor 2
  235. // highest bucket start of 0.01 sec * 2^14 == 163.84 sec
  236. Buckets: prometheus.ExponentialBuckets(.01, 2, 15),
  237. })
  238. currentRev = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  239. Namespace: "etcd_debugging",
  240. Subsystem: "mvcc",
  241. Name: "current_revision",
  242. Help: "The current revision of store.",
  243. },
  244. func() float64 {
  245. reportCurrentRevMu.RLock()
  246. defer reportCurrentRevMu.RUnlock()
  247. return reportCurrentRev()
  248. },
  249. )
  250. // overridden by mvcc initialization
  251. reportCurrentRevMu sync.RWMutex
  252. reportCurrentRev = func() float64 { return 0 }
  253. compactRev = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  254. Namespace: "etcd_debugging",
  255. Subsystem: "mvcc",
  256. Name: "compact_revision",
  257. Help: "The revision of the last compaction in store.",
  258. },
  259. func() float64 {
  260. reportCompactRevMu.RLock()
  261. defer reportCompactRevMu.RUnlock()
  262. return reportCompactRev()
  263. },
  264. )
  265. // overridden by mvcc initialization
  266. reportCompactRevMu sync.RWMutex
  267. reportCompactRev = func() float64 { return 0 }
  268. )
  269. func init() {
  270. prometheus.MustRegister(rangeCounter)
  271. prometheus.MustRegister(rangeCounterDebug)
  272. prometheus.MustRegister(putCounter)
  273. prometheus.MustRegister(putCounterDebug)
  274. prometheus.MustRegister(deleteCounter)
  275. prometheus.MustRegister(deleteCounterDebug)
  276. prometheus.MustRegister(txnCounter)
  277. prometheus.MustRegister(txnCounterDebug)
  278. prometheus.MustRegister(keysGauge)
  279. prometheus.MustRegister(watchStreamGauge)
  280. prometheus.MustRegister(watcherGauge)
  281. prometheus.MustRegister(slowWatcherGauge)
  282. prometheus.MustRegister(totalEventsCounter)
  283. prometheus.MustRegister(pendingEventsGauge)
  284. prometheus.MustRegister(indexCompactionPauseMs)
  285. prometheus.MustRegister(dbCompactionPauseMs)
  286. prometheus.MustRegister(dbCompactionTotalMs)
  287. prometheus.MustRegister(dbCompactionKeysCounter)
  288. prometheus.MustRegister(dbTotalSize)
  289. prometheus.MustRegister(dbTotalSizeDebug)
  290. prometheus.MustRegister(dbTotalSizeInUse)
  291. prometheus.MustRegister(dbOpenReadTxN)
  292. prometheus.MustRegister(hashSec)
  293. prometheus.MustRegister(hashRevSec)
  294. prometheus.MustRegister(currentRev)
  295. prometheus.MustRegister(compactRev)
  296. }
  297. // ReportEventReceived reports that an event is received.
  298. // This function should be called when the external systems received an
  299. // event from mvcc.Watcher.
  300. func ReportEventReceived(n int) {
  301. pendingEventsGauge.Sub(float64(n))
  302. totalEventsCounter.Add(float64(n))
  303. }