-
Notifications
You must be signed in to change notification settings - Fork 39
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
mihhu
wants to merge
1
commit into
master
Choose a base branch
from
cloud-logs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,735
−83
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}`; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?