From ffaff455f6bc27323f25d6b9cc81db2ade6af45e Mon Sep 17 00:00:00 2001 From: Stefano Rivoir Date: Tue, 26 Aug 2025 08:36:50 +0200 Subject: [PATCH] Add metrics for cache hit/miss Fixes #365 --- docs/index.md | 1 + lib/cache.mjs | 6 +++++- lib/const.mjs | 2 ++ passweaver-api.mjs | 2 ++ test/metrics.spec.cjs | 2 ++ 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 50f9546..d71bddd 100644 --- a/docs/index.md +++ b/docs/index.md @@ -313,6 +313,7 @@ If enabled in configuration, PassWeaver API export various metrics (along with d - One time tokens count (`onetimetokens_created_total`, `onetimetokens_read_total`) - KMS encryptions and decryptions count (`kms_encryptions_total`, `kms_decryptions_total`) - KMS encryptions and descriptions for each KMS (`kms_encryptions_per_kms_total`, `kms_decryptions_per_kms_total`) + - Cache hits and misses (`cache_hits_total`, `cache_misses_total`) ## Cache diff --git a/lib/cache.mjs b/lib/cache.mjs index ab53902..e74042a 100644 --- a/lib/cache.mjs +++ b/lib/cache.mjs @@ -9,6 +9,8 @@ import * as Config from './config.mjs' import * as CacheNode from './cache/cache-node.mjs' import * as CacheRedis from './cache/cache-redis.mjs' +import * as Metrics from './metrics.mjs' +import * as Const from './const.mjs' let Cache @@ -86,7 +88,9 @@ export async function resetItemTypes () { * @param {string} key Key to retreive */ export async function get (user, key) { - return await Cache.get(`${prefix}:${key}.${user}.`) + const ret = await Cache.get(`${prefix}:${key}.${user}.`) + Metrics.counterInc(ret ? Const.METRICS_CACHE_HITS : Const.METRICS_CACHE_MISSES) + return ret } /** diff --git a/lib/const.mjs b/lib/const.mjs index a957024..4de9c89 100644 --- a/lib/const.mjs +++ b/lib/const.mjs @@ -123,3 +123,5 @@ export const METRICS_KMS_DECRYPTIONS = 'kms_decryptions_total' export const METRICS_KMS_ENCRYPTIONS_PER_KMS = 'kms_encryptions_per_kms_total' export const METRICS_KMS_DECRYPTIONS_PER_KMS = 'kms_decryptions_per_kms_total' export const METRICS_LOGIN_APIKEYS_PER_KEY = 'login_apikeys_per_key_total' +export const METRICS_CACHE_HITS = 'cache_hits_total' +export const METRICS_CACHE_MISSES = 'cache_misses_total' diff --git a/passweaver-api.mjs b/passweaver-api.mjs index 2243ef5..cf666ec 100644 --- a/passweaver-api.mjs +++ b/passweaver-api.mjs @@ -136,6 +136,8 @@ if (cfg?.enable_metrics) { Metrics.createCounter(Const.METRICS_KMS_ENCRYPTIONS_PER_KMS, 'Encryptions per KMS', 'kms_description') Metrics.createCounter(Const.METRICS_KMS_DECRYPTIONS_PER_KMS, 'Decryptions per KMS', 'kms_description') Metrics.createCounter(Const.METRICS_LOGIN_APIKEYS_PER_KEY, 'Login per API key', 'apikey_description') + Metrics.createCounter(Const.METRICS_CACHE_HITS, 'Cache hits') + Metrics.createCounter(Const.METRICS_CACHE_MISSES, 'Cache misses') } // HTTP(S) server start diff --git a/test/metrics.spec.cjs b/test/metrics.spec.cjs index 2641a0f..233b88e 100644 --- a/test/metrics.spec.cjs +++ b/test/metrics.spec.cjs @@ -33,6 +33,8 @@ describe('Metrics', function () { assert.match(res1.text, /kms_encryptions_per_kms_total/) assert.match(res1.text, /kms_decryptions_per_kms_total/) assert.match(res1.text, /login_apikeys_per_key_total/) + assert.match(res1.text, /cache_hits_total/) + assert.match(res1.text, /cache_misses_total/) }) it('Check per-KMS metrics', async () => {