Skip to content

Commit b1c00f6

Browse files
committed
🚧 Zwico
1 parent 45f414a commit b1c00f6

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

‎src/middlewares/extract-header.middleware.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { NextFunction, Response } from 'express';
22
import { BridgeRequest } from '../models';
3+
import { AsyncLocalStorage } from 'async_hooks';
34

45
const DEFAULT_LOCALE = 'de-DE';
56

7+
export const integrationIdStorage = new AsyncLocalStorage<string>();
8+
69
export function extractHeaderMiddleware(
710
req: BridgeRequest<any>,
811
res: Response,
@@ -20,5 +23,5 @@ export function extractHeaderMiddleware(
2023
locale,
2124
};
2225

23-
next();
26+
integrationIdStorage.run(userId, () => next());
2427
}

‎src/util/logger.util.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { anonymizeKey } from './anonymize-key';
21
import { context, trace } from '@opentelemetry/api';
32

3+
import { anonymizeKey } from './anonymize-key';
4+
import { integrationIdStorage } from '../middlewares';
5+
46
function addMessageToTraceSpan(
57
method: 'log' | 'error' | 'warn',
68
message: string,
@@ -20,75 +22,75 @@ function addMessageToTraceSpan(
2022
* @param source the context where the log originated from (usually the function name)
2123
* @param message the message of the log
2224
* @param apiKey the refreshToken
23-
* @param integrationUserId platypus user id
2425
* @param args additional data, will be stringified and appended
2526
*/
2627
export const infoLogger = (
2728
source: string,
2829
message: string,
2930
apiKey?: string,
30-
integrationUserId?: string,
3131
...args: unknown[]
3232
): void => {
33-
addMessageToTraceSpan('log', message, [...args, integrationUserId]);
33+
const userId = integrationIdStorage.getStore();
3434

35-
logger(console.info, source, message, apiKey, integrationUserId, ...args);
35+
addMessageToTraceSpan('log', message, [...args, userId]);
36+
37+
logger(console.info, source, message, apiKey, ...args);
3638
};
3739

3840
/**
3941
* Logging function equivalent to console.error
4042
* @param source the context where the log originated from (usually the function name)
4143
* @param message the message of the log
4244
* @param apiKey the refreshToken
43-
* @param integrationUserId platypus user id
4445
* @param args additional data, will be stringified and appended
4546
*/
4647
export const errorLogger = (
4748
source: string,
4849
message: string,
4950
apiKey?: string,
50-
integrationUserId?: string,
5151
...args: unknown[]
5252
): void => {
53-
addMessageToTraceSpan('error', message, [...args, integrationUserId]);
53+
const userId = integrationIdStorage.getStore();
54+
55+
addMessageToTraceSpan('error', message, [...args, userId]);
5456

55-
logger(console.error, source, message, apiKey, integrationUserId, ...args);
57+
logger(console.error, source, message, apiKey, ...args);
5658
};
5759

5860
/**
5961
* Logging function equivalent to console.warn
6062
* @param source the context where the log originated from (usually the function name)
6163
* @param message the message of the log
6264
* @param apiKey the refreshToken
63-
* @param integrationUserId platypus user id
6465
* @param args additional data, will be stringified and appended
6566
*/
6667
export const warnLogger = (
6768
source: string,
6869
message: string,
6970
apiKey?: string,
70-
integrationUserId?: string,
7171
...args: unknown[]
7272
): void => {
73-
addMessageToTraceSpan('warn', message, [...args, integrationUserId]);
73+
const userId = integrationIdStorage.getStore();
74+
75+
addMessageToTraceSpan('warn', message, [...args, userId]);
7476

75-
logger(console.warn, source, message, apiKey, integrationUserId, ...args);
77+
logger(console.warn, source, message, apiKey, ...args);
7678
};
7779

7880
const logger = (
7981
logFn: (message?: any, ...optionalParams: any[]) => void,
8082
source: string,
8183
message: string,
8284
apiKey: string | undefined,
83-
integrationUserId: string | undefined,
8485
...args: unknown[]
8586
): void => {
8687
// eslint-disable-next-line no-console
8788
const anonymizedApiKey = apiKey ? anonymizeKey(apiKey) : undefined;
89+
const userId = integrationIdStorage.getStore();
8890

8991
const formatedMessage = constructLogMessage(
9092
anonymizedApiKey ? `[${anonymizedApiKey}]` : undefined,
91-
integrationUserId ? `[${integrationUserId}]` : undefined,
93+
userId ? `[${userId}]` : undefined,
9294
`[${source}]`,
9395
message,
9496
);
@@ -99,7 +101,7 @@ const logger = (
99101
logFn(
100102
JSON.stringify({
101103
message: formatedMessage,
102-
data: { ...args, integrationUserId },
104+
data: { ...args, integrationUserId: userId },
103105
}),
104106
);
105107
}

0 commit comments

Comments
 (0)