@digitraffic/common 2023.3.21-2 → 2023.4.3-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/dist/aws/infra/api/handler-factory.d.ts +2 -2
- package/dist/aws/infra/api/integration.d.ts +2 -1
- package/dist/aws/infra/api/integration.js +14 -2
- package/dist/aws/infra/api/response.d.ts +12 -5
- package/dist/aws/infra/api/response.js +13 -12
- package/dist/aws/infra/api/responses.d.ts +3 -1
- package/dist/aws/infra/api/responses.js +9 -1
- package/dist/aws/infra/canaries/canary-parameters.d.ts +1 -1
- package/dist/aws/infra/canaries/url-checker.d.ts +2 -2
- package/dist/aws/infra/stack/lambda-configs.d.ts +3 -3
- package/dist/aws/infra/stack/monitoredfunction.js +33 -33
- package/dist/aws/runtime/dt-logger.d.ts +61 -28
- package/dist/aws/runtime/dt-logger.js +41 -18
- package/dist/aws/runtime/secrets/dbsecret.d.ts +2 -2
- package/dist/aws/runtime/secrets/secret.d.ts +1 -1
- package/dist/database/database.d.ts +2 -2
- package/dist/database/database.js +3 -4
- package/dist/test/asserter.d.ts +1 -1
- package/dist/test/httpserver.d.ts +1 -1
- package/dist/types/either.d.ts +1 -1
- package/dist/types/urn.d.ts +1 -1
- package/dist/utils/logging.d.ts +30 -3
- package/dist/utils/logging.js +44 -8
- package/package.json +30 -25
- package/src/aws/infra/api/integration.ts +26 -9
- package/src/aws/infra/api/response.ts +25 -31
- package/src/aws/infra/api/responses.ts +9 -2
- package/src/aws/runtime/dt-logger.ts +94 -35
- package/src/database/database.ts +4 -5
- package/src/utils/logging.ts +48 -14
package/dist/types/urn.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export
|
1
|
+
export type URN<Namespace extends string, NamespaceSpecificString extends string = ""> = `urn:${Namespace}:${NamespaceSpecificString}`;
|
package/dist/utils/logging.d.ts
CHANGED
@@ -1,4 +1,31 @@
|
|
1
|
-
import { AxiosError } from "axios";
|
2
1
|
import { DtLogger } from "../aws/runtime/dt-logger";
|
3
|
-
|
4
|
-
|
2
|
+
/**
|
3
|
+
* Curried version of logException.
|
4
|
+
*
|
5
|
+
* @example <caption>Using default configuration</caption>
|
6
|
+
* Promise.reject(x).catch(createExceptionLogger())
|
7
|
+
*
|
8
|
+
* @example <caption>Providing external logger and requiring stack</caption>
|
9
|
+
* import {logger} from "@digitraffic/common/dist/aws/runtime/dt-logger-default"
|
10
|
+
* Promise.reject(x).catch(createExceptionLogger(logger, true))
|
11
|
+
*
|
12
|
+
* @param [logger=undefined] - DtLogger to use. If not given, will create a new instance of DtLogger
|
13
|
+
* @param [includeStack=false] - Define if the stack trace should be logged.
|
14
|
+
* @param error - The error instance to be logged.
|
15
|
+
* @returns Logs the error without rethrowing.
|
16
|
+
* @see {@link logException}
|
17
|
+
*/
|
18
|
+
export declare function createExceptionLogger(logger?: DtLogger | undefined, includeStack?: boolean): (error: unknown) => void;
|
19
|
+
/**
|
20
|
+
* Log given exception with level ERROR to given logger.
|
21
|
+
*
|
22
|
+
* Supports AxiosError, Error and string
|
23
|
+
*
|
24
|
+
* @param logger - DtLogger to use
|
25
|
+
* @param error - AxiosError, Error or string to log
|
26
|
+
* @param [includeStack=true] - Include stack in the message, default false
|
27
|
+
* @returns Logs the error without rethrowing
|
28
|
+
* @see {@link DtLogger.log}
|
29
|
+
* @see {@link createExceptionLogger} for a curried setup
|
30
|
+
*/
|
31
|
+
export declare function logException(logger: DtLogger, error: unknown, includeStack?: boolean): void;
|
package/dist/utils/logging.js
CHANGED
@@ -1,28 +1,64 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.logException = void 0;
|
3
|
+
exports.logException = exports.createExceptionLogger = void 0;
|
4
4
|
const axios_1 = require("axios");
|
5
|
+
const dt_logger_1 = require("../aws/runtime/dt-logger");
|
5
6
|
const utils_1 = require("./utils");
|
6
7
|
const functionName = (0, utils_1.getEnvVariableOrElse)("AWS_LAMBDA_FUNCTION_NAME", "test");
|
8
|
+
/**
|
9
|
+
* Curried version of logException.
|
10
|
+
*
|
11
|
+
* @example <caption>Using default configuration</caption>
|
12
|
+
* Promise.reject(x).catch(createExceptionLogger())
|
13
|
+
*
|
14
|
+
* @example <caption>Providing external logger and requiring stack</caption>
|
15
|
+
* import {logger} from "@digitraffic/common/dist/aws/runtime/dt-logger-default"
|
16
|
+
* Promise.reject(x).catch(createExceptionLogger(logger, true))
|
17
|
+
*
|
18
|
+
* @param [logger=undefined] - DtLogger to use. If not given, will create a new instance of DtLogger
|
19
|
+
* @param [includeStack=false] - Define if the stack trace should be logged.
|
20
|
+
* @param error - The error instance to be logged.
|
21
|
+
* @returns Logs the error without rethrowing.
|
22
|
+
* @see {@link logException}
|
23
|
+
*/
|
24
|
+
function createExceptionLogger(logger = undefined, includeStack = false) {
|
25
|
+
let thatLogger;
|
26
|
+
if (logger) {
|
27
|
+
thatLogger = logger;
|
28
|
+
}
|
29
|
+
else {
|
30
|
+
thatLogger = new dt_logger_1.DtLogger();
|
31
|
+
}
|
32
|
+
return (error) => {
|
33
|
+
logException(thatLogger, error, includeStack);
|
34
|
+
};
|
35
|
+
}
|
36
|
+
exports.createExceptionLogger = createExceptionLogger;
|
7
37
|
/**
|
8
38
|
* Log given exception with level ERROR to given logger.
|
9
39
|
*
|
10
40
|
* Supports AxiosError, Error and string
|
11
41
|
*
|
12
|
-
* @param logger DtLogger to use
|
13
|
-
* @param error AxiosError, Error or string to log
|
14
|
-
* @param includeStack Include stack in the message, default false
|
15
|
-
* @
|
42
|
+
* @param logger - DtLogger to use
|
43
|
+
* @param error - AxiosError, Error or string to log
|
44
|
+
* @param [includeStack=true] - Include stack in the message, default false
|
45
|
+
* @returns Logs the error without rethrowing
|
46
|
+
* @see {@link DtLogger.log}
|
47
|
+
* @see {@link createExceptionLogger} for a curried setup
|
16
48
|
*/
|
17
49
|
function logException(logger, error, includeStack = false) {
|
18
|
-
const message = error instanceof Error
|
50
|
+
const message = error instanceof Error
|
51
|
+
? error.message
|
52
|
+
: typeof error === "string"
|
53
|
+
? error
|
54
|
+
: JSON.stringify(error);
|
19
55
|
const stack = error instanceof Error && includeStack ? error.stack : undefined;
|
20
|
-
const
|
56
|
+
const customCode = error instanceof axios_1.AxiosError ? error.code : undefined;
|
21
57
|
logger.error({
|
22
58
|
type: "Error",
|
23
59
|
method: `${functionName}.logException`,
|
24
60
|
message,
|
25
|
-
|
61
|
+
customCode,
|
26
62
|
stack,
|
27
63
|
});
|
28
64
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@digitraffic/common",
|
3
|
-
"version": "2023.3
|
3
|
+
"version": "2023.4.3-1",
|
4
4
|
"description": "",
|
5
5
|
"repository": {
|
6
6
|
"type": "git",
|
@@ -17,13 +17,13 @@
|
|
17
17
|
"src/**/*.ts"
|
18
18
|
],
|
19
19
|
"peerDependencies": {
|
20
|
-
"@aws-cdk/aws-synthetics-alpha": "2.
|
20
|
+
"@aws-cdk/aws-synthetics-alpha": "2.70.0-alpha.0",
|
21
21
|
"@types/geojson": "^7946.0.10",
|
22
|
-
"aws-cdk-lib": "2.
|
23
|
-
"aws-sdk": "^2.
|
24
|
-
"axios": "^1.
|
22
|
+
"aws-cdk-lib": "2.70.0",
|
23
|
+
"aws-sdk": "^2.1343.0",
|
24
|
+
"axios": "^1.3.4",
|
25
25
|
"change-case": "^4.1.2",
|
26
|
-
"constructs": "^10.1.
|
26
|
+
"constructs": "^10.1.292",
|
27
27
|
"geojson-validation": "^1.0.2",
|
28
28
|
"moment": "^2.29.4",
|
29
29
|
"node-ttl": "^0.2.0",
|
@@ -31,38 +31,39 @@
|
|
31
31
|
"pg-promise": "^10.12.0"
|
32
32
|
},
|
33
33
|
"devDependencies": {
|
34
|
-
"@aws-cdk/aws-synthetics-alpha": "2.
|
35
|
-
"@types/aws-lambda": "^8.10.
|
34
|
+
"@aws-cdk/aws-synthetics-alpha": "2.70.0-alpha.0",
|
35
|
+
"@types/aws-lambda": "^8.10.114",
|
36
36
|
"@types/geojson": "^7946.0.10",
|
37
|
-
"@types/jest": "^29.
|
38
|
-
"@types/
|
37
|
+
"@types/jest": "^29.5.0",
|
38
|
+
"@types/lodash": "^4.14.192",
|
39
|
+
"@types/node": "^18.15.10",
|
39
40
|
"@types/ramda": "^0.28.23",
|
40
41
|
"@types/sinon": "^10.0.13",
|
41
|
-
"@typescript-eslint/eslint-plugin": "
|
42
|
-
"@typescript-eslint/parser": "^5.
|
43
|
-
"aws-cdk-lib": "2.
|
44
|
-
"aws-sdk": "^2.
|
45
|
-
"axios": "^1.
|
42
|
+
"@typescript-eslint/eslint-plugin": "~5.56.0",
|
43
|
+
"@typescript-eslint/parser": "^5.56.0",
|
44
|
+
"aws-cdk-lib": "^2.70.0",
|
45
|
+
"aws-sdk": "^2.1343.0",
|
46
|
+
"axios": "^1.3.4",
|
46
47
|
"change-case": "^4.1.2",
|
47
|
-
"constructs": "^10.1.
|
48
|
-
"eslint": "~8.
|
49
|
-
"eslint-config-prettier": "^8.
|
50
|
-
"eslint-plugin-deprecation": "1.3.3",
|
48
|
+
"constructs": "^10.1.292",
|
49
|
+
"eslint": "~8.36.0",
|
50
|
+
"eslint-config-prettier": "^8.8.0",
|
51
|
+
"eslint-plugin-deprecation": "~1.3.3",
|
51
52
|
"geojson-validation": "^1.0.2",
|
52
53
|
"husky": "^8.0.3",
|
53
|
-
"jest": "^29.
|
54
|
+
"jest": "^29.5.0",
|
54
55
|
"jest-junit": "^15.0.0",
|
55
|
-
"lint-staged": "^13.
|
56
|
+
"lint-staged": "^13.2.0",
|
56
57
|
"moment": "^2.29.4",
|
57
58
|
"node-ttl": "^0.2.0",
|
58
59
|
"pg-native": "^3.0.1",
|
59
60
|
"pg-promise": "^10.12.0",
|
60
|
-
"prettier": "^2.8.
|
61
|
+
"prettier": "^2.8.7",
|
61
62
|
"ramda": "^0.28.0",
|
62
63
|
"rimraf": "^4.1.0",
|
63
|
-
"sinon": "^15.0.
|
64
|
+
"sinon": "^15.0.3",
|
64
65
|
"ts-jest": "^29.0.5",
|
65
|
-
"typescript": "~4.
|
66
|
+
"typescript": "~4.9.5"
|
66
67
|
},
|
67
68
|
"externals": [
|
68
69
|
"aws-sdk",
|
@@ -71,12 +72,16 @@
|
|
71
72
|
"lint-staged": {
|
72
73
|
"*.{js,ts,css,md,yml,yaml,json}": "prettier --write"
|
73
74
|
},
|
75
|
+
"dependencies": {
|
76
|
+
"lodash": "^4.17.21"
|
77
|
+
},
|
74
78
|
"scripts": {
|
75
79
|
"build": "tsc",
|
76
80
|
"lint": "eslint --cache .",
|
77
81
|
"eslint-report": "eslint . --format html",
|
78
82
|
"ci:eslint-report": "eslint . --format html -o report.html",
|
79
83
|
"clean": "rimraf dist output",
|
80
|
-
"test": "jest --detectOpenHandles --forceExit --coverage --coverageDirectory=output/coverage/jest"
|
84
|
+
"test": "jest --detectOpenHandles --forceExit --coverage --coverageDirectory=output/coverage/jest",
|
85
|
+
"test:watch": "jest --detectOpenHandles --forceExit --coverage --coverageDirectory=output/coverage/jest --watch"
|
81
86
|
}
|
82
87
|
}
|
@@ -7,7 +7,7 @@ import { IFunction } from "aws-cdk-lib/aws-lambda";
|
|
7
7
|
import { MediaType } from "../../types/mediatypes";
|
8
8
|
import { DigitrafficIntegrationResponse } from "../../runtime/digitraffic-integration-response";
|
9
9
|
|
10
|
-
type ParameterType = "path" | "querystring";
|
10
|
+
type ParameterType = "path" | "querystring" | "context";
|
11
11
|
|
12
12
|
interface ApiParameter {
|
13
13
|
type: ParameterType;
|
@@ -44,6 +44,14 @@ export class DigitrafficIntegration {
|
|
44
44
|
return this;
|
45
45
|
}
|
46
46
|
|
47
|
+
addContextParameter(...names: string[]): this {
|
48
|
+
names.forEach((name) =>
|
49
|
+
this.parameters.push({ type: "context", name })
|
50
|
+
);
|
51
|
+
|
52
|
+
return this;
|
53
|
+
}
|
54
|
+
|
47
55
|
build(): LambdaIntegration {
|
48
56
|
const integrationResponses = this.createResponses();
|
49
57
|
|
@@ -65,11 +73,14 @@ export class DigitrafficIntegration {
|
|
65
73
|
createRequestParameters(): Record<string, string> {
|
66
74
|
const requestParameters: Record<string, string> = {};
|
67
75
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
76
|
+
// filter out context parameters
|
77
|
+
this.parameters
|
78
|
+
.filter((parameter) => parameter.type !== "context")
|
79
|
+
.forEach((parameter: ApiParameter) => {
|
80
|
+
requestParameters[
|
81
|
+
`integration.request.${parameter.type}.${parameter.name}`
|
82
|
+
] = `method.request.${parameter.type}.${parameter.name}`;
|
83
|
+
});
|
73
84
|
|
74
85
|
return requestParameters;
|
75
86
|
}
|
@@ -78,9 +89,15 @@ export class DigitrafficIntegration {
|
|
78
89
|
const requestJson: Record<string, string> = {};
|
79
90
|
|
80
91
|
this.parameters.forEach((parameter: ApiParameter) => {
|
81
|
-
|
82
|
-
|
83
|
-
|
92
|
+
if (parameter.type === "context") {
|
93
|
+
requestJson[
|
94
|
+
parameter.name
|
95
|
+
] = `$util.parseJson($context.${parameter.name})`;
|
96
|
+
} else {
|
97
|
+
requestJson[
|
98
|
+
parameter.name
|
99
|
+
] = `$util.escapeJavaScript($input.params('${parameter.name}'))`;
|
100
|
+
}
|
84
101
|
});
|
85
102
|
|
86
103
|
return {
|
@@ -1,7 +1,13 @@
|
|
1
|
-
import {MediaType} from "../../types/mediatypes";
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
1
|
+
import { MediaType } from "../../types/mediatypes";
|
2
|
+
import {
|
3
|
+
JsonSchema,
|
4
|
+
JsonSchemaType,
|
5
|
+
JsonSchemaVersion,
|
6
|
+
MethodResponse,
|
7
|
+
Model,
|
8
|
+
} from "aws-cdk-lib/aws-apigateway";
|
9
|
+
import { IModel } from "aws-cdk-lib/aws-apigateway/lib/model";
|
10
|
+
import { dateFromIsoString } from "../../../utils/date-utils";
|
5
11
|
|
6
12
|
/**
|
7
13
|
* This is velocity-script, that assumes the response to be LambdaResponse(status and body).
|
@@ -37,7 +43,9 @@ $util.base64Decode($inputRoot.body)
|
|
37
43
|
|
38
44
|
export const getDeprecatedDefaultLambdaResponse = (sunset: string) => {
|
39
45
|
const setDeprecationHeaders = `#set ($context.responseOverride.header.Deprecation = 'true')
|
40
|
-
#set ($context.responseOverride.header.Sunset = '${dateFromIsoString(
|
46
|
+
#set ($context.responseOverride.header.Sunset = '${dateFromIsoString(
|
47
|
+
sunset
|
48
|
+
).toUTCString()}')`;
|
41
49
|
return RESPONSE_DEFAULT_LAMBDA.concat(setDeprecationHeaders);
|
42
50
|
};
|
43
51
|
|
@@ -75,35 +83,21 @@ const BadRequestMessage = "Bad request";
|
|
75
83
|
const BadRequestResponse = JSON.stringify({ message: BadRequestMessage });
|
76
84
|
|
77
85
|
/// @deprecated
|
78
|
-
export const BadRequestResponseTemplate =
|
79
|
-
MediaType.APPLICATION_JSON,
|
80
|
-
|
81
|
-
);
|
82
|
-
/// @deprecated
|
83
|
-
export const NotFoundResponseTemplate = createResponses(
|
84
|
-
MediaType.APPLICATION_JSON,
|
85
|
-
NotFoundResponse
|
86
|
-
);
|
86
|
+
export const BadRequestResponseTemplate = {
|
87
|
+
[MediaType.APPLICATION_JSON]: BadRequestResponse,
|
88
|
+
};
|
87
89
|
/// @deprecated
|
88
|
-
export const
|
89
|
-
MediaType.
|
90
|
-
|
91
|
-
);
|
90
|
+
export const NotFoundResponseTemplate = {
|
91
|
+
[MediaType.APPLICATION_JSON]: NotFoundResponse,
|
92
|
+
};
|
92
93
|
/// @deprecated
|
93
|
-
export const
|
94
|
-
MediaType.
|
95
|
-
|
96
|
-
);
|
97
|
-
|
94
|
+
export const XmlResponseTemplate = {
|
95
|
+
[MediaType.APPLICATION_XML]: BODY_FROM_INPUT_PATH,
|
96
|
+
};
|
98
97
|
/// @deprecated
|
99
|
-
export
|
100
|
-
|
101
|
-
|
102
|
-
): Record<string, T> {
|
103
|
-
return {
|
104
|
-
[key]: value,
|
105
|
-
};
|
106
|
-
}
|
98
|
+
export const InternalServerErrorResponseTemplate = {
|
99
|
+
[MediaType.APPLICATION_JSON]: InternalServerErrorResponse,
|
100
|
+
};
|
107
101
|
|
108
102
|
export class DigitrafficMethodResponse {
|
109
103
|
static response(
|
@@ -1,6 +1,5 @@
|
|
1
1
|
import {
|
2
2
|
InternalServerErrorResponseTemplate,
|
3
|
-
createResponses,
|
4
3
|
XmlResponseTemplate,
|
5
4
|
NotFoundResponseTemplate,
|
6
5
|
BadRequestResponseTemplate,
|
@@ -20,32 +19,38 @@ import {
|
|
20
19
|
} from "../../types/errors";
|
21
20
|
import { MediaType } from "../../types/mediatypes";
|
22
21
|
|
22
|
+
/// @deprecated
|
23
23
|
export const RESPONSE_200_OK: IntegrationResponse = {
|
24
24
|
statusCode: "200",
|
25
25
|
};
|
26
26
|
|
27
|
+
/// @deprecated
|
27
28
|
export const RESPONSE_400_BAD_REQUEST: IntegrationResponse = {
|
28
29
|
statusCode: "400",
|
29
30
|
selectionPattern: BAD_REQUEST_MESSAGE,
|
30
31
|
responseTemplates: BadRequestResponseTemplate,
|
31
32
|
};
|
32
33
|
|
34
|
+
/// @deprecated
|
33
35
|
export const RESPONSE_500_SERVER_ERROR: IntegrationResponse = {
|
34
36
|
statusCode: "500",
|
35
37
|
selectionPattern: ERROR_MESSAGE,
|
36
38
|
responseTemplates: InternalServerErrorResponseTemplate,
|
37
39
|
};
|
38
40
|
|
41
|
+
/// @deprecated
|
39
42
|
const RESPONSE_XML = {
|
40
43
|
responseTemplates: XmlResponseTemplate,
|
41
44
|
};
|
42
45
|
|
46
|
+
/// @deprecated
|
43
47
|
export const RESPONSE_CORS_INTEGRATION = {
|
44
48
|
responseParameters: {
|
45
49
|
"method.response.header.Access-Control-Allow-Origin": "'*'",
|
46
50
|
},
|
47
51
|
};
|
48
52
|
|
53
|
+
/// @deprecated
|
49
54
|
export const RESPONSE_404_NOT_FOUND = {
|
50
55
|
statusCode: "404",
|
51
56
|
selectionPattern: NOT_FOUND_MESSAGE,
|
@@ -63,7 +68,9 @@ export function methodResponse(
|
|
63
68
|
): MethodResponse {
|
64
69
|
return {
|
65
70
|
statusCode: status,
|
66
|
-
responseModels:
|
71
|
+
responseModels: {
|
72
|
+
[contentType]: model,
|
73
|
+
},
|
67
74
|
responseParameters: parameters ?? {},
|
68
75
|
};
|
69
76
|
}
|
@@ -1,37 +1,76 @@
|
|
1
1
|
import { Writable } from "stream";
|
2
|
+
import _ from "lodash";
|
2
3
|
|
3
|
-
|
4
|
+
/** Logging level */
|
5
|
+
export type LOG_LEVEL = "DEBUG" | "INFO" | "WARN" | "ERROR";
|
4
6
|
|
7
|
+
/**
|
8
|
+
* Configuration object for configuring the Digitraffic logging utility
|
9
|
+
* @see {@link DtLogger}
|
10
|
+
*/
|
5
11
|
export interface LoggerConfiguration {
|
12
|
+
/** Name of the lambda */
|
6
13
|
lambdaName?: string;
|
14
|
+
/** The file name where the logging occurs */
|
7
15
|
fileName?: string;
|
16
|
+
/** The lambda runtime environment */
|
8
17
|
runTime?: string;
|
18
|
+
/** Custom end point to write the logs to */
|
9
19
|
writeStream?: Writable;
|
10
20
|
}
|
11
21
|
|
12
|
-
|
13
|
-
|
22
|
+
interface LoggableTypeInternal extends LoggableType {
|
23
|
+
level: LOG_LEVEL;
|
24
|
+
}
|
25
|
+
|
26
|
+
/**
|
27
|
+
* CustomParams allows to add keys prefixed with `custom` keyword to be added to an
|
28
|
+
* object.
|
29
|
+
*/
|
30
|
+
export interface CustomParams {
|
31
|
+
/** do not log your apikey! */
|
32
|
+
customApikey?: never;
|
33
|
+
/** do not log your apikey! */
|
34
|
+
customApiKey?: never;
|
35
|
+
[key: `custom${Capitalize<string>}Count`]: number;
|
36
|
+
|
37
|
+
[key: `custom${Capitalize<string>}`]:
|
38
|
+
| string
|
39
|
+
| number
|
40
|
+
| boolean
|
41
|
+
| Date
|
42
|
+
| null
|
43
|
+
| undefined;
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Digitraffic logging object.
|
48
|
+
*
|
49
|
+
* `method` property is the only required propetry. {@link CustomParams} can be added by
|
50
|
+
* prefixin the property with keyword `custom`. The prefix is removed before writing to
|
51
|
+
* logging end point.
|
52
|
+
*
|
53
|
+
* @see {@link CustomParams}
|
54
|
+
*/
|
55
|
+
export interface LoggableType extends CustomParams {
|
56
|
+
/** Name of the method logging the message */
|
14
57
|
method: `${string}.${string}`;
|
15
|
-
/**
|
58
|
+
/** Message to log, optional */
|
16
59
|
message?: string;
|
17
|
-
/**
|
60
|
+
/** Type of message, optional */
|
18
61
|
type?: string;
|
19
|
-
/**
|
20
|
-
stack?: string;
|
21
|
-
/**
|
62
|
+
/** Stack trace, optional */
|
63
|
+
stack?: string | undefined;
|
64
|
+
/** Amount of time some operation took in milliseconds, optional */
|
22
65
|
tookMs?: number;
|
23
|
-
/**
|
24
|
-
|
25
|
-
/** do not log your apikey! */
|
26
|
-
apikey?: never;
|
27
|
-
/** do not log your apikey! */
|
28
|
-
apiKey?: never;
|
29
|
-
/** any other loggable key */
|
30
|
-
[key: string]: string | number | boolean | Date | undefined;
|
66
|
+
/** Pass error object, which will be stringified before logging */
|
67
|
+
error?: unknown;
|
31
68
|
}
|
32
69
|
|
33
70
|
/**
|
34
|
-
* Helper class for json-logging.
|
71
|
+
* Helper class for json-logging.
|
72
|
+
*
|
73
|
+
* Logged line will include:
|
35
74
|
* * log-level
|
36
75
|
* * lambdaName (taken from process environment)
|
37
76
|
* * runtime (taken from process environment)
|
@@ -39,15 +78,18 @@ export interface LoggableType {
|
|
39
78
|
*/
|
40
79
|
export class DtLogger {
|
41
80
|
readonly lambdaName?: string;
|
42
|
-
readonly fileName?: string;
|
43
81
|
readonly runtime?: string;
|
44
82
|
|
45
83
|
readonly writeStream: Writable;
|
46
84
|
|
85
|
+
/**
|
86
|
+
* Create a new Logger instance.
|
87
|
+
* @constructor
|
88
|
+
* @param {LoggerConfiguration?} [config] - Accepts configuration options @see {@link LoggerConfiguration}
|
89
|
+
*/
|
47
90
|
constructor(config?: LoggerConfiguration) {
|
48
91
|
this.lambdaName =
|
49
92
|
config?.lambdaName ?? process.env.AWS_LAMBDA_FUNCTION_NAME;
|
50
|
-
this.fileName = config?.fileName;
|
51
93
|
this.runtime = config?.runTime ?? process.env.AWS_EXECUTION_ENV;
|
52
94
|
this.writeStream = config?.writeStream ?? process.stdout;
|
53
95
|
}
|
@@ -56,13 +98,13 @@ export class DtLogger {
|
|
56
98
|
* Log given message with level DEBUG. This will not be forwarded to centralized logging system!.
|
57
99
|
*
|
58
100
|
* @param message anything
|
59
|
-
* @see
|
101
|
+
* @see {@link LoggableType}
|
102
|
+
* @see {@link DtLogger.log}
|
60
103
|
*/
|
61
104
|
debug(message: unknown): void {
|
62
105
|
const logMessage = {
|
63
106
|
message,
|
64
107
|
level: "DEBUG",
|
65
|
-
fileName: this.fileName,
|
66
108
|
lambdaName: this.lambdaName,
|
67
109
|
runtime: this.runtime,
|
68
110
|
};
|
@@ -74,46 +116,57 @@ export class DtLogger {
|
|
74
116
|
* Log given message with level INFO
|
75
117
|
*
|
76
118
|
* @param message Json-object to log
|
77
|
-
* @see
|
119
|
+
* @see {@link LoggableType}
|
120
|
+
* @see {@link DtLogger.log}
|
78
121
|
*/
|
79
122
|
info(message: LoggableType): void {
|
80
|
-
this.log("INFO"
|
123
|
+
this.log({ ...message, level: "INFO" });
|
81
124
|
}
|
82
125
|
|
83
126
|
/**
|
84
127
|
* Log given message with level WARN
|
85
128
|
*
|
86
129
|
* @param message Json-object to log
|
87
|
-
* @see
|
130
|
+
* @see {@link LoggableType}
|
131
|
+
* @see {@link DtLogger.log}
|
88
132
|
*/
|
89
133
|
warn(message: LoggableType): void {
|
90
|
-
this.log("WARN"
|
134
|
+
this.log({ ...message, level: "WARN" });
|
91
135
|
}
|
92
136
|
/**
|
93
137
|
* Log given message with level INFO
|
94
138
|
*
|
95
139
|
* @param message Json-object to log
|
96
|
-
* @see
|
140
|
+
* @see {@link LoggableType}
|
141
|
+
* @see {@link DtLogger.log}
|
97
142
|
*/
|
98
143
|
error(message: LoggableType): void {
|
99
|
-
this.log(
|
144
|
+
this.log({
|
145
|
+
...message,
|
146
|
+
level: "ERROR",
|
147
|
+
});
|
100
148
|
}
|
101
149
|
|
102
150
|
/**
|
103
|
-
* Log message with given log level.
|
151
|
+
* Log message with given log level.
|
152
|
+
*
|
104
153
|
* Some metadata is also added to the message:
|
105
154
|
* * runtime - can be configured with constructor or inferred from environment
|
106
155
|
* * lambdaName - can be configured with constructor or inferred from environment
|
107
|
-
* * fileName - can be configured with constructor
|
108
156
|
*
|
109
|
-
* @param
|
110
|
-
* @
|
157
|
+
* @param message Json-object to log
|
158
|
+
* @see {@link LoggableType}
|
111
159
|
*/
|
112
|
-
log(
|
160
|
+
private log(message: LoggableTypeInternal): void {
|
161
|
+
const error = message.error
|
162
|
+
? typeof message.error === "string"
|
163
|
+
? message.error
|
164
|
+
: JSON.stringify(message.error)
|
165
|
+
: undefined;
|
166
|
+
|
113
167
|
const logMessage = {
|
114
|
-
...message,
|
115
|
-
|
116
|
-
fileName: message.fileName ?? this.fileName,
|
168
|
+
...removePrefix("custom", message),
|
169
|
+
error,
|
117
170
|
lambdaName: this.lambdaName,
|
118
171
|
runtime: this.runtime,
|
119
172
|
};
|
@@ -121,3 +174,9 @@ export class DtLogger {
|
|
121
174
|
this.writeStream.write(JSON.stringify(logMessage) + "\n");
|
122
175
|
}
|
123
176
|
}
|
177
|
+
|
178
|
+
function removePrefix(prefix: string, loggable: LoggableType) {
|
179
|
+
return _.mapKeys(loggable, (_index, key: string) =>
|
180
|
+
key.startsWith(prefix) ? _.lowerFirst(key.replace(prefix, "")) : key
|
181
|
+
);
|
182
|
+
}
|
package/src/database/database.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import { IDatabase, ITask } from "pg-promise";
|
2
|
-
import { getEnvVariable } from "../utils/utils";
|
3
|
-
import { envValue } from "../aws/runtime/environment";
|
2
|
+
import { getEnvVariable, getEnvVariableOrElse } from "../utils/utils";
|
4
3
|
|
5
4
|
export enum DatabaseEnvironmentKeys {
|
6
5
|
DB_USER = "DB_USER",
|
@@ -78,13 +77,13 @@ async function doInDatabase<T>(
|
|
78
77
|
readonly: boolean,
|
79
78
|
fn: (db: DTDatabase) => Promise<T>
|
80
79
|
): Promise<T> {
|
81
|
-
const db_application =
|
80
|
+
const db_application = getEnvVariableOrElse(
|
82
81
|
DatabaseEnvironmentKeys.DB_APPLICATION,
|
83
82
|
"unknown-cdk-application"
|
84
83
|
);
|
85
84
|
const db_uri = readonly
|
86
|
-
?
|
87
|
-
:
|
85
|
+
? getEnvVariable(DatabaseEnvironmentKeys.DB_RO_URI)
|
86
|
+
: getEnvVariable(DatabaseEnvironmentKeys.DB_URI);
|
88
87
|
|
89
88
|
const db = initDbConnection(
|
90
89
|
getEnvVariable(DatabaseEnvironmentKeys.DB_USER),
|