metrics.go 8.4 KB

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