Skip to content

Commit

Permalink
Telemetry data object updated with sent dates and clean up after mess…
Browse files Browse the repository at this point in the history
…age is sent, touch #456.
  • Loading branch information
PaulDalek committed Jul 11, 2024
1 parent fe48746 commit 8425836
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
39 changes: 35 additions & 4 deletions lib/server/web_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getOptions } from '../config.js';
import { log, logWithStack } from '../logger.js';
import { telemetryData } from '../telemetry.js';
import { addTimer } from '../timers.js';
import { getNewDate } from '../utils.js';

// WebSocket clients map
const webSocketClients = new Map();
Expand Down Expand Up @@ -75,24 +76,50 @@ export function init(address) {
}
}

/**
* Sets up an interval to send messages through a WebSocket connection
* at a specified interval.
*
* @param {Object} webSocketOptions - Options for the WebSocket connection.
*/
function sendingMessageInterval(webSocketOptions) {
// Set the sending message interval
messageInterval = setInterval(() => {
try {
// Get the first WebSocket client
const webSocketClient = getClients().next().value;

// Log info about message sending process
log(3, `[websocket] WebSocket message sending queue.`);

// If the client is found, open and there is data to send
if (
webSocketClient &&
webSocketClient.readyState === WebSocket.OPEN &&
Object.keys(telemetryData).length > 1 &&
Object.keys(telemetryData.optionsPerRequest).length > 0 &&
telemetryData.numberOfRequests > 0
) {
// Log info about message sending process
log(3, `[websocket] Sending data through a WebSocket connection.`);

// Set the dates
const currentDate = getNewDate();
telemetryData.lastSent = telemetryData.timeOfSent || currentDate;
telemetryData.timeOfSent = currentDate;

// Send through the WebSocket
webSocketClient.send(JSON.stringify(telemetryData));

// Clear the requests number and data before collecting a new one
telemetryData.numberOfRequests = 0;
telemetryData.optionsPerRequest = {};
}
} catch (error) {
logWithStack(1, `[websocket] Could not send data through WebSocket.`);
logWithStack(
1,
error,
`[websocket] Could not send data through WebSocket.`
);
}
}, webSocketOptions.messageInterval);

Expand Down Expand Up @@ -153,8 +180,12 @@ function connect(webSocketUrl, connectionOptions, clientOptions) {
`[websocket] WebSocket: ${clientOptions.id} - Error occured.`
);

// Block the reconnect mechanism when getting 403
if (error.message.includes('403')) {
// Block the reconnect mechanism when getting 403 or self-signed certificate
// error code
if (
error.message.includes('403') ||
error.code === 'DEPTH_ZERO_SELF_SIGNED_CERT'
) {
clientOptions.reconnect = false;
clientOptions.reconnectTry = webSocketOptions.reconnectAttempts;
} else {
Expand Down
6 changes: 5 additions & 1 deletion lib/telemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const telemetryTemplate = JSON.parse(

// The object with telemetry data collected
export const telemetryData = {
timeOfSent: null,
lastSent: null,
optionsPerRequest: {},
numberOfRequests: 0
};

Expand Down Expand Up @@ -82,7 +85,8 @@ function filterData(template, options) {

export function prepareTelemetry(chartOptions, requestId) {
// Save the filtered or absolute options under the request's id
telemetryData[requestId] = getOptions().webSocket.gatherAllData
telemetryData.optionsPerRequest[requestId] = getOptions().webSocket
.gatherAllOptions
? chartOptions
: filterData(telemetryTemplate, chartOptions);

Expand Down

0 comments on commit 8425836

Please sign in to comment.