cat-a-logs 2.0.7 → 2.0.9

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.
package/client/index.js CHANGED
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { Logger } from "@aws-lambda-powertools/logger";
11
11
  import { Ajv } from "ajv";
12
- //cache entries are structured thusly: 'Namespace + Dimensions(Alphabetically)': EMFObject
12
+ //cache entries are structured thusly: 'Namespace + DimensionsKeys(Alphabetically)': EMFObject
13
13
  const cache = {};
14
14
  //let latency = 300; (Example metric to track)
15
15
  //Example for in-line use of Cat-a-log w/maximum arguments: catalog(latency, "Latency" , "lambda-function-metrics", "Milliseconds", {'functionVersion': '$LATEST', 'Server': 'Prod'}, 60, deploy);
@@ -25,7 +25,7 @@ function catalog(trackedVariable_1, metricName_1) {
25
25
  for (let i = 0; i < yourKeys.length; i++) {
26
26
  if (badKeys.includes(yourKeys[i])) {
27
27
  //if a dimension name or metric name conflicts with native logger keys, throw error
28
- throw new Error("metricName, or Dimension names cannot be the same as these native logger keys: level || message || sampling_rate || service || timestamp || xray_trace_id");
28
+ throw new Error("metricName, or Dimension names CANNOT be the same as these native Logger keys: level || message || sampling_rate || service || timestamp || xray_trace_id");
29
29
  }
30
30
  }
31
31
  //EMF specification catch: if tracked variable is an array with a length greater than 100, throw error and do not log
@@ -34,11 +34,9 @@ function catalog(trackedVariable_1, metricName_1) {
34
34
  throw new Error("metric value cannot have more than 100 elements");
35
35
  }
36
36
  //EMF specification catch: make sure provided dimension object does not have more than 30 entries
37
- // if (Object.keys(CustomerDefinedDimension).length > 30) {
38
- // throw new Error(
39
- // "EMF has a limit of 30 user defined dimension keys per log"
40
- // );
41
- // }
37
+ if (Object.keys(CustomerDefinedDimension).length > 30) {
38
+ throw new Error("EMF has a limit of 30 user defined dimension keys per log");
39
+ }
42
40
  //Create new instance of Logger to use in function
43
41
  const logger = new Logger({ serviceName: "serverlessAirline" });
44
42
  //Set up Ajv instance for JSON validation
@@ -204,11 +202,27 @@ function catalog(trackedVariable_1, metricName_1) {
204
202
  logger.info(`Your EMF compliant Structured Metrics Log ${i + 1}`, cache[Object.keys(cache)[i]]);
205
203
  }
206
204
  //clear cache after logging all cached objects to Lambda
207
- console.log("BEFORE:", cache);
205
+ console.log("BEFORE:", JSON.stringify(cache, null, 2));
208
206
  for (var member in cache)
209
207
  delete cache[member];
210
208
  console.log("AFTER:", cache);
211
209
  }
212
210
  });
213
211
  }
214
- export { cache, catalog };
212
+ //If you want to manually deploy the cache without writing a catalog for it
213
+ function deployCatalog() {
214
+ return __awaiter(this, void 0, void 0, function* () {
215
+ //Create a new instance of Logger
216
+ const logger = new Logger({ serviceName: "serverlessAirline" });
217
+ //Log all cached objects to Cloudwatch through Lambda
218
+ for (let i = 0; i < Object.keys(cache).length; i++) {
219
+ logger.info(`Your EMF compliant Structured Metrics Log ${i + 1}`, cache[Object.keys(cache)[i]]);
220
+ }
221
+ //clear cache after logging all cached objects to Lambda
222
+ console.log("BEFORE:", JSON.stringify(cache, null, 2));
223
+ for (var member in cache)
224
+ delete cache[member];
225
+ console.log("AFTER:", cache);
226
+ });
227
+ }
228
+ export { cache, catalog, deployCatalog };
package/client/index.ts CHANGED
@@ -151,14 +151,16 @@ async function catalog(
151
151
  //usable instance of the validation JSON
152
152
  const validateEmf = ajv.compile(emfSchema);
153
153
  //sort customerDimensions key values in alphabetical order. We will use this to keep the keys in our cache consistant. Since the order of the dimensions do not change where the metrics are stored
154
- const sortedDimensions: { [key: string]: string } = {};
154
+ // const sortedDimensions: { [key: string]: string } = {}; // Doesn't need to be an object. Made it a string
155
+ let sortedDimensions: string = '';
156
+ const sortedCustomerDimensions: Array<string> = Object.keys(CustomerDefinedDimension).sort();
155
157
  for (
156
158
  let i = 0;
157
- i < Object.keys(CustomerDefinedDimension).sort().length;
159
+ i < sortedCustomerDimensions.length;
158
160
  i++
159
161
  ) {
160
- sortedDimensions[Object.keys(CustomerDefinedDimension).sort()[i]] =
161
- CustomerDefinedDimension[Object.keys(CustomerDefinedDimension).sort()[i]];
162
+ sortedDimensions +=
163
+ sortedCustomerDimensions[i];
162
164
  }
163
165
  //Check if Object with Namespace and Dimensions already exists in cache
164
166
  let check = cache[`${metricNamespace}${sortedDimensions}`];
@@ -183,7 +185,7 @@ async function catalog(
183
185
  CloudWatchMetrics: [
184
186
  {
185
187
  Namespace: metricNamespace,
186
- Dimensions: [Object.keys(sortedDimensions)],
188
+ Dimensions: sortedCustomerDimensions,
187
189
  Metrics: [
188
190
  {
189
191
  Name: metricName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cat-a-logs",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "description": "Create & send structured logs to AWS Cloudwatch logs",
5
5
  "main": "index.js",
6
6
  "exports": {