Skip to content

Commit 55be48c

Browse files
committed
Add WebSocket failover counter metric, abnormal closure tracking, and URL change logging
1 parent 8057302 commit 55be48c

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/metrics/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,9 @@ export const metrics = new Metrics(() => ({
357357
help: 'The number of addresses in PoR request input parameters',
358358
labelNames: ['feed_id'] as const,
359359
}),
360+
wsConnectionFailoverCount: new client.Gauge({
361+
name: 'ws_connection_failover_count',
362+
help: 'The number of consecutive connection issues (unresponsive/no data, abnormal closures), used to trigger URL failover. Resets to 0 when data flows successfully.',
363+
labelNames: ['transport_name'] as const,
364+
}),
360365
}))

src/transports/websocket.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ export class WebSocketTransport<
295295
`Closed websocket connection. Code: ${event.code} ; reason: ${event.reason?.toString()}`,
296296
)
297297

298+
// If abnormal closure, increment failover counter to trigger potential URL switch
299+
// Code 1000 is normal closure, all other codes indicate abnormal disconnections
300+
if (event.code !== 1000) {
301+
this.streamHandlerInvocationsWithNoConnection += 1
302+
logger.info(
303+
`Abnormal closure detected (code ${event.code}), incremented failover counter to ${this.streamHandlerInvocationsWithNoConnection}`,
304+
)
305+
metrics.get('wsConnectionFailoverCount').labels({ transport_name: this.name }).set(this.streamHandlerInvocationsWithNoConnection)
306+
}
307+
298308
// Record active ws connections by decrementing count on close
299309
// Using URL in label since connection_key is removed from v3
300310
metrics.get('wsConnectionActive').dec()
@@ -414,9 +424,10 @@ export class WebSocketTransport<
414424
// to determine minimum TTL of an open connection given no explicit connection errors.
415425
if (connectionUnresponsive) {
416426
this.streamHandlerInvocationsWithNoConnection += 1
417-
logger.trace(
418-
`The connection is unresponsive, incremented streamHandlerIterationsWithNoConnection = ${this.streamHandlerInvocationsWithNoConnection}`,
427+
logger.info(
428+
`The connection is unresponsive (last message ${timeSinceLastMessage}ms ago), incremented failover counter to ${this.streamHandlerInvocationsWithNoConnection}`,
419429
)
430+
metrics.get('wsConnectionFailoverCount').labels({ transport_name: this.name }).set(this.streamHandlerInvocationsWithNoConnection)
420431
}
421432

422433
// We want to check if the URL we calculate is different from the one currently connected.
@@ -431,9 +442,10 @@ export class WebSocketTransport<
431442
// Check if we should close the current connection
432443
if (!connectionClosed && (urlChanged || connectionUnresponsive)) {
433444
if (urlChanged) {
445+
logger.info('Websocket URL has changed, closing connection to reconnect...')
434446
censorLogs(() =>
435447
logger.debug(
436-
`Websocket url has changed from ${this.currentUrl} to ${urlFromConfig}, closing connection...`,
448+
`Websocket URL changed from ${this.currentUrl} to ${urlFromConfig}`,
437449
),
438450
)
439451
} else {

0 commit comments

Comments
 (0)