record last received ws message

This commit is contained in:
wphan
2023-12-07 10:55:03 -08:00
parent 281cffd54f
commit 0e7a056825
2 changed files with 37 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ enum METRIC_TYPES {
endpoint_response_times_histogram = 'endpoint_response_times_histogram', endpoint_response_times_histogram = 'endpoint_response_times_histogram',
endpoint_response_status = 'endpoint_response_status', endpoint_response_status = 'endpoint_response_status',
gpa_fetch_duration = 'gpa_fetch_duration', gpa_fetch_duration = 'gpa_fetch_duration',
last_ws_message_received_ts = 'last_ws_message_received_ts',
health_status = 'health_status', health_status = 'health_status',
} }
@@ -109,6 +110,20 @@ healthStatusGauge.addCallback((obs: ObservableResult) => {
obs.observe(healthStatus, {}); obs.observe(healthStatus, {});
}); });
let lastWsMsgReceivedTs = 0;
const setLastReceivedWsMsgTs = (ts: number) => {
lastWsMsgReceivedTs = ts;
};
const lastWsReceivedTsGauge = meter.createObservableGauge(
METRIC_TYPES.last_ws_message_received_ts,
{
description: 'Timestamp of last received websocket message',
}
);
lastWsReceivedTsGauge.addCallback((obs: ObservableResult) => {
obs.observe(lastWsMsgReceivedTs, {});
});
const endpointResponseTimeHistogram = meter.createHistogram( const endpointResponseTimeHistogram = meter.createHistogram(
METRIC_TYPES.endpoint_response_times_histogram, METRIC_TYPES.endpoint_response_times_histogram,
{ {
@@ -202,4 +217,5 @@ export {
gpaFetchDurationHistogram, gpaFetchDurationHistogram,
responseStatusCounter, responseStatusCounter,
handleHealthCheck, handleHealthCheck,
setLastReceivedWsMsgTs,
}; };

View File

@@ -28,12 +28,18 @@ import {
initialize, initialize,
isVariant, isVariant,
OrderSubscriber, OrderSubscriber,
UserAccount,
Order,
} from '@drift-labs/sdk'; } from '@drift-labs/sdk';
import { logger, setLogLevel } from './utils/logger'; import { logger, setLogLevel } from './utils/logger';
import * as http from 'http'; import * as http from 'http';
import { gpaFetchDurationHistogram, handleHealthCheck } from './core/metrics'; import {
gpaFetchDurationHistogram,
handleHealthCheck,
setLastReceivedWsMsgTs,
} from './core/metrics';
import { handleResponseTime } from './core/middleware'; import { handleResponseTime } from './core/middleware';
import { import {
SubscriberLookup, SubscriberLookup,
@@ -252,6 +258,20 @@ const main = async () => {
driftClient, driftClient,
subscriptionConfig, subscriptionConfig,
}); });
orderSubscriber.eventEmitter.on(
'onUpdate',
(
_account: UserAccount,
_updatedOrders: Order[],
_pubkey: PublicKey,
_slot: number,
dataType: 'raw' | 'decoded'
) => {
if (dataType === 'decoded') {
setLastReceivedWsMsgTs(Date.now());
}
}
);
dlobProvider = getDLOBProviderFromOrderSubscriber(orderSubscriber); dlobProvider = getDLOBProviderFromOrderSubscriber(orderSubscriber);