Skip to content

Commit

Permalink
fix(bonsai-core): add info logs for final debugging push (#1428)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo authored Jan 13, 2025
1 parent 8bd6bdf commit 9cf3969
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/abacus-ts/logs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { log } from '@/lib/telemetry';
import { log, logInfo } from '@/lib/telemetry';

export function logAbacusTsError(source: string, message: string, ...args: any[]) {
log(`bonsai: ${source}: ${message}`, undefined, { context: args });
}

export function logAbacusTsInfo(source: string, message: string, ...args: any[]) {
logInfo(`bonsai: ${source}: ${message}`, { context: args });
}
35 changes: 32 additions & 3 deletions src/abacus-ts/websocket/lib/indexerWebsocket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logAbacusTsError } from '@/abacus-ts/logs';
import { logAbacusTsError, logAbacusTsInfo } from '@/abacus-ts/logs';
import typia from 'typia';

import { timeUnits } from '@/constants/time';
Expand Down Expand Up @@ -84,6 +84,12 @@ export class IndexerWebsocket {
logAbacusTsError('IndexerWebsocket', 'this subscription already exists', `${channel}/${id}`);
throw new Error(`IndexerWebsocket error: this subscription already exists. ${channel}/${id}`);
}
logAbacusTsInfo('IndexerWebsocket', 'adding subscription', {
channel,
id,
socketNonNull: this.socket != null,
socketActive: this.socket?.isActive(),
});
this.subscriptions[channel][id ?? NO_ID_SPECIAL_STRING_ID] = {
channel,
id,
Expand Down Expand Up @@ -121,6 +127,12 @@ export class IndexerWebsocket {
);
return;
}
logAbacusTsInfo('IndexerWebsocket', 'removing subscription', {
channel,
id,
socketNonNull: this.socket != null,
socketActive: this.socket?.isActive(),
});
if (
this.socket != null &&
this.socket.isActive() &&
Expand Down Expand Up @@ -157,7 +169,7 @@ export class IndexerWebsocket {
if (Date.now() - lastRefresh > CHANNEL_RETRY_COOLDOWN_MS) {
this.lastRetryTimeMsByChannel[maybeChannel] = Date.now();
this._refreshChannelSubs(maybeChannel);
logAbacusTsError(
logAbacusTsInfo(
'IndexerWebsocket',
'error fetching data for channel, refetching',
maybeChannel
Expand All @@ -176,8 +188,13 @@ export class IndexerWebsocket {
const message = isWsMessage(messagePre);
if (message.type === 'error') {
this._handleErrorReceived(message.message);
} else if (message.type === 'connected' || message.type === 'unsubscribed') {
} else if (message.type === 'connected') {
// do nothing
} else if (message.type === 'unsubscribed') {
logAbacusTsInfo('IndexerWebsocket', `unsubscribe confirmed`, {
channel: message.channel,
id: message.id,
});
} else if (
message.type === 'subscribed' ||
message.type === 'channel_batch_data' ||
Expand All @@ -186,6 +203,7 @@ export class IndexerWebsocket {
) {
const channel = message.channel;
const id = message.id;

if (this.subscriptions[channel] == null) {
// hide error for channel we expect to see it on
if (channel !== 'v4_orderbook') {
Expand All @@ -211,6 +229,10 @@ export class IndexerWebsocket {
return;
}
if (message.type === 'subscribed') {
logAbacusTsInfo('IndexerWebsocket', `subscription confirmed`, {
channel,
id,
});
this.subscriptions[channel][id ?? NO_ID_SPECIAL_STRING_ID]!.handleBaseData(
message.contents,
message
Expand Down Expand Up @@ -239,6 +261,13 @@ export class IndexerWebsocket {

// when websocket churns, reconnect all known subscribers
private _handleFreshConnect = () => {
logAbacusTsInfo('IndexerWebsocket', 'freshly connected', {
socketNonNull: this.socket != null,
socketActive: this.socket?.isActive(),
numSubs: Object.values(this.subscriptions)
.flatMap((o) => Object.values(o))
.filter(isTruthy).length,
});
if (this.socket != null && this.socket.isActive()) {
Object.values(this.subscriptions)
.filter(isTruthy)
Expand Down
9 changes: 9 additions & 0 deletions src/lib/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export const log = (location: string, error?: Error, metadata?: object) => {
globalThis.dispatchEvent(customEvent);
};

export const logInfo = (location: string, metadata?: object) => {
if (isDev) {
// eslint-disable-next-line no-console
console.log('telemetry/logInfo:', { location, metadata });
}

dd.info(`[Info] ${location}`, metadata);
};

// Log rejected Promises without a .catch() handler
globalThis.addEventListener('unhandledrejection', (event) => {
event.preventDefault();
Expand Down

0 comments on commit 9cf3969

Please sign in to comment.