Skip to content

Commit 73cb357

Browse files
authored
Merge pull request #1049 from snyk/feat/client-websocket-duration-logging
feat: client websocket duration logging [ACC-3075]
2 parents 734265f + f95434e commit 73cb357

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

lib/hybrid-sdk/client/socket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export const createWebSocket = (
291291
websocket.on('service', serviceHandler);
292292

293293
websocket.on('close', () => {
294-
closeHandler(localClientOps, identifyingMetadata);
294+
closeHandler(websocket, localClientOps, identifyingMetadata);
295295
});
296296

297297
return websocket;

lib/hybrid-sdk/client/socketHandlers/closeHandler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { LoadedClientOpts } from '../../common/types/options';
22
import { log as logger } from '../../../logs/logger';
3+
import { WebSocketConnection } from '../types/client';
34

45
export const closeHandler = (
6+
websocket: WebSocketConnection,
57
clientOps: LoadedClientOpts,
68
identifyingMetadata,
79
) => {
10+
// default duration of -1 so that it is obvious if this misbehaves
11+
let durationMs = -1;
12+
if (websocket.connectionStartTime) {
13+
durationMs = Date.now() - websocket.connectionStartTime;
14+
}
815
logger.warn(
916
{
1017
url: clientOps.config.brokerServerUrl,
1118
token: clientOps.config.universalBrokerEnabled
1219
? identifyingMetadata.integrationId
1320
: clientOps.config.brokerToken,
21+
durationMs,
1422
},
1523
'Websocket connection to the broker server was closed.',
1624
);

lib/hybrid-sdk/client/socketHandlers/openHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const openHandler = (
3535
},
3636
'Successfully established a websocket connection to the broker server.',
3737
);
38+
io.connectionStartTime = Date.now();
3839
logger.debug(
3940
{
4041
metadata,

lib/hybrid-sdk/client/types/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface WebSocketConnection
6262
socketVersion?: number;
6363
supportedIntegrationType: string;
6464
timeoutHandlerId?: NodeJS.Timeout;
65+
connectionStartTime?: number;
6566

6667
// Added by primus, but specific to the client
6768
socket: any;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { closeHandler } from '../../../../lib/hybrid-sdk/client/socketHandlers/closeHandler';
2+
import { log as logger } from '../../../../lib/logs/logger';
3+
import { WebSocketConnection } from '../../../../lib/hybrid-sdk/client/types/client';
4+
import { LoadedClientOpts } from '../../../../lib/hybrid-sdk/common/types/options';
5+
6+
jest.mock('../../../../lib/logs/logger');
7+
8+
describe('closeHandler', () => {
9+
let mockWebsocket: WebSocketConnection;
10+
let mockClientOpts: LoadedClientOpts;
11+
12+
beforeEach(() => {
13+
jest.clearAllMocks();
14+
mockWebsocket = {
15+
connectionStartTime: undefined,
16+
} as unknown as WebSocketConnection;
17+
mockClientOpts = {
18+
config: {
19+
brokerServerUrl: 'https://broker.snyk.io',
20+
brokerToken: 'mock-token',
21+
universalBrokerEnabled: false,
22+
},
23+
} as unknown as LoadedClientOpts;
24+
});
25+
26+
it('should log warning with durationMs when connectionStartTime is present', () => {
27+
const startTime = Date.now() - 5000; // 5 seconds ago
28+
mockWebsocket.connectionStartTime = startTime;
29+
30+
// Mock Date.now to return startTime + 5000
31+
const now = startTime + 5000;
32+
jest.spyOn(Date, 'now').mockReturnValue(now);
33+
34+
closeHandler(mockWebsocket, mockClientOpts, {});
35+
36+
expect(logger.warn).toHaveBeenCalledWith(
37+
expect.objectContaining({
38+
durationMs: 5000,
39+
url: 'https://broker.snyk.io',
40+
token: 'mock-token',
41+
}),
42+
'Websocket connection to the broker server was closed.',
43+
);
44+
});
45+
46+
it('should log warning with durationMs -1 when connectionStartTime is undefined', () => {
47+
closeHandler(mockWebsocket, mockClientOpts, {});
48+
49+
expect(logger.warn).toHaveBeenCalledWith(
50+
expect.objectContaining({
51+
durationMs: -1,
52+
url: 'https://broker.snyk.io',
53+
token: 'mock-token',
54+
}),
55+
'Websocket connection to the broker server was closed.',
56+
);
57+
});
58+
});

0 commit comments

Comments
 (0)