diff --git a/src/cache.js b/src/cache.js index eee57121645555056a5ec62b8da455bc3fdccddc..aa6bf69e6d26b823180701b7b1c70bad0fad7370 100644 --- a/src/cache.js +++ b/src/cache.js @@ -47,6 +47,35 @@ export const versionCountGauge = new Gauge({ } }) +export const usedMemoryGauge = new Gauge({ + name: 'redis_used_ram', + help: 'Total used memory in human readable units and as a percentage (RSS / total memory)', + labelNames: ['memory_type'], + async collect () { + redis.client.info('memory', (err, result) => { + if (err) { + throw err + } + + // Parse the result + const infoLines = result.split('\r\n') + const memoryInfo = {} + + for (const line of infoLines) { + const [key, value] = line.split(':') + if (key && value) { + memoryInfo[key] = value + } + } + const usedMemoryRSS = parseFloat(memoryInfo.used_memory_rss) + const memoryTotal = parseFloat(memoryInfo.total_system_memory) + + this.set({ memory_type: 'usedMemoryRSSHuman' }, usedMemoryRSS) + this.set({ memory_type: 'totalUsedMemoryPercentage' }, usedMemoryRSS / memoryTotal) + }) + } +}) + export function set (key, value, timeout) { logger.debug(`[Cache] Set ${key}`) if (cache[key] === value) return @@ -62,7 +91,6 @@ export async function clear () { } fileCacheSizeGauge.reset() } - export async function get (key, fallback) { if (cache[key]) { logger.debug(`[Cache] Resolve from memory: ${key}`)