diff --git a/src/helpers/cache.ts b/src/helpers/cache.ts index 92d08eb1..961e87e9 100644 --- a/src/helpers/cache.ts +++ b/src/helpers/cache.ts @@ -1,5 +1,6 @@ import redis from '../redis'; import { get, set } from '../aws'; +import { cacheActivitesCount } from '../metrics'; export const VP_KEY_PREFIX = 'vp'; @@ -15,6 +16,7 @@ export async function cachedVp>( toCache = true ) { if (!toCache || !redis) { + cacheActivitesCount.inc({ type: 'vp', status: 'skip' }); return { result: await callback(), cache: false }; } @@ -24,12 +26,15 @@ export async function cachedVp>( cache.vp = parseFloat(cache.vp); cache.vp_by_strategy = JSON.parse(cache.vp_by_strategy); + cacheActivitesCount.inc({ type: 'vp', status: 'hit' }); return { result: cache as Awaited, cache: true }; } const result = await callback(); + let cacheHitStatus = 'unqualified'; if (result.vp_state === 'final') { + cacheHitStatus = 'miss'; const multi = redis.multi(); multi.hSet(`${VP_KEY_PREFIX}:${key}`, 'vp', result.vp); multi.hSet(`${VP_KEY_PREFIX}:${key}`, 'vp_by_strategy', JSON.stringify(result.vp_by_strategy)); @@ -37,22 +42,26 @@ export async function cachedVp>( multi.exec(); } + cacheActivitesCount.inc({ type: 'vp', status: cacheHitStatus }); return { result, cache: false }; } export async function cachedScores(key: string, callback: () => Type, toCache = false) { if (!toCache || !!process.env.AWS_REGION) { + cacheActivitesCount.inc({ type: 'scores', status: 'skip' }); return { scores: await callback(), cache: false }; } const cache = await get(key); if (cache) { + cacheActivitesCount.inc({ type: 'scores', status: 'hit' }); return { scores: cache as Awaited, cache: true }; } const scores = await callback(); set(key, scores); + cacheActivitesCount.inc({ type: 'scores', status: 'miss' }); return { scores, cache: false }; } diff --git a/src/metrics.ts b/src/metrics.ts index 10b0d90d..36ff3ef9 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -30,3 +30,9 @@ export const requestDeduplicatorSize = new client.Gauge({ name: 'request_deduplicator_size', help: 'Total number of items in the deduplicator queue' }); + +export const cacheActivitesCount = new client.Gauge({ + name: 'cache_activites_count', + help: 'Number of requests to the cache layer', + labelNames: ['type', 'status'] +});