Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger) send logs to aws #1030

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spot-electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (isDev || process.argv.indexOf('--show-devtools') !== -1) {
}

const { createApplicationWindow } = require('./src/application-window');
const { awsLogger } = require('./src/logger');

// Imports from features that we need to load.
// Import log transport early as it caches any logs produced even before able to send.
Expand All @@ -28,6 +29,12 @@ require('./src/client-control');
require('./src/exit');
require('./src/volume-control');

process.env.USE_CLOUDWATCH_LOGS && awsLogger.maybeCreateLogStream()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the Electron app going to get these env variables?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that the person that installs the app can set up these too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work since the app is deployed fully packaged and the environment cannot be set just for that app, you'd need to launch it from the terminal or something.

I think the right thing to do is to use a JSON file which we can add in branding, and the code can read it from there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the AWS credentials?

.catch(error => {
console.error('Error - couldn\'t identify Cloudwatch log stream', error);
process.env.USE_CLOUDWATCH_LOGS = false;
});

app.on('ready', createApplicationWindow);

app.on(
Expand Down
2,629 changes: 2,550 additions & 79 deletions spot-electron/package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions spot-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
"node-schedule": "2.1.1"
},
"devDependencies": {
"@aws-sdk/client-cloudwatch-logs": "3.395.0",
"@aws-sdk/client-firehose": "3.395.0",
"@babel/eslint-parser": "7.19.1",
"@babel/preset-env": "7.20.2",
"@types/jest": "29.2.6",
"@jitsi/eslint-config": "4.1.5",
"@types/jest": "29.2.6",
"dotenv": "16.0.3",
"electron": "22.3.18",
"electron-builder": "23.6.0",
Expand Down Expand Up @@ -82,4 +84,4 @@
"@jitsi/node-ibeacons": "0.2.0",
"win-audio": "2.0.2"
}
}
}
4 changes: 3 additions & 1 deletion spot-electron/src/application-window/applicationwindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const isDev = require('electron-is-dev');
const process = require('process');

const { defaultSpotURL } = require('../../config');
const { logger, fileLogger } = require('../logger');
const { logger, fileLogger, awsLogger } = require('../logger');
const { OnlineDetector } = require('../online-detector');

/**
Expand Down Expand Up @@ -95,6 +95,8 @@ function createApplicationWindow() {

applicationWindow.webContents.on('console-message', (_, level, message) => {
fileLogger.logToFile(level, message);
process.env.USE_S3_LOGS && awsLogger.logToS3(level, message);
process.env.USE_CLOUDWATCH_LOGS && awsLogger.logToCloudwatch(level, message);
});

onlineDetector.start();
Expand Down
139 changes: 139 additions & 0 deletions spot-electron/src/logger/awsLogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const {
CloudWatchLogsClient,
DescribeLogStreamsCommand,
PutLogEventsCommand,
CreateLogStreamCommand
} = require('@aws-sdk/client-cloudwatch-logs');
const {
FirehoseClient,
PutRecordCommand
} = require('@aws-sdk/client-firehose');

const { getHostId, parseLogMessage } = require('./utils');

const region = process.env.REGION;
const credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
};
const deliveryStreamName = process.env.FIREHOSE_DELIVERY_STREAM;
const logGroupName = process.env.CLOUDWATCH_LOG_GROUP;
const logStreamName = getHostId();

const firehoseClient = new FirehoseClient({
credentials,
region
});

const cloudwatchClient = new CloudWatchLogsClient({
credentials,
region
});

/**
* Creates a Cloudwatch log stream.
*/
const createLogStream = async () => {
const command = new CreateLogStreamCommand({
logGroupName,
logStreamName
});

try {
await cloudwatchClient.send(command);
console.log(`Successfully created log stream ${logStreamName}.`);
} catch (error) {
console.error('Error - couldn\'t create log stream:', error, error.stack);
}
};

/**
* Adds logs to a Cloudwatch log stream.
*/
const putLogEvents = async message => {
const command = new PutLogEventsCommand({
logEvents: [ {
message,
timestamp: Date.now()
} ],
logGroupName,
logStreamName
});

try {
await cloudwatchClient.send(command);
} catch (error) {
console.error('Error - couldn\'t save log events:', error, error.stack);
}
};

/**
* Creates a Cloudwatch log stream if it doesn't exist.
*/
const maybeCreateLogStream = async () => {
const command = new DescribeLogStreamsCommand({
logGroupName,
logStreamNamePrefix: logStreamName
});

try {
const response = await cloudwatchClient.send(command);

// This call can return an empty response without throwing an exception.
// Additionally, the results may not match 100%,
// So we need to make sure the log stream gets created.
const isLogStreamPresent = response.logStreams.find(stream => stream.logStreamName === logStreamName);

if (!response.logStreams.length || !isLogStreamPresent) {
createLogStream();
}
} catch (error) {
if (error.__type === 'ResourceNotFoundException') {
console.error('Error - log stream does not exist', error, error.stack);

createLogStream();
} else {
console.error('Error - couldn\'t describe log streams:', error, error.stack);
}
}
};

/**
* Logs a message to Cloudwatch.
*
* @param {string} level - The log level index.
* @param {string} message - The main string to be logged.
* @returns {void}
*/
const logToCloudwatch = async (level, message) => {
putLogEvents(parseLogMessage(level, message));
};

/**
* Logs a message to S3 via Firehose.
*
* @param {string} level - The log level index.
* @param {string} message - The main string to be logged.
* @returns {void}
*/
const logToS3 = async (level, message) => {
const command = new PutRecordCommand({
DeliveryStreamName: deliveryStreamName,
Record: {
Data: Buffer.from(`
${logStreamName} | ${new Date().toISOString()} | ${parseLogMessage(level, message)}`)
}
});

try {
await firehoseClient.send(command);
} catch (error) {
console.error('Error - couldn\'t put record:', error, error.stack);
}
};

module.exports = {
maybeCreateLogStream,
logToS3,
logToCloudwatch
};
3 changes: 2 additions & 1 deletion spot-electron/src/logger/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
logger: require('./logger'),
fileLogger: require('./fileLogger')
fileLogger: require('./fileLogger'),
awsLogger: require('./awsLogger')
};
30 changes: 30 additions & 0 deletions spot-electron/src/logger/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const os = require('os');

/**
* Log levels map.
*/
const LOG_LEVEL = {
0: 'TRACE',
1: 'DEBUG',
2: 'INFO',
3: 'LOG',
4: 'WARN',
5: 'ERROR'
};

module.exports = {
/**
* Builds the log message.
*/
parseLogMessage: (level, message) => `[${LOG_LEVEL[level]}] ${message}`,

/**
* Returns the host identifier.
*/
getHostId: () => {
const ipAddress = [].concat(...Object.values(os.networkInterfaces()))
saghul marked this conversation as resolved.
Show resolved Hide resolved
.find(networkInterface => !networkInterface.internal && networkInterface.family === 'IPv4')?.address;

return `${ipAddress}-${os.hostname()}`;
}
};