metrics.go 6.1 KB

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