1919 */
2020
2121import { Logger } from 'pino' ;
22- import { Registry } from 'prom-client' ;
22+ import { Counter , Registry } from 'prom-client' ;
2323import { ICacheClient } from '../../clients/cache/ICacheClient' ;
2424import { LocalLRUCache , RedisCache } from '../../clients' ;
2525import { RedisCacheError } from '../../errors/RedisCacheError' ;
@@ -63,6 +63,21 @@ export class CacheService {
6363 * @param {Logger } logger - The logger used for logging all output from this class.
6464 * @param {Registry } register - The metrics register used for metrics tracking.
6565 */
66+
67+ private static readonly cacheTypes = {
68+ REDIS : 'redis' ,
69+ LRU : 'lru' ,
70+ } ;
71+
72+ private static readonly methods = {
73+ GET : 'get' ,
74+ GET_ASYNC : 'getAsync' ,
75+ SET : 'set' ,
76+ DELETE : 'delete' ,
77+ } ;
78+
79+ private readonly cacheMethodsCounter : Counter ;
80+
6681 public constructor ( logger : Logger , register : Registry ) {
6782 this . logger = logger ;
6883 this . register = register ;
@@ -74,6 +89,21 @@ export class CacheService {
7489 if ( this . isSharedCacheEnabled ) {
7590 this . sharedCache = new RedisCache ( logger . child ( { name : 'redisCache' } ) , register ) ;
7691 }
92+
93+ /**
94+ * Labels:
95+ * callingMethod - The method initiating the cache operation
96+ * cacheType - redis/lru
97+ * method - The CacheService method being called
98+ */
99+ const metricName = 'rpc_cache_service_methods_counter' ;
100+ this . register . removeSingleMetric ( metricName ) ;
101+ this . cacheMethodsCounter = new Counter ( {
102+ name : metricName ,
103+ help : 'Counter for calls to methods of CacheService separated by CallingMethod and CacheType' ,
104+ registers : [ register ] ,
105+ labelNames : [ 'callingMethod' , 'cacheType' , 'method' ] ,
106+ } ) ;
77107 }
78108
79109 /**
@@ -99,21 +129,21 @@ export class CacheService {
99129 * @param {string } [requestIdPrefix] - The optional request ID prefix.
100130 * @returns {Promise<any> } A Promise that resolves with the cached value or null if not found.
101131 */
102- public async getAsync (
103- key : string ,
104- callingMethod : string ,
105- requestIdPrefix ?: string
106- ) : Promise < any > {
132+ public async getAsync ( key : string , callingMethod : string , requestIdPrefix ?: string ) : Promise < any > {
107133 if ( ! this . isSharedCacheEnabled ) {
108134 return null ;
109135 }
110136
111137 try {
138+ this . cacheMethodsCounter
139+ . labels ( callingMethod , CacheService . cacheTypes . REDIS , CacheService . methods . GET_ASYNC )
140+ . inc ( 1 ) ;
141+
112142 return await this . sharedCache . get ( key , callingMethod , requestIdPrefix ) ;
113143 } catch ( error ) {
114144 const redisError = new RedisCacheError ( error ) ;
115145 this . logger . error (
116- `${ requestIdPrefix } Error occurred while getting the cache from Redis. Fallback to internal cache. Error is: ${ redisError . fullError } `
146+ `${ requestIdPrefix } Error occurred while getting the cache from Redis. Fallback to internal cache. Error is: ${ redisError . fullError } ` ,
117147 ) ;
118148 }
119149 }
@@ -127,6 +157,8 @@ export class CacheService {
127157 * @returns {Promise<any> } A Promise that resolves with the cached value or null if not found.
128158 */
129159 public get ( key : string , callingMethod : string , requestIdPrefix ?: string ) : any {
160+ this . cacheMethodsCounter . labels ( callingMethod , CacheService . cacheTypes . LRU , CacheService . methods . GET ) . inc ( 1 ) ;
161+
130162 return this . internalCache . get ( key , callingMethod , requestIdPrefix ) ;
131163 }
132164
@@ -147,18 +179,22 @@ export class CacheService {
147179 callingMethod : string ,
148180 ttl ?: number ,
149181 requestIdPrefix ?: string ,
150- shared : boolean = false
182+ shared : boolean = false ,
151183 ) : void {
152184 if ( shared && this . isSharedCacheEnabled ) {
153185 try {
186+ this . cacheMethodsCounter . labels ( callingMethod , CacheService . cacheTypes . REDIS , CacheService . methods . SET ) . inc ( 1 ) ;
187+
154188 this . sharedCache . set ( key , value , callingMethod , ttl , requestIdPrefix ) ;
155189 } catch ( error ) {
156190 const redisError = new RedisCacheError ( error ) ;
157191 this . logger . error (
158- `${ requestIdPrefix } Error occurred while setting the cache to Redis. Fallback to internal cache. Error is: ${ redisError . fullError } `
192+ `${ requestIdPrefix } Error occurred while setting the cache to Redis. Fallback to internal cache. Error is: ${ redisError . fullError } ` ,
159193 ) ;
160194 }
161195 } else {
196+ this . cacheMethodsCounter . labels ( callingMethod , CacheService . cacheTypes . LRU , CacheService . methods . SET ) . inc ( 1 ) ;
197+
162198 this . internalCache . set ( key , value , callingMethod , ttl , requestIdPrefix ) ;
163199 }
164200 }
@@ -175,14 +211,20 @@ export class CacheService {
175211 public delete ( key : string , callingMethod : string , requestIdPrefix ?: string , shared : boolean = false ) : void {
176212 if ( shared && this . isSharedCacheEnabled ) {
177213 try {
214+ this . cacheMethodsCounter
215+ . labels ( callingMethod , CacheService . cacheTypes . REDIS , CacheService . methods . DELETE )
216+ . inc ( 1 ) ;
217+
178218 return this . sharedCache . delete ( key , callingMethod , requestIdPrefix ) ;
179219 } catch ( error ) {
180220 const redisError = new RedisCacheError ( error ) ;
181221 this . logger . error (
182- `${ requestIdPrefix } Error occurred while deleting cache from Redis. Error is: ${ redisError . fullError } `
222+ `${ requestIdPrefix } Error occurred while deleting cache from Redis. Error is: ${ redisError . fullError } ` ,
183223 ) ;
184224 }
185225 } else {
226+ this . cacheMethodsCounter . labels ( callingMethod , CacheService . cacheTypes . LRU , CacheService . methods . DELETE ) . inc ( 1 ) ;
227+
186228 this . internalCache . delete ( key , callingMethod , requestIdPrefix ) ;
187229 }
188230 }
@@ -200,7 +242,7 @@ export class CacheService {
200242 } catch ( error ) {
201243 const redisError = new RedisCacheError ( error ) ;
202244 this . logger . error (
203- `${ requestIdPrefix } Error occurred while clearing Redis cache. Error is: ${ redisError . fullError } `
245+ `${ requestIdPrefix } Error occurred while clearing Redis cache. Error is: ${ redisError . fullError } ` ,
204246 ) ;
205247 }
206248 } else {
0 commit comments