Skip to content

Commit

Permalink
feat: improve logger, fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed Sep 2, 2024
1 parent 1589f17 commit a5d4842
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/lambda-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tracer = require("@lumigo/tracer")({

const { load } = require("./aws/aws-user-function.js");

const initLogger = require("./logger.js");
const initLogger = require("./lib/logger");
const logger = initLogger();

const getHandlerAsync = async () => {
Expand Down Expand Up @@ -60,7 +60,7 @@ const handler = async (event, context, callback) => {
}

const resultValue = await tracer.trace(userHandler)(event, context, callback);
logger.flushQueue();
await logger.flushQueue();
return resultValue;
};

Expand Down
6 changes: 6 additions & 0 deletions packages/lambda-layer/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
moduleNameMapper: {
"^uuid$": require.resolve("uuid"),
"^node:crypto$": require.resolve("node:crypto"),
},
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getTraceId } = require("@lumigo/tracer/dist/utils");
const { default: axios } = require("axios");
const { v4: uuid } = require("uuid");
const fetch = require("node-fetch");

const uuid = require("../utils/uuid");

/**
* Initialise log catcher and forwarder.
Expand All @@ -16,15 +17,14 @@ const initLogger = (externalLogger = undefined) => {
queue = [];

try {
await axios.post(
`https://${process.env.AUTO_TRACE_HOST}/api/spans`,
queueToSend,
{
headers: {
Authorization: "t_0000000000000000",
},
await fetch(`https://${process.env.AUTO_TRACE_HOST}/api/spans`, {
method: "POST",
body: JSON.stringify(queueToSend),
headers: {
"Content-Type": "application/json",
Authorization: "t_0000000000000000",
},
);
});
} catch (e) {
global.console._originalConsoleError(
`[Tracer] Failed sending log to remote server: ${e.message}`,
Expand All @@ -41,6 +41,7 @@ const initLogger = (externalLogger = undefined) => {
if (externalLogger) {
return externalLogger(type, args);
}

try {
const trace = getTraceId(process.env._X_AMZN_TRACE_ID);

Expand Down Expand Up @@ -70,9 +71,14 @@ const initLogger = (externalLogger = undefined) => {
}
};

const stop = () => {
global.console = global.console._originalConsole;
};

global.console = (function (console) {
return {
_originalConsoleError: (error) => console.error.apply(console, error),
_originalConsole: console,
_originalConsoleError: (error) => console.error.apply(console, [error]),
log: function () {
console.log.apply(console, arguments);
remoteLogger("log", arguments);
Expand All @@ -92,7 +98,7 @@ const initLogger = (externalLogger = undefined) => {
};
})(console);

return { flushQueue };
return { flushQueue, stop, queue };
};

module.exports = initLogger;
41 changes: 41 additions & 0 deletions packages/lambda-layer/lib/logger/logger.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
jest.mock("node-fetch");
const fetch = require("node-fetch");

const initLogger = require("./index.js");

describe("logger", () => {
let logger;

beforeAll(() => {
logger = initLogger();
});

beforeEach(() => {
// @ts-ignore
fetch.mockClear();
});

afterAll(() => {
logger.stop();
});

it("should send logs", async () => {
console.log("Hello, world!");

await logger.flushQueue();
expect(fetch).toHaveBeenCalledTimes(1);

// @ts-ignore
const body = JSON.parse(fetch.mock.calls[0][1].body);
expect(body).toHaveLength(1);
expect(body[0].log).toBe('{"0":"Hello, world!"}');
});

it("flushes logs when the queue is full", async () => {
for (let i = 0; i < 120; i++) {
console.log("Hello, world!");
}

expect(fetch).toHaveBeenCalledTimes(2);
});
});
25 changes: 25 additions & 0 deletions packages/lambda-layer/lib/utils/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// We use an alternative UUID implementation to prevent bundling issues.
// This one is less secure, but randomness is less of an issue for log IDs.

const str = () =>
(
"00000000000000000" + (Math.random() * 0xffffffffffffffff).toString(16)
).slice(-16);

const uuid = () => {
const a = str();
const b = str();
return (
a.slice(0, 8) +
"-" +
a.slice(8, 12) +
"-4" +
a.slice(13) +
"-a" +
b.slice(1, 4) +
"-" +
b.slice(4)
);
};

module.exports = uuid;
2 changes: 1 addition & 1 deletion packages/lambda-layer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dependencies": {
"@lumigo/tracer": "^1.99.3",
"axios": "^1.7.6",
"uuid": "^10.0.0"
"node-fetch": "^2"
},
"installConfig": {
"hoistingLimits": "workspaces"
Expand Down
13 changes: 2 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4850,7 +4850,7 @@ __metadata:
axios: "npm:^1.7.6"
esbuild: "npm:^0.23.1"
jest: "npm:^24.9.0"
uuid: "npm:^10.0.0"
node-fetch: "npm:^2"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -11557,7 +11557,7 @@ __metadata:
languageName: node
linkType: hard

"node-fetch@npm:^2.6.11, node-fetch@npm:^2.6.7, node-fetch@npm:^2.6.8":
"node-fetch@npm:^2, node-fetch@npm:^2.6.11, node-fetch@npm:^2.6.7, node-fetch@npm:^2.6.8":
version: 2.7.0
resolution: "node-fetch@npm:2.7.0"
dependencies:
Expand Down Expand Up @@ -15048,15 +15048,6 @@ __metadata:
languageName: node
linkType: hard

"uuid@npm:^10.0.0":
version: 10.0.0
resolution: "uuid@npm:10.0.0"
bin:
uuid: dist/bin/uuid
checksum: 10c0/eab18c27fe4ab9fb9709a5d5f40119b45f2ec8314f8d4cf12ce27e4c6f4ffa4a6321dc7db6c515068fa373c075b49691ba969f0010bf37f44c37ca40cd6bf7fe
languageName: node
linkType: hard

"uuid@npm:^3.3.2":
version: 3.4.0
resolution: "uuid@npm:3.4.0"
Expand Down

0 comments on commit a5d4842

Please sign in to comment.