From 9587d8e160dc59fde3803ad553aac8c0bab7ce06 Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Fri, 27 Dec 2024 00:40:43 +0530 Subject: [PATCH 01/10] handled the production import data error --- src/libraries/requestTracing.ts | 36 +++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index bcb63841e0..6713545df0 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -2,7 +2,7 @@ import cls from "cls-hooked"; // No type defintions available for package 'cls-bluebird' // @ts-expect-error--ts-ignore import clsBluebird from "cls-bluebird"; -import { customAlphabet } from "nanoid"; +// import { customAlphabet } from "nanoid"; import type { NextFunction, Request, Response } from "express"; // Alphabets used in the custom nanoid function @@ -12,7 +12,7 @@ const alphabets = "0123456789abcdefghijklmnopqrstuvwxyz"; * Custom nanoid function to generate a unique 10 characters request ID * using the characters in the alphabets variable. */ -const nanoid = customAlphabet(alphabets, 10); +// const nanoid = customAlphabet(alphabets, 10); /** * Namespace for request tracing to maintain context across asynchronous operations. @@ -32,6 +32,10 @@ export const tracingIdHeaderName = "X-Tracing-Id"; */ const tracingIdContextKeyName = "tracingId"; +const getNanoid = async () => { + const { customAlphabet } = await import("nanoid"); + return customAlphabet(alphabets, 10); +}; /** * Sets the tracing ID in the namespace context. * @param tracingId - The tracing ID to set. @@ -59,14 +63,15 @@ export const middleware = () => { requestTracingNamespace.bindEmitter(req); requestTracingNamespace.bindEmitter(res); - const tracingId = req.header(tracingIdHeaderName) || nanoid(); - // We need to set header to ensure API gateway which proxies request, forwards the header as well - req.headers[tracingIdHeaderName] = tracingId; - res.header(tracingIdHeaderName, tracingId); // Adding tracing ID to response headers + getNanoid().then((nanoid) => { + const tracingId = req.header(tracingIdHeaderName) || nanoid(); + req.headers[tracingIdHeaderName] = tracingId; + res.header(tracingIdHeaderName, tracingId); - requestTracingNamespace.run(() => { - setTracingId(tracingId); - next(); + requestTracingNamespace.run(() => { + setTracingId(tracingId); + next(); + }); }); }; }; @@ -82,8 +87,13 @@ export const trace = async ( tracingId: string, method: () => T, ): Promise => { - await requestTracingNamespace.runAndReturn(() => { - setTracingId(tracingId || nanoid()); - return method(); - }); + try { + const nanoid = await getNanoid(); + await requestTracingNamespace.runAndReturn(() => { + setTracingId(tracingId || nanoid()); + return method(); + }); + } catch (error) { + throw error; + } }; From 9fa195551d99b6978c67b465f8a5cc124b496092 Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Fri, 27 Dec 2024 00:43:16 +0530 Subject: [PATCH 02/10] removed extra imports --- src/libraries/requestTracing.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index 6713545df0..a086b7050d 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -2,7 +2,6 @@ import cls from "cls-hooked"; // No type defintions available for package 'cls-bluebird' // @ts-expect-error--ts-ignore import clsBluebird from "cls-bluebird"; -// import { customAlphabet } from "nanoid"; import type { NextFunction, Request, Response } from "express"; // Alphabets used in the custom nanoid function @@ -12,7 +11,6 @@ const alphabets = "0123456789abcdefghijklmnopqrstuvwxyz"; * Custom nanoid function to generate a unique 10 characters request ID * using the characters in the alphabets variable. */ -// const nanoid = customAlphabet(alphabets, 10); /** * Namespace for request tracing to maintain context across asynchronous operations. From 7d2e2b53979bac9ec55a2ff528ba196f9832c7be Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Fri, 27 Dec 2024 01:04:00 +0530 Subject: [PATCH 03/10] removed try catch --- src/libraries/requestTracing.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index a086b7050d..b6742f9a87 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -85,13 +85,9 @@ export const trace = async ( tracingId: string, method: () => T, ): Promise => { - try { const nanoid = await getNanoid(); await requestTracingNamespace.runAndReturn(() => { setTracingId(tracingId || nanoid()); return method(); }); - } catch (error) { - throw error; - } }; From 1962c0195d121a6f21260c639b85ec3339f888fa Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Fri, 27 Dec 2024 01:08:30 +0530 Subject: [PATCH 04/10] removed added catch block to getNanoid fucntion --- src/libraries/requestTracing.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index b6742f9a87..f1e8b09d1f 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -3,6 +3,7 @@ import cls from "cls-hooked"; // @ts-expect-error--ts-ignore import clsBluebird from "cls-bluebird"; import type { NextFunction, Request, Response } from "express"; +import { error } from "console"; // Alphabets used in the custom nanoid function const alphabets = "0123456789abcdefghijklmnopqrstuvwxyz"; @@ -70,7 +71,10 @@ export const middleware = () => { setTracingId(tracingId); next(); }); - }); + }) + .catch((error)=>{ + next(error); + }) }; }; From 8f9652c694e7aed09975efa5be85191b5ad0dc5a Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Fri, 27 Dec 2024 01:12:31 +0530 Subject: [PATCH 05/10] format check --- src/libraries/requestTracing.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index f1e8b09d1f..06e40a31f4 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -3,7 +3,6 @@ import cls from "cls-hooked"; // @ts-expect-error--ts-ignore import clsBluebird from "cls-bluebird"; import type { NextFunction, Request, Response } from "express"; -import { error } from "console"; // Alphabets used in the custom nanoid function const alphabets = "0123456789abcdefghijklmnopqrstuvwxyz"; From 4f3979cf79e22c1cbbfdbf6a3ee452c5885a7f24 Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Mon, 30 Dec 2024 00:34:19 +0530 Subject: [PATCH 06/10] fixed the error of getNanoid --- src/libraries/requestTracing.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index 06e40a31f4..49bc610ef7 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -30,7 +30,7 @@ export const tracingIdHeaderName = "X-Tracing-Id"; */ const tracingIdContextKeyName = "tracingId"; -const getNanoid = async () => { +const getNanoid: () => Promise<() => string> = async () => { const { customAlphabet } = await import("nanoid"); return customAlphabet(alphabets, 10); }; From ef6625458232e00e27ca33f5b58844b4a6d3855f Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Mon, 30 Dec 2024 00:37:03 +0530 Subject: [PATCH 07/10] run prettier --- src/libraries/requestTracing.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index 49bc610ef7..73a70f1a8b 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -61,19 +61,20 @@ export const middleware = () => { requestTracingNamespace.bindEmitter(req); requestTracingNamespace.bindEmitter(res); - getNanoid().then((nanoid) => { - const tracingId = req.header(tracingIdHeaderName) || nanoid(); - req.headers[tracingIdHeaderName] = tracingId; - res.header(tracingIdHeaderName, tracingId); + getNanoid() + .then((nanoid) => { + const tracingId = req.header(tracingIdHeaderName) || nanoid(); + req.headers[tracingIdHeaderName] = tracingId; + res.header(tracingIdHeaderName, tracingId); - requestTracingNamespace.run(() => { - setTracingId(tracingId); - next(); + requestTracingNamespace.run(() => { + setTracingId(tracingId); + next(); + }); + }) + .catch((error) => { + next(error); }); - }) - .catch((error)=>{ - next(error); - }) }; }; @@ -88,9 +89,9 @@ export const trace = async ( tracingId: string, method: () => T, ): Promise => { - const nanoid = await getNanoid(); - await requestTracingNamespace.runAndReturn(() => { - setTracingId(tracingId || nanoid()); - return method(); - }); + const nanoid = await getNanoid(); + await requestTracingNamespace.runAndReturn(() => { + setTracingId(tracingId || nanoid()); + return method(); + }); }; From 754e398b1b26b729ce6e6ca7a087d84fb4f42c5e Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Tue, 31 Dec 2024 23:26:08 +0530 Subject: [PATCH 08/10] fixed the issue --- src/libraries/requestTracing.ts | 2 +- tests/libraries/requestTracing.spec.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index 73a70f1a8b..3e7205cc7d 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -60,7 +60,6 @@ export const middleware = () => { return (req: Request, res: Response, next: NextFunction): void => { requestTracingNamespace.bindEmitter(req); requestTracingNamespace.bindEmitter(res); - getNanoid() .then((nanoid) => { const tracingId = req.header(tracingIdHeaderName) || nanoid(); @@ -75,6 +74,7 @@ export const middleware = () => { .catch((error) => { next(error); }); + next(); }; }; diff --git a/tests/libraries/requestTracing.spec.ts b/tests/libraries/requestTracing.spec.ts index 460256143e..2bc20a8b95 100644 --- a/tests/libraries/requestTracing.spec.ts +++ b/tests/libraries/requestTracing.spec.ts @@ -27,7 +27,15 @@ describe("middleware -> requestContext", () => { context = requestTracingNamespace.createContext(); requestTracingNamespace.enter(context); }); - + it("test requestContext Middleware", () => { + middleware()( + mockRequest as Request, + mockResponse as Response, + nextFunction as NextFunction, + ); + expect(nextFunction).toBeCalledTimes(1); + }); + it("basic storing and retrieving the key value pair", () => { requestTracingNamespace.set(tracingIdContextKeyName, "value"); const response = requestTracingNamespace.get(tracingIdContextKeyName); From cd36bd5ee6d8e53420ccb4c3fa0ff64344690c65 Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Tue, 31 Dec 2024 23:47:35 +0530 Subject: [PATCH 09/10] format fix --- src/libraries/requestTracing.ts | 2 +- tests/libraries/requestTracing.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index 3e7205cc7d..e0f07d0d39 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -74,7 +74,7 @@ export const middleware = () => { .catch((error) => { next(error); }); - next(); + next(); }; }; diff --git a/tests/libraries/requestTracing.spec.ts b/tests/libraries/requestTracing.spec.ts index 2bc20a8b95..c82fb9aee2 100644 --- a/tests/libraries/requestTracing.spec.ts +++ b/tests/libraries/requestTracing.spec.ts @@ -35,7 +35,7 @@ describe("middleware -> requestContext", () => { ); expect(nextFunction).toBeCalledTimes(1); }); - + it("basic storing and retrieving the key value pair", () => { requestTracingNamespace.set(tracingIdContextKeyName, "value"); const response = requestTracingNamespace.get(tracingIdContextKeyName); From 00a9878d694f660ebde6f5028cc6417496d9652e Mon Sep 17 00:00:00 2001 From: Ramneet04 Date: Wed, 1 Jan 2025 14:45:17 +0530 Subject: [PATCH 10/10] remove dubplicate next() --- src/libraries/requestTracing.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/requestTracing.ts b/src/libraries/requestTracing.ts index e0f07d0d39..3b29cde34e 100644 --- a/src/libraries/requestTracing.ts +++ b/src/libraries/requestTracing.ts @@ -68,7 +68,6 @@ export const middleware = () => { requestTracingNamespace.run(() => { setTracingId(tracingId); - next(); }); }) .catch((error) => {