From 61a39b511596a7396c8aee2386f09fc99d529b9b Mon Sep 17 00:00:00 2001 From: Wan <495709+wa0x6e@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:41:45 +0800 Subject: [PATCH] chore: instrument the cache engine (#250) --- src/lib/cache.ts | 9 +++++++-- src/lib/metrics/index.ts | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lib/cache.ts b/src/lib/cache.ts index 181b60f..d931b48 100644 --- a/src/lib/cache.ts +++ b/src/lib/cache.ts @@ -1,4 +1,5 @@ import { IStorage } from './storage/types'; +import { cacheHitCount } from './metrics'; export default class Cache { id: string; @@ -17,8 +18,12 @@ export default class Cache { return ''; } - getCache() { - return this.storage.get(this.filename); + async getCache() { + const cache = await this.storage.get(this.filename); + + cacheHitCount.inc({ status: !cache ? 'MISS' : 'HIT', type: this.constructor.name }); + + return cache; } async isCacheable() { diff --git a/src/lib/metrics/index.ts b/src/lib/metrics/index.ts index f4a0d08..56c2f97 100644 --- a/src/lib/metrics/index.ts +++ b/src/lib/metrics/index.ts @@ -157,6 +157,12 @@ try { } } +export const cacheHitCount = new client.Counter({ + name: 'cache_hit_count', + help: 'Number of hit/miss of the cache engine', + labelNames: ['status', 'type'] +}); + const providersResponseCode = new client.Gauge({ name: 'provider_response_code', help: 'Response code of each provider request',