metrics.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package y
  17. import (
  18. "expvar"
  19. )
  20. const (
  21. BADGER_METRIC_PREFIX = "badger_"
  22. )
  23. var (
  24. // lsmSize has size of the LSM in bytes
  25. lsmSize *expvar.Map
  26. // vlogSize has size of the value log in bytes
  27. vlogSize *expvar.Map
  28. // pendingWrites tracks the number of pending writes.
  29. pendingWrites *expvar.Map
  30. // These are cumulative
  31. // VLOG METRICS
  32. // numReads has cumulative number of reads from vlog
  33. numReadsVlog *expvar.Int
  34. // numWrites has cumulative number of writes into vlog
  35. numWritesVlog *expvar.Int
  36. // numBytesRead has cumulative number of bytes read from VLOG
  37. numBytesReadVlog *expvar.Int
  38. // numBytesVlogWritten has cumulative number of bytes written into VLOG
  39. numBytesVlogWritten *expvar.Int
  40. // LSM METRICS
  41. // numBytesRead has cumulative number of bytes read from LSM tree
  42. numBytesReadLSM *expvar.Int
  43. // numBytesWrittenToL0 has cumulative number of bytes written into LSM Tree
  44. numBytesWrittenToL0 *expvar.Int
  45. // numLSMGets is number of LSM gets
  46. numLSMGets *expvar.Map
  47. // numBytesCompactionWritten is the number of bytes written in the lsm tree due to compaction
  48. numBytesCompactionWritten *expvar.Map
  49. // numLSMBloomHits is number of LMS bloom hits
  50. numLSMBloomHits *expvar.Map
  51. // DB METRICS
  52. // numGets is number of gets -> Number of get requests made
  53. numGets *expvar.Int
  54. // number of get queries in which we actually get a result
  55. numGetsWithResults *expvar.Int
  56. // number of iterators created, these would be the number of range queries
  57. numIteratorsCreated *expvar.Int
  58. // numPuts is number of puts -> Number of puts requests made
  59. numPuts *expvar.Int
  60. // numMemtableGets is number of memtable gets -> Number of get requests made on memtable
  61. numMemtableGets *expvar.Int
  62. // numCompactionTables is the number of tables being compacted
  63. numCompactionTables *expvar.Int
  64. // Total writes by a user in bytes
  65. numBytesWrittenUser *expvar.Int
  66. )
  67. // These variables are global and have cumulative values for all kv stores.
  68. // Naming convention of metrics: {badger_version}_{singular operation}_{granularity}_{component}
  69. func init() {
  70. numReadsVlog = expvar.NewInt(BADGER_METRIC_PREFIX + "read_num_vlog")
  71. numBytesReadVlog = expvar.NewInt(BADGER_METRIC_PREFIX + "read_bytes_vlog")
  72. numWritesVlog = expvar.NewInt(BADGER_METRIC_PREFIX + "write_num_vlog")
  73. numBytesVlogWritten = expvar.NewInt(BADGER_METRIC_PREFIX + "write_bytes_vlog")
  74. numBytesReadLSM = expvar.NewInt(BADGER_METRIC_PREFIX + "read_bytes_lsm")
  75. numBytesWrittenToL0 = expvar.NewInt(BADGER_METRIC_PREFIX + "write_bytes_l0")
  76. numBytesCompactionWritten = expvar.NewMap(BADGER_METRIC_PREFIX + "write_bytes_compaction")
  77. numLSMGets = expvar.NewMap(BADGER_METRIC_PREFIX + "get_num_lsm")
  78. numLSMBloomHits = expvar.NewMap(BADGER_METRIC_PREFIX + "hit_num_lsm_bloom_filter")
  79. numMemtableGets = expvar.NewInt(BADGER_METRIC_PREFIX + "get_num_memtable")
  80. // User operations
  81. numGets = expvar.NewInt(BADGER_METRIC_PREFIX + "get_num_user")
  82. numPuts = expvar.NewInt(BADGER_METRIC_PREFIX + "put_num_user")
  83. numBytesWrittenUser = expvar.NewInt(BADGER_METRIC_PREFIX + "write_bytes_user")
  84. // Required for Enabled
  85. numGetsWithResults = expvar.NewInt(BADGER_METRIC_PREFIX + "get_with_result_num_user")
  86. numIteratorsCreated = expvar.NewInt(BADGER_METRIC_PREFIX + "iterator_num_user")
  87. // Sizes
  88. lsmSize = expvar.NewMap(BADGER_METRIC_PREFIX + "size_bytes_lsm")
  89. vlogSize = expvar.NewMap(BADGER_METRIC_PREFIX + "size_bytes_vlog")
  90. pendingWrites = expvar.NewMap(BADGER_METRIC_PREFIX + "write_pending_num_memtable")
  91. numCompactionTables = expvar.NewInt(BADGER_METRIC_PREFIX + "compaction_current_num_lsm")
  92. }
  93. func NumIteratorsCreatedAdd(enabled bool, val int64) {
  94. addInt(enabled, numIteratorsCreated, val)
  95. }
  96. func NumGetsWithResultsAdd(enabled bool, val int64) {
  97. addInt(enabled, numGetsWithResults, val)
  98. }
  99. func NumReadsVlogAdd(enabled bool, val int64) {
  100. addInt(enabled, numReadsVlog, val)
  101. }
  102. func NumBytesWrittenUserAdd(enabled bool, val int64) {
  103. addInt(enabled, numBytesWrittenUser, val)
  104. }
  105. func NumWritesVlogAdd(enabled bool, val int64) {
  106. addInt(enabled, numWritesVlog, val)
  107. }
  108. func NumBytesReadsVlogAdd(enabled bool, val int64) {
  109. addInt(enabled, numBytesReadVlog, val)
  110. }
  111. func NumBytesReadsLSMAdd(enabled bool, val int64) {
  112. addInt(enabled, numBytesReadLSM, val)
  113. }
  114. func NumBytesWrittenVlogAdd(enabled bool, val int64) {
  115. addInt(enabled, numBytesVlogWritten, val)
  116. }
  117. func NumBytesWrittenToL0Add(enabled bool, val int64) {
  118. addInt(enabled, numBytesWrittenToL0, val)
  119. }
  120. func NumBytesCompactionWrittenAdd(enabled bool, key string, val int64) {
  121. addToMap(enabled, numBytesCompactionWritten, key, val)
  122. }
  123. func NumGetsAdd(enabled bool, val int64) {
  124. addInt(enabled, numGets, val)
  125. }
  126. func NumPutsAdd(enabled bool, val int64) {
  127. addInt(enabled, numPuts, val)
  128. }
  129. func NumMemtableGetsAdd(enabled bool, val int64) {
  130. addInt(enabled, numMemtableGets, val)
  131. }
  132. func NumCompactionTablesAdd(enabled bool, val int64) {
  133. addInt(enabled, numCompactionTables, val)
  134. }
  135. func LSMSizeSet(enabled bool, key string, val expvar.Var) {
  136. storeToMap(enabled, lsmSize, key, val)
  137. }
  138. func VlogSizeSet(enabled bool, key string, val expvar.Var) {
  139. storeToMap(enabled, vlogSize, key, val)
  140. }
  141. func PendingWritesSet(enabled bool, key string, val expvar.Var) {
  142. storeToMap(enabled, pendingWrites, key, val)
  143. }
  144. func NumLSMBloomHitsAdd(enabled bool, key string, val int64) {
  145. addToMap(enabled, numLSMBloomHits, key, val)
  146. }
  147. func NumLSMGetsAdd(enabled bool, key string, val int64) {
  148. addToMap(enabled, numLSMGets, key, val)
  149. }
  150. func LSMSizeGet(enabled bool, key string) expvar.Var {
  151. return getFromMap(enabled, lsmSize, key)
  152. }
  153. func VlogSizeGet(enabled bool, key string) expvar.Var {
  154. return getFromMap(enabled, vlogSize, key)
  155. }
  156. func addInt(enabled bool, metric *expvar.Int, val int64) {
  157. if !enabled {
  158. return
  159. }
  160. metric.Add(val)
  161. }
  162. func addToMap(enabled bool, metric *expvar.Map, key string, val int64) {
  163. if !enabled {
  164. return
  165. }
  166. metric.Add(key, val)
  167. }
  168. func storeToMap(enabled bool, metric *expvar.Map, key string, val expvar.Var) {
  169. if !enabled {
  170. return
  171. }
  172. metric.Set(key, val)
  173. }
  174. func getFromMap(enabled bool, metric *expvar.Map, key string) expvar.Var {
  175. if !enabled {
  176. return nil
  177. }
  178. return metric.Get(key)
  179. }