metrics.go 8.8 KB

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