vtlab-generic-functions 1.0.17 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,14 @@
1
1
  const { SQSClient, SendMessageCommand } = require('@aws-sdk/client-sqs');
2
2
  const { randomUUID } = require('crypto');
3
3
  const config = require('../config');
4
+ const s3bucket = require("../s3bucket");
5
+ const secretsManager = require("../config");
4
6
 
5
7
  if (!global.LOG_STREAM_NAME) {
6
8
  global.LOG_STREAM_NAME = `${process.env.AWS_LAMBDA_FUNCTION_NAME}-${process.env.AWS_LAMBDA_LOG_STREAM_NAME}-${randomUUID()}`;
7
9
  }
8
10
 
9
- function safeStringify(obj) {
11
+ const safeStringify = obj => {
10
12
  //this avoids circular references
11
13
  const cache = new Set();
12
14
  return JSON.stringify(obj, (key, value) => {
@@ -19,15 +21,39 @@ function safeStringify(obj) {
19
21
  }
20
22
  return value;
21
23
  });
22
- }
24
+ };
25
+
26
+ const generateTimestampWithRandomLetters = () => {
27
+ const getRandomLetter = () => {
28
+ const letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
29
+ return letters.charAt(Math.floor(Math.random() * letters.length));
30
+ };
31
+
32
+ const getRandomLetters = (count) => {
33
+ let result = '';
34
+ for (let i = 0; i < count; i++) {
35
+ result += getRandomLetter();
36
+ }
37
+ return result;
38
+ };
39
+
40
+ const timestamp = Math.floor(Date.now() / 1000);
41
+ const randomLetters = getRandomLetters(4);
42
+
43
+ return `${timestamp}${randomLetters}`;
44
+ };
23
45
 
24
46
  const putLogEvents = async (logEvents = [], logGroupName, logLevel = 2, logStreamName = null) => {
25
47
  if (logLevel === 0) {
26
48
  return;
27
49
  }
28
50
 
29
- if (!logGroupName) {
30
- throw new Error('logEvents and logGroupName are required');
51
+ if(!logStreamName){
52
+ logStreamName = global.LOG_STREAM_NAME;
53
+ }
54
+
55
+ if (!logGroupName || !logStreamName) {
56
+ throw new Error('logEvents and logGroupName and logStreamName are required');
31
57
  }
32
58
 
33
59
  let awsCredentials = await config.get('awsCredentials');
@@ -82,20 +108,40 @@ const putLogEvents = async (logEvents = [], logGroupName, logLevel = 2, logStrea
82
108
  }
83
109
 
84
110
  let cloudWatchLogEvents = [];
85
- let maxBytes = 200000;
111
+ //max 8kb log event
112
+ let maxBytes = 8*1024;
86
113
  for (let logEvent of logEvents) {
87
114
  if (logEvent.length > maxBytes) {
88
- for (let j = 0; j < logEvent.length; j += maxBytes) {
89
- let logEventChunk = logEvent.substring(j, j + maxBytes);
115
+ let bucketOptions = {
116
+ ACL: "private", ContentType: 'text/plain'
117
+ }
118
+ //get first 300 chars
119
+ const logFirst300Chars = logEvent.substring(0, 300);
120
+ try {
121
+ let groupFolder = logGroupName.replace(/\//g, '_');
122
+ let streamName = logStreamName.replace(/\//g, '_');
123
+ const path = `${process.env.NODE_ENV}/errors/${groupFolder}/${streamName}/${generateTimestampWithRandomLetters()}.txt`;
124
+ //upload logevent to s3
125
+ let bucketConfirmedPath = await s3bucket.uploadToS3(path, logEvent, bucketOptions, "s3LogsBucket");
126
+ console.log("bucketConfirmedPath", bucketConfirmedPath);
127
+ // get bucket name from s3 secrets
128
+ let bucket = await secretsManager.get('s3LogsBucket');
129
+ let signedURL = await s3bucket.generatePresignedUrl(bucket, path);
130
+
131
+ // Set event with the signed URL
132
+ cloudWatchLogEvents.push({
133
+ message: `${logLevel} ${logFirst300Chars}... Payload size too big. Read the full log here: ${path} ${signedURL}`,
134
+ timestamp: new Date().getTime()
135
+ });
136
+ } catch (e) {
90
137
  cloudWatchLogEvents.push({
91
- message: `${logLevel} ${logEventChunk}`,
138
+ message: `${logLevel} ${logFirst300Chars}... Payload size too big. Error uploading to S3: ${e.message}`,
92
139
  timestamp: new Date().getTime()
93
140
  });
94
141
  }
95
142
  } else {
96
143
  cloudWatchLogEvents.push({
97
- message: `${logLevel} ${logEvent}`,
98
- timestamp: new Date().getTime()
144
+ message: `${logLevel} ${logEvent}`, timestamp: new Date().getTime()
99
145
  });
100
146
  }
101
147
  }
@@ -113,7 +159,7 @@ const putLogEvents = async (logEvents = [], logGroupName, logLevel = 2, logStrea
113
159
  MessageBody: JSON.stringify({
114
160
  logEvents: [event],
115
161
  logGroupName: logGroupName,
116
- logStreamName: logStreamName || global.LOG_STREAM_NAME,
162
+ logStreamName: logStreamName,
117
163
  logLevel: logLevel
118
164
  }),
119
165
  QueueUrl
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtlab-generic-functions",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "scripts": {
5
5
  "test": "jest"
6
6
  },
package/rds/index.js CHANGED
@@ -9,8 +9,7 @@ const {getValueOrNull} = require('../utils/index');
9
9
  */
10
10
  const connectMyRDS = async (databaseName) => {
11
11
  try {
12
- let dbParams = await config.get(`rds`);
13
- dbParams = dbParams[databaseName];
12
+ const dbParams = await config.get(`databaseName.rds`);
14
13
 
15
14
  if (!dbParams) {
16
15
  throw new Error('Database name is not valid in the Secret Manager');