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