diff --git a/src/core/metrics.ts b/src/core/metrics.ts index 9675b68..d1683f4 100644 --- a/src/core/metrics.ts +++ b/src/core/metrics.ts @@ -40,6 +40,8 @@ enum METRIC_TYPES { gpa_fetch_duration = 'gpa_fetch_duration', last_ws_message_received_ts = 'last_ws_message_received_ts', account_updates_count = 'account_updates_count', + cache_hit_count = 'cache_hit_count', + cache_miss_count = 'cache_miss_count', current_system_ts = 'current_system_ts', health_status = 'health_status', } @@ -126,6 +128,14 @@ lastWsReceivedTsGauge.addCallback((obs: ObservableResult) => { obs.observe(lastWsMsgReceivedTs, {}); }); +const cacheHitCounter = meter.createCounter(METRIC_TYPES.cache_hit_count, { + description: 'Total redis cache hits', +}); + +const cacheMissCounter = meter.createCounter(METRIC_TYPES.cache_miss_count, { + description: 'Total redis cache misses', +}); + const accountUpdatesCounter = meter.createCounter( METRIC_TYPES.account_updates_count, { @@ -238,4 +248,6 @@ export { handleHealthCheck, setLastReceivedWsMsgTs, accountUpdatesCounter, + cacheHitCounter, + cacheMissCounter, }; diff --git a/src/index.ts b/src/index.ts index 7e688b2..c4031da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,7 +37,9 @@ import { gpaFetchDurationHistogram, handleHealthCheck, accountUpdatesCounter, + cacheHitCounter, setLastReceivedWsMsgTs, + cacheMissCounter, } from './core/metrics'; import { handleResponseTime } from './core/middleware'; import { @@ -768,22 +770,10 @@ const main = async () => { redisL2 = await redisClient.client.get( `last_update_orderbook_perp_${normedMarketIndex}_depth_5` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 20) { redisL2 = await redisClient.client.get( `last_update_orderbook_perp_${normedMarketIndex}_depth_20` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 100) { redisL2 = await redisClient.client.get( `last_update_orderbook_perp_${normedMarketIndex}_depth_100` @@ -791,9 +781,7 @@ const main = async () => { } if ( redisL2 && - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 + slotSource.getSlot() - parseInt(JSON.parse(redisL2).slot) < 10 ) l2Formatted = redisL2; } else if ( @@ -808,22 +796,10 @@ const main = async () => { redisL2 = await redisClient.client.get( `last_update_orderbook_spot_${normedMarketIndex}_depth_5` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 20) { redisL2 = await redisClient.client.get( `last_update_orderbook_spot_${normedMarketIndex}_depth_20` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 100) { redisL2 = await redisClient.client.get( `last_update_orderbook_spot_${normedMarketIndex}_depth_100` @@ -831,14 +807,13 @@ const main = async () => { } if ( redisL2 && - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 + slotSource.getSlot() - parseInt(JSON.parse(redisL2).slot) < 10 ) l2Formatted = redisL2; } if (l2Formatted) { + cacheHitCounter.add(1); res.writeHead(200); res.end(JSON.stringify(l2Formatted)); return; @@ -896,7 +871,7 @@ const main = async () => { ); } } - + cacheMissCounter.add(1); res.writeHead(200); res.end(JSON.stringify(l2Formatted)); } catch (err) { @@ -976,27 +951,20 @@ const main = async () => { redisL2 = await redisClient.client.get( `last_update_orderbook_perp_${normedMarketIndex}_depth_5` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 20) { redisL2 = await redisClient.client.get( `last_update_orderbook_perp_${normedMarketIndex}_depth_20` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 100) { redisL2 = await redisClient.client.get( `last_update_orderbook_perp_${normedMarketIndex}_depth_100` ); } + if ( + redisL2 && + slotSource.getSlot() - parseInt(JSON.parse(redisL2).slot) < 10 + ) + l2Formatted = redisL2; } else if ( marketType === 'spot' && normedParam['includePhoenix'].toLowerCase() === 'true' && @@ -1008,22 +976,10 @@ const main = async () => { redisL2 = await redisClient.client.get( `last_update_orderbook_spot_${normedMarketIndex}_depth_5` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 20) { redisL2 = await redisClient.client.get( `last_update_orderbook_spot_${normedMarketIndex}_depth_20` ); - if ( - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 - ) - l2Formatted = redisL2; } else if (parseInt(adjustedDepth as string) === 100) { redisL2 = await redisClient.client.get( `last_update_orderbook_spot_${normedMarketIndex}_depth_100` @@ -1031,14 +987,13 @@ const main = async () => { } if ( redisL2 && - Math.abs( - parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot() - ) < 10 + slotSource.getSlot() - parseInt(JSON.parse(redisL2).slot) < 10 ) l2Formatted = redisL2; } if (l2Formatted) { + cacheHitCounter.add(1); return l2Formatted; } } @@ -1097,6 +1052,7 @@ const main = async () => { ); } } + cacheMissCounter.add(1); return l2Formatted; }) );