Skip to main content

metrics.go

Overview

The metrics.go file defines and initializes various metrics related to BadgerDB's performance and resource usage. It uses the expvar package to expose these metrics for monitoring. The file also provides functions to increment or set these metrics.

Detailed Documentation

Constants

BADGER_METRIC_PREFIX

const (
BADGER_METRIC_PREFIX = "badger_"
)
  • Purpose: Defines a constant string that serves as a prefix for all BadgerDB metrics. This helps in easily identifying BadgerDB related metrics.

Variables

lsmSize

var (
// lsmSize has size of the LSM in bytes
lsmSize *expvar.Map
  • Purpose: An expvar.Map that stores the sizes of different levels or components of the LSM (Log-Structured Merge) tree in bytes.

vlogSize

	// vlogSize has size of the value log in bytes
vlogSize *expvar.Map
  • Purpose: An expvar.Map that stores the size of the value log in bytes.

pendingWrites

	// pendingWrites tracks the number of pending writes.
pendingWrites *expvar.Map
  • Purpose: An expvar.Map that tracks the number of pending writes to the memtable.

numReadsVlog

	// numReads has cumulative number of reads from vlog
numReadsVlog *expvar.Int
  • Purpose: An expvar.Int that stores the cumulative number of reads from the value log.

numWritesVlog

	// numWrites has cumulative number of writes into vlog
numWritesVlog *expvar.Int
  • Purpose: An expvar.Int that stores the cumulative number of writes into the value log.

numBytesReadVlog

	// numBytesRead has cumulative number of bytes read from VLOG
numBytesReadVlog *expvar.Int
  • Purpose: An expvar.Int that stores the cumulative number of bytes read from the value log.

numBytesVlogWritten

	// numBytesVlogWritten has cumulative number of bytes written into VLOG
numBytesVlogWritten *expvar.Int
  • Purpose: An expvar.Int that stores the cumulative number of bytes written into the value log.

numBytesReadLSM

	// numBytesRead has cumulative number of bytes read from LSM tree
numBytesReadLSM *expvar.Int
  • Purpose: An expvar.Int that stores the cumulative number of bytes read from the LSM tree.

numBytesWrittenToL0

	// numBytesWrittenToL0 has cumulative number of bytes written into LSM Tree
numBytesWrittenToL0 *expvar.Int
  • Purpose: An expvar.Int that stores the cumulative number of bytes written into Level 0 of the LSM tree.

numLSMGets

	// numLSMGets is number of LSM gets
numLSMGets *expvar.Map
  • Purpose: An expvar.Map that stores the number of gets served by different levels or components in the LSM tree.

numBytesCompactionWritten

	// numBytesCompactionWritten is the number of bytes written in the lsm tree due to compaction
numBytesCompactionWritten *expvar.Map
  • Purpose: An expvar.Map that stores the number of bytes written during compaction operations in the LSM tree.

numLSMBloomHits

	// numLSMBloomHits is number of LMS bloom hits
numLSMBloomHits *expvar.Map
  • Purpose: An expvar.Map that stores the number of bloom filter hits in the LSM tree.

numGets

	// numGets is number of gets -> Number of get requests made
numGets *expvar.Int
  • Purpose: An expvar.Int that stores the total number of get requests made to the database.

numGetsWithResults

	// number of get queries in which we actually get a result
numGetsWithResults *expvar.Int
  • Purpose: An expvar.Int that stores the number of get requests that returned a result.

numIteratorsCreated

	// number of iterators created, these would be the number of range queries
numIteratorsCreated *expvar.Int
  • Purpose: An expvar.Int that stores the number of iterators created, representing range queries.

numPuts

	// numPuts is number of puts -> Number of puts requests made
numPuts *expvar.Int
  • Purpose: An expvar.Int that stores the total number of put requests made to the database.

numMemtableGets

	// numMemtableGets is number of memtable gets -> Number of get requests made on memtable
numMemtableGets *expvar.Int
  • Purpose: An expvar.Int that stores the number of get requests served by the memtable.

numCompactionTables

	// numCompactionTables is the number of tables being compacted
numCompactionTables *expvar.Int
  • Purpose: An expvar.Int that stores the number of tables currently being compacted in the LSM tree.

numBytesWrittenUser

	// Total writes by a user in bytes
numBytesWrittenUser *expvar.Int
  • Purpose: An expvar.Int that stores the total number of bytes written by user put requests.

Functions

init

func init() {
numReadsVlog = expvar.NewInt(BADGER_METRIC_PREFIX + "read_num_vlog")
numBytesReadVlog = expvar.NewInt(BADGER_METRIC_PREFIX + "read_bytes_vlog")
numWritesVlog = expvar.NewInt(BADGER_METRIC_PREFIX + "write_num_vlog")
numBytesVlogWritten = expvar.NewInt(BADGER_METRIC_PREFIX + "write_bytes_vlog")

numBytesReadLSM = expvar.NewInt(BADGER_METRIC_PREFIX + "read_bytes_lsm")
numBytesWrittenToL0 = expvar.NewInt(BADGER_METRIC_PREFIX + "write_bytes_l0")
numBytesCompactionWritten = expvar.NewMap(BADGER_METRIC_PREFIX + "write_bytes_compaction")

numLSMGets = expvar.NewMap(BADGER_METRIC_PREFIX + "get_num_lsm")
numLSMBloomHits = expvar.NewMap(BADGER_METRIC_PREFIX + "hit_num_lsm_bloom_filter")
numMemtableGets = expvar.NewInt(BADGER_METRIC_PREFIX + "get_num_memtable")

// User operations
numGets = expvar.NewInt(BADGER_METRIC_PREFIX + "get_num_user")
numPuts = expvar.NewInt(BADGER_METRIC_PREFIX + "put_num_user")
numBytesWrittenUser = expvar.NewInt(BADGER_METRIC_PREFIX + "write_bytes_user")

// Required for Enabled
numGetsWithResults = expvar.NewInt(BADGER_METRIC_PREFIX + "get_with_result_num_user")
numIteratorsCreated = expvar.NewInt(BADGER_METRIC_PREFIX + "iterator_num_user")

// Sizes
lsmSize = expvar.NewMap(BADGER_METRIC_PREFIX + "size_bytes_lsm")
vlogSize = expvar.NewMap(BADGER_METRIC_PREFIX + "size_bytes_vlog")

pendingWrites = expvar.NewMap(BADGER_METRIC_PREFIX + "write_pending_num_memtable")
numCompactionTables = expvar.NewInt(BADGER_METRIC_PREFIX + "compaction_current_num_lsm")
}
  • Purpose: Initializes all the expvar metrics. It is automatically called when the package is imported.

NumIteratorsCreatedAdd

func NumIteratorsCreatedAdd(enabled bool, val int64) {
addInt(enabled, numIteratorsCreated, val)
}
  • Purpose: Adds a value to the numIteratorsCreated metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumGetsWithResultsAdd

func NumGetsWithResultsAdd(enabled bool, val int64) {
addInt(enabled, numGetsWithResults, val)
}
  • Purpose: Adds a value to the numGetsWithResults metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumReadsVlogAdd

func NumReadsVlogAdd(enabled bool, val int64) {
addInt(enabled, numReadsVlog, val)
}
  • Purpose: Adds a value to the numReadsVlog metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumBytesWrittenUserAdd

func NumBytesWrittenUserAdd(enabled bool, val int64) {
addInt(enabled, numBytesWrittenUser, val)
}
  • Purpose: Adds a value to the numBytesWrittenUser metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumWritesVlogAdd

func NumWritesVlogAdd(enabled bool, val int64) {
addInt(enabled, numWritesVlog, val)
}
  • Purpose: Adds a value to the numWritesVlog metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumBytesReadsVlogAdd

func NumBytesReadsVlogAdd(enabled bool, val int64) {
addInt(enabled, numBytesReadVlog, val)
}
  • Purpose: Adds a value to the numBytesReadVlog metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumBytesReadsLSMAdd

func NumBytesReadsLSMAdd(enabled bool, val int64) {
addInt(enabled, numBytesReadLSM, val)
}
  • Purpose: Adds a value to the numBytesReadLSM metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumBytesWrittenVlogAdd

func NumBytesWrittenVlogAdd(enabled bool, val int64) {
addInt(enabled, numBytesVlogWritten, val)
}
  • Purpose: Adds a value to the numBytesVlogWritten metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumBytesWrittenToL0Add

func NumBytesWrittenToL0Add(enabled bool, val int64) {
addInt(enabled, numBytesWrittenToL0, val)
}
  • Purpose: Adds a value to the numBytesWrittenToL0 metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumBytesCompactionWrittenAdd

func NumBytesCompactionWrittenAdd(enabled bool, key string, val int64) {
addToMap(enabled, numBytesCompactionWritten, key, val)
}
  • Purpose: Adds a value to the numBytesCompactionWritten map for a specific key.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to identify the specific compaction.
    • val (int64): The value to add to the metric.
  • Returns: None

NumGetsAdd

func NumGetsAdd(enabled bool, val int64) {
addInt(enabled, numGets, val)
}
  • Purpose: Adds a value to the numGets metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumPutsAdd

func NumPutsAdd(enabled bool, val int64) {
addInt(enabled, numPuts, val)
}
  • Purpose: Adds a value to the numPuts metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumMemtableGetsAdd

func NumMemtableGetsAdd(enabled bool, val int64) {
addInt(enabled, numMemtableGets, val)
}
  • Purpose: Adds a value to the numMemtableGets metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

NumCompactionTablesAdd

func NumCompactionTablesAdd(enabled bool, val int64) {
addInt(enabled, numCompactionTables, val)
}
  • Purpose: Adds a value to the numCompactionTables metric.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • val (int64): The value to add to the metric.
  • Returns: None

LSMSizeSet

func LSMSizeSet(enabled bool, key string, val expvar.Var) {
storeToMap(enabled, lsmSize, key, val)
}
  • Purpose: Sets the value of a key in the lsmSize map.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to set the value for.
    • val (expvar.Var): The expvar.Var value to set.
  • Returns: None

VlogSizeSet

func VlogSizeSet(enabled bool, key string, val expvar.Var) {
storeToMap(enabled, vlogSize, key, val)
}
  • Purpose: Sets the value of a key in the vlogSize map.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to set the value for.
    • val (expvar.Var): The expvar.Var value to set.
  • Returns: None

PendingWritesSet

func PendingWritesSet(enabled bool, key string, val expvar.Var) {
storeToMap(enabled, pendingWrites, key, val)
}
  • Purpose: Sets the value of a key in the pendingWrites map.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to set the value for.
    • val (expvar.Var): The expvar.Var value to set.
  • Returns: None

NumLSMBloomHitsAdd

func NumLSMBloomHitsAdd(enabled bool, key string, val int64) {
addToMap(enabled, numLSMBloomHits, key, val)
}
  • Purpose: Adds a value to the numLSMBloomHits map for a specific key.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to identify the specific bloom filter.
    • val (int64): The value to add to the metric.
  • Returns: None

NumLSMGetsAdd

func NumLSMGetsAdd(enabled bool, key string, val int64) {
addToMap(enabled, numLSMGets, key, val)
}
  • Purpose: Adds a value to the numLSMGets map for a specific key.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to identify the specific LSM level or component.
    • val (int64): The value to add to the metric.
  • Returns: None

LSMSizeGet

func LSMSizeGet(enabled bool, key string) expvar.Var {
return getFromMap(enabled, lsmSize, key)
}
  • Purpose: Retrieves a value from the lsmSize map for a given key.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to retrieve the value for.
  • Returns: expvar.Var: The expvar.Var value associated with the key, or nil if metrics are disabled or the key is not found.

VlogSizeGet

func VlogSizeGet(enabled bool, key string) expvar.Var {
return getFromMap(enabled, vlogSize, key)
}
  • Purpose: Retrieves a value from the vlogSize map for a given key.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • key (string): The key to retrieve the value for.
  • Returns: expvar.Var: The expvar.Var value associated with the key, or nil if metrics are disabled or the key is not found.

addInt

func addInt(enabled bool, metric *expvar.Int, val int64) {
if !enabled {
return
}

metric.Add(val)
}
  • Purpose: Adds a value to an expvar.Int metric if metrics are enabled.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • metric (*expvar.Int): A pointer to the expvar.Int metric.
    • val (int64): The value to add to the metric.
  • Returns: None

addToMap

func addToMap(enabled bool, metric *expvar.Map, key string, val int64) {
if !enabled {
return
}

metric.Add(key, val)
}
  • Purpose: Adds a value to an expvar.Map metric for a specific key, if metrics are enabled.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • metric (*expvar.Map): A pointer to the expvar.Map metric.
    • key (string): The key to identify the value to add.
    • val (int64): The value to add to the metric.
  • Returns: None

storeToMap

func storeToMap(enabled bool, metric *expvar.Map, key string, val expvar.Var) {
if !enabled {
return
}

metric.Set(key, val)
}
  • Purpose: Stores an expvar.Var value in an expvar.Map metric for a specific key, if metrics are enabled.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • metric (*expvar.Map): A pointer to the expvar.Map metric.
    • key (string): The key to store the value under.
    • val (expvar.Var): The expvar.Var value to store.
  • Returns: None

getFromMap

func getFromMap(enabled bool, metric *expvar.Map, key string) expvar.Var {
if !enabled {
return nil
}

return metric.Get(key)
}
  • Purpose: Retrieves an expvar.Var value from an expvar.Map metric for a specific key, if metrics are enabled.
  • Parameters:
    • enabled (bool): A boolean indicating whether metrics are enabled.
    • metric (*expvar.Map): A pointer to the expvar.Map metric.
    • key (string): The key to retrieve the value for.
  • Returns: expvar.Var: The expvar.Var value associated with the key, or nil if metrics are disabled.

Code Examples

Incrementing the number of gets

import { NumGetsAdd } from './y/metrics';

// Assuming metrics are enabled
const enabled = true;
const numberOfGets = 10;

NumGetsAdd(enabled, numberOfGets);

Setting the LSM size

import { LSMSizeSet } from './y/metrics';
import { expvar } from 'expvar';

// Assuming metrics are enabled
const enabled = true;
const lsmLevel = "level_1";
const lsmSizeInBytes = expvar.NewInt("1024"); // Example size

LSMSizeSet(enabled, lsmLevel, lsmSizeInBytes);

Getting Started Relevance