@skillswaveca/nova-shared-libraries 4.16.2 → 4.17.1
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/docs-base/package-info.js +5 -5
- package/package.json +1 -1
- package/packages/drivers/package.json +1 -1
- package/packages/nova-middleware/package.json +1 -1
- package/packages/nova-middleware/src/distributed-logging-middleware.js +36 -36
- package/packages/nova-model/package.json +1 -1
- package/packages/nova-router/package.json +1 -1
- package/packages/nova-utils/package.json +1 -1
- package/packages/nova-utils/src/logger.js +39 -18
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
export const packageInfo = [
|
|
2
2
|
{
|
|
3
3
|
"name": "@skillswaveca/nova-utils",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.17.0",
|
|
5
5
|
"description": "A collection of random utils used in nova repos",
|
|
6
6
|
"docsPath": "./nova-utils/index.html"
|
|
7
7
|
},
|
|
8
8
|
{
|
|
9
9
|
"name": "@skillswaveca/nova-router",
|
|
10
|
-
"version": "4.
|
|
10
|
+
"version": "4.17.0",
|
|
11
11
|
"description": "An extended Koa router that enables better validation",
|
|
12
12
|
"docsPath": "./nova-router/index.html"
|
|
13
13
|
},
|
|
14
14
|
{
|
|
15
15
|
"name": "@skillswaveca/nova-model",
|
|
16
|
-
"version": "4.
|
|
16
|
+
"version": "4.17.0",
|
|
17
17
|
"description": "Nova model stuff",
|
|
18
18
|
"docsPath": "./nova-model/index.html"
|
|
19
19
|
},
|
|
20
20
|
{
|
|
21
21
|
"name": "@skillswaveca/nova-middleware",
|
|
22
|
-
"version": "4.
|
|
22
|
+
"version": "4.17.0",
|
|
23
23
|
"description": "A collection of middleware used by nova projects",
|
|
24
24
|
"docsPath": "./nova-middleware/index.html"
|
|
25
25
|
},
|
|
26
26
|
{
|
|
27
27
|
"name": "@skillswaveca/nova-drivers",
|
|
28
|
-
"version": "4.
|
|
28
|
+
"version": "4.17.0",
|
|
29
29
|
"description": "Some helper drivers for AWS services",
|
|
30
30
|
"docsPath": "./drivers/index.html"
|
|
31
31
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@skillswaveca/nova-shared-libraries",
|
|
4
4
|
"description": "A monorepo of shared libraries for Nova projects.",
|
|
5
5
|
"repository": "https://github.com/SkillsWave/nova-shared-libraries",
|
|
6
|
-
"version": "4.
|
|
6
|
+
"version": "4.17.1",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"keywords": [],
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@skillswaveca/nova-drivers",
|
|
4
4
|
"description": "Some helper drivers for AWS services",
|
|
5
5
|
"repository": "https://github.com/SkillsWave/nova-shared-libraries",
|
|
6
|
-
"version": "4.
|
|
6
|
+
"version": "4.17.1",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"pre-release": "pnpm run create-index",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@skillswaveca/nova-middleware",
|
|
4
4
|
"description": "A collection of middleware used by nova projects",
|
|
5
5
|
"repository": "https://github.com/SkillsWave/nova-shared-libraries",
|
|
6
|
-
"version": "4.
|
|
6
|
+
"version": "4.17.1",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"pre-release": "pnpm run create-index",
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
|
|
3
|
-
import logger from '../../nova-utils/src/logger.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Koa middleware for log synchronization across multiple services.
|
|
7
|
-
* Provides a log context with `x-nova-request-id` as a default log value
|
|
8
|
-
* that can be used to sync logs across services. Also provides the property
|
|
9
|
-
* `context.novaRequestId` that can be used to get the current request id when
|
|
10
|
-
* trigger external services.
|
|
11
|
-
*
|
|
12
|
-
* If a request does not contain a header `x-nova-request-id` then a new
|
|
13
|
-
* uuid will be provided.
|
|
14
|
-
*
|
|
15
|
-
* @returns {(function(*, *): Promise<void>)|*}
|
|
16
|
-
*/
|
|
17
|
-
export const distributedLoggingMiddleware = () => {
|
|
18
|
-
return async(context, next) => {
|
|
19
|
-
if (!context.log) {
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const defaultLogger = context.log;
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} catch (error) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
await next();
|
|
35
|
-
};
|
|
36
|
-
};
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
|
|
3
|
+
import { log as logger } from '../../nova-utils/src/logger.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Koa middleware for log synchronization across multiple services.
|
|
7
|
+
* Provides a log context with `x-nova-request-id` as a default log value
|
|
8
|
+
* that can be used to sync logs across services. Also provides the property
|
|
9
|
+
* `context.novaRequestId` that can be used to get the current request id when
|
|
10
|
+
* trigger external services.
|
|
11
|
+
*
|
|
12
|
+
* If a request does not contain a header `x-nova-request-id` then a new
|
|
13
|
+
* uuid will be provided.
|
|
14
|
+
*
|
|
15
|
+
* @returns {(function(*, *): Promise<void>)|*}
|
|
16
|
+
*/
|
|
17
|
+
export const distributedLoggingMiddleware = () => {
|
|
18
|
+
return async(context, next) => {
|
|
19
|
+
if (!context.log) {
|
|
20
|
+
context.log = logger;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const defaultLogger = context.log;
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const novaRequestId = context.request.headers['x-nova-request-id'] || uuidv4();
|
|
27
|
+
context.log = context.log.child({ 'x-nova-request-id': novaRequestId });
|
|
28
|
+
context.novaRequestId = novaRequestId;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
context.log.error({ error: error.message }, 'Distributed Logging Middleware: Error');
|
|
31
|
+
context.log = defaultLogger;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await next();
|
|
35
|
+
};
|
|
36
|
+
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@skillswaveca/nova-model",
|
|
4
4
|
"description": "Nova model stuff",
|
|
5
5
|
"repository": "https://github.com/SkillsWave/nova-shared-libraries",
|
|
6
|
-
"version": "4.
|
|
6
|
+
"version": "4.17.1",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"pre-release": "pnpm run create-index",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@skillswaveca/nova-router",
|
|
4
4
|
"description": "An extended Koa router that enables better validation",
|
|
5
5
|
"repository": "https://github.com/SkillsWave/nova-shared-libraries",
|
|
6
|
-
"version": "4.
|
|
6
|
+
"version": "4.17.1",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"pre-release": "pnpm run create-index",
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@skillswaveca/nova-utils",
|
|
4
4
|
"description": "A collection of random utils used in nova repos",
|
|
5
5
|
"repository": "https://github.com/SkillsWave/nova-shared-libraries",
|
|
6
|
-
"version": "4.
|
|
6
|
+
"version": "4.17.1",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"pre-release": "pnpm run create-index",
|
|
@@ -1,26 +1,47 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
import pino from 'pino';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
4
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
'context.drivers',
|
|
10
|
-
'context.config',
|
|
11
|
-
'event.Records[*].dynamodb',
|
|
12
|
-
],
|
|
13
|
-
remove: true,
|
|
14
|
-
};
|
|
15
|
-
const serializers = pino.stdSerializers;
|
|
16
|
-
const logger = pino({ base, level, redact, serializers });
|
|
5
|
+
export const asyncContext = new AsyncLocalStorage();
|
|
6
|
+
// Create a base logger instance
|
|
7
|
+
const baseLogger = pino({
|
|
8
|
+
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
|
|
9
|
+
});
|
|
17
10
|
|
|
18
|
-
|
|
11
|
+
// addToContext function: adds a key/value pair to the current request's logger context.
|
|
12
|
+
export const addToContext = (key, value) => {
|
|
13
|
+
const store = asyncContext.getStore();
|
|
14
|
+
if (store) {
|
|
15
|
+
// Get the current child logger or fallback to the base logger.
|
|
16
|
+
const currentLogger = store.get('logger') || baseLogger;
|
|
17
|
+
// Create a new child logger with the additional key/value pair.
|
|
18
|
+
const updatedLogger = currentLogger.child({ [key]: value });
|
|
19
|
+
// Update the store so that all further logging in this async context will include the new field.
|
|
20
|
+
store.set('logger', updatedLogger);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
19
23
|
|
|
20
|
-
const
|
|
21
|
-
|
|
24
|
+
export const contextMiddleware = async(ctx, next) => {
|
|
25
|
+
const requestId = uuidv4();
|
|
26
|
+
const child = baseLogger.child({ requestId });
|
|
27
|
+
const store = new Map();
|
|
28
|
+
store.set('logger', child);
|
|
29
|
+
await asyncContext.run(store, async() => {
|
|
30
|
+
await next();
|
|
31
|
+
});
|
|
22
32
|
};
|
|
23
33
|
|
|
24
|
-
|
|
34
|
+
// Wrap the base logger with a Proxy that uses the child logger from AsyncLocalStorage if available.
|
|
35
|
+
export const log = new Proxy(baseLogger, {
|
|
36
|
+
get(target, property, receiver) {
|
|
37
|
+
const store = asyncContext.getStore();
|
|
38
|
+
if (store && store.get('logger')) {
|
|
39
|
+
target = store.get('logger');
|
|
40
|
+
}
|
|
41
|
+
return Reflect.get(target, property, receiver);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const logger = log;
|
|
25
46
|
|
|
26
|
-
export
|
|
47
|
+
export default log;
|