From 1760d6f0c4579622247ce0486222bc9c708f9e2e Mon Sep 17 00:00:00 2001 From: Nour Alharithi Date: Thu, 14 Dec 2023 16:04:24 -0800 Subject: [PATCH] fix circular references --- src/core/metrics.ts | 19 ++----------------- src/index.ts | 12 ++++++++++++ src/serverLite.ts | 25 ++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/core/metrics.ts b/src/core/metrics.ts index 6b102f7..09e920f 100644 --- a/src/core/metrics.ts +++ b/src/core/metrics.ts @@ -7,13 +7,6 @@ import { MeterProvider, View, } from '@opentelemetry/sdk-metrics-base'; -import { - ORDERBOOK_UPDATE_INTERVAL, - commitHash, - driftEnv, - endpoint, - wsEndpoint, -} from '..'; import { SlotSource } from '@drift-labs/sdk'; /** @@ -93,15 +86,6 @@ const runtimeSpecsGauge = meter.createObservableGauge( description: 'Runtime sepcification of this program', } ); -const bootTimeMs = Date.now(); -runtimeSpecsGauge.addCallback((obs) => { - obs.observe(bootTimeMs, { - commit: commitHash, - driftEnv, - rpcEndpoint: endpoint, - wsEndpoint: wsEndpoint, - }); -}); let healthStatus: HEALTH_STATUS = HEALTH_STATUS.Ok; const healthStatusGauge = meter.createObservableGauge( @@ -171,7 +155,7 @@ const responseStatusCounter = meter.createCounter( } ); -const healthCheckInterval = 2 * (ORDERBOOK_UPDATE_INTERVAL ?? 1000); // ORDERBOOK_UPDATE_INTERVAL is NaN here for some reason ... hardcode to 1000. +const healthCheckInterval = 2000; let lastHealthCheckSlot = -1; let lastHealthCheckState = true; // true = healthy, false = unhealthy let lastHealthCheckPerformed = Date.now() - healthCheckInterval; @@ -245,4 +229,5 @@ export { setLastReceivedWsMsgTs, accountUpdatesCounter, cacheHitCounter, + runtimeSpecsGauge, }; diff --git a/src/index.ts b/src/index.ts index d68990a..59ac17e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,6 +39,7 @@ import { accountUpdatesCounter, cacheHitCounter, setLastReceivedWsMsgTs, + runtimeSpecsGauge, } from './core/metrics'; import { handleResponseTime } from './core/middleware'; import { @@ -125,6 +126,17 @@ app.use((req, _res, next) => { next(); }); +// Metrics defined here +const bootTimeMs = Date.now(); +runtimeSpecsGauge.addCallback((obs) => { + obs.observe(bootTimeMs, { + commit: commitHash, + driftEnv, + rpcEndpoint: endpoint, + wsEndpoint: wsEndpoint, + }); +}); + app.use(errorHandler); const server = http.createServer(app); diff --git a/src/serverLite.ts b/src/serverLite.ts index 4587672..8a03b22 100644 --- a/src/serverLite.ts +++ b/src/serverLite.ts @@ -10,7 +10,11 @@ import { Commitment, Connection } from '@solana/web3.js'; import { logger, setLogLevel } from './utils/logger'; import * as http from 'http'; -import { handleHealthCheck, cacheHitCounter } from './core/metrics'; +import { + handleHealthCheck, + cacheHitCounter, + runtimeSpecsGauge, +} from './core/metrics'; import { handleResponseTime } from './core/middleware'; import { errorHandler, normalizeBatchQueryParams, sleep } from './utils/utils'; import { RedisClient } from './utils/redisClient'; @@ -21,6 +25,7 @@ import { SlotSubscriber, SpotMarkets, isVariant, + initialize, } from '@drift-labs/sdk'; require('dotenv').config(); @@ -72,6 +77,17 @@ app.use( }) ); +// Metrics defined here +const bootTimeMs = Date.now(); +runtimeSpecsGauge.addCallback((obs) => { + obs.observe(bootTimeMs, { + commit: commitHash, + driftEnv, + rpcEndpoint: endpoint, + wsEndpoint: wsEndpoint, + }); +}); + // strip off /dlob, if the request comes from exchange history server LB app.use((req, _res, next) => { if (req.url.startsWith('/dlob')) { @@ -183,7 +199,7 @@ const main = async () => { if (redisL2) { slotDiff = slotSubscriber.getSlot() - parseInt(JSON.parse(redisL2).slot); - if (slotDiff < 20) { + if (slotDiff < SLOT_STALENESS_TOLERANCE) { l2Formatted = redisL2; } } @@ -211,7 +227,7 @@ const main = async () => { if (redisL2) { slotDiff = slotSubscriber.getSlot() - parseInt(JSON.parse(redisL2).slot); - if (slotDiff < 20) { + if (slotDiff < SLOT_STALENESS_TOLERANCE) { l2Formatted = redisL2; } } @@ -228,6 +244,9 @@ const main = async () => { if (slotDiff) { res.writeHead(500); res.end(`Slot too stale : ${slotDiff}`); + } else { + res.writeHead(400); + res.end(`Bad Request: no cached L2 found`); } } } catch (err) {