Skip to content

Commit

Permalink
docs: example
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMuller committed Oct 18, 2024
1 parent d9aa5d8 commit 2fd4a12
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 34 deletions.
19 changes: 19 additions & 0 deletions example/helpers/maths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { logger } = require("../services/logger");
const { delay } = require("../services/helper");

const multiply = async (n, factor) => {
const sleepMs = Math.floor(Math.random() * 1000 * factor);

await delay(sleepMs);

const result = n * factor;

/**
* Log a single multiplication result with an additional userid value
*/
logger.debug("Multiply", { n, duration: sleepMs, result, userid: "mySecretUser" });

return result;
};

module.exports = { multiply };
3 changes: 3 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { main } = require("./multiply");

main({ factor: 2 });
30 changes: 0 additions & 30 deletions example/logger/logger-example.js

This file was deleted.

4 changes: 0 additions & 4 deletions example/logger/services/logger.js

This file was deleted.

76 changes: 76 additions & 0 deletions example/multiply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const { logger, metricUnits } = require("./services/logger.js");
const { multiply } = require("./helpers/maths.js");
const MAX_FACTOR = 10;
/**
*
* @typedef {Object} event
* @property {number} factor -- multiplication factor (optional)
* @property {string} correlationId -- An existing correlationId (optional)
* @param {*} context -- Lambda context
*/
const main = async (event, context) => {
try {
/**
* Set the correlation or create a new one
*/
logger.setCorrelationId(event.correlationId);

/**
* Log event, context and environment variables
*/
logger.logInputEvent({ event, context, env: process.env });

/**
* Set the handler name on all log outputs
*/
logger.addContextKey({
handlerNamespace: "multiply",
});

/**
* Add the factor to all future log outputs
*/
if (event.factor) {
logger.addContextKey({ factor: event.factor });
if (event.factor > MAX_FACTOR) {
const cause = { factor: event.factor, limit: 10, reason: "too big" };
/**
* Log the error with a payload
*/
logger.error("invalid factor", cause);
throw new RangeError("invalid factor", { cause });
}
}

const start = new Date().getTime();
const promises = [1, 2, 3, 4, 5].map((n) => multiply(n, event.factor || 1));

const result = await Promise.all(promises);
const end = new Date().getTime();

/**
* Log with additional attribute to mask
*/
logger.warn("result", { result }, {}, ["factor"]);

/**
* Create a metric for the duration per factor
*/
logger.metric("multiply", {
name: "Duration",
unit: metricUnits.Milliseconds,
value: end - start,
dimensions: [["factor", (event.factor || "1").toString()]],
});
} catch (error) {
/**
* Log the error with an error object
*/

logger.error("global error", error);
} finally {
logger.clearLogContext();
}
};

module.exports = { main };
5 changes: 5 additions & 0 deletions example/services/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

module.exports = { delay };
4 changes: 4 additions & 0 deletions example/services/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { Logger } = require("../../lib/cjs/index");
const logger = new Logger("myService", "myApp");
const metricUnits = Logger.METRIC_UNITS;
module.exports = { logger, metricUnits };

0 comments on commit 2fd4a12

Please sign in to comment.