@comicrelief/lambda-wrapper 1.10.2 → 2.0.0-beta.10
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/README.md +196 -50
- package/dist/core/DependencyAwareClass.d.ts +16 -0
- package/dist/core/DependencyAwareClass.d.ts.map +1 -0
- package/dist/core/DependencyAwareClass.js +20 -0
- package/dist/core/DependencyAwareClass.js.map +1 -0
- package/dist/core/DependencyInjection.d.ts +62 -0
- package/dist/core/DependencyInjection.d.ts.map +1 -0
- package/dist/core/DependencyInjection.js +105 -0
- package/dist/core/DependencyInjection.js.map +1 -0
- package/dist/core/LambdaWrapper.d.ts +73 -0
- package/dist/core/LambdaWrapper.d.ts.map +1 -0
- package/dist/core/LambdaWrapper.js +170 -0
- package/dist/core/LambdaWrapper.js.map +1 -0
- package/dist/core/config.d.ts +20 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +21 -0
- package/dist/core/config.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -143
- package/dist/index.js.map +1 -0
- package/dist/models/ResponseModel.d.ts +73 -0
- package/dist/models/ResponseModel.d.ts.map +1 -0
- package/dist/models/ResponseModel.js +100 -0
- package/dist/models/ResponseModel.js.map +1 -0
- package/dist/models/SQSMessageModel.d.ts +46 -0
- package/dist/models/SQSMessageModel.d.ts.map +1 -0
- package/dist/models/SQSMessageModel.js +65 -0
- package/dist/models/SQSMessageModel.js.map +1 -0
- package/dist/services/BaseConfigService.d.ts +101 -0
- package/dist/services/BaseConfigService.d.ts.map +1 -0
- package/dist/services/BaseConfigService.js +180 -0
- package/dist/services/BaseConfigService.js.map +1 -0
- package/dist/services/HTTPService.d.ts +29 -0
- package/dist/services/HTTPService.d.ts.map +1 -0
- package/dist/services/HTTPService.js +55 -0
- package/dist/services/HTTPService.js.map +1 -0
- package/dist/services/LoggerService.d.ts +102 -0
- package/dist/services/LoggerService.d.ts.map +1 -0
- package/dist/services/LoggerService.js +249 -0
- package/dist/services/LoggerService.js.map +1 -0
- package/dist/services/RequestService.d.ts +119 -0
- package/dist/services/RequestService.d.ts.map +1 -0
- package/dist/services/RequestService.js +293 -0
- package/dist/services/RequestService.js.map +1 -0
- package/dist/services/SQSService.d.ts +263 -0
- package/dist/services/SQSService.d.ts.map +1 -0
- package/dist/services/SQSService.js +411 -0
- package/dist/services/SQSService.js.map +1 -0
- package/dist/services/TimerService.d.ts +22 -0
- package/dist/services/TimerService.d.ts.map +1 -0
- package/dist/services/TimerService.js +40 -0
- package/dist/services/TimerService.js.map +1 -0
- package/dist/types/Status.d.ts +6 -0
- package/dist/types/Status.d.ts.map +1 -0
- package/dist/types/Status.js +3 -0
- package/dist/types/Status.js.map +1 -0
- package/dist/utils/LambdaTermination.d.ts +14 -0
- package/dist/utils/LambdaTermination.d.ts.map +1 -0
- package/dist/utils/LambdaTermination.js +23 -0
- package/dist/utils/LambdaTermination.js.map +1 -0
- package/dist/utils/PromisifiedDelay.d.ts +17 -0
- package/dist/utils/PromisifiedDelay.d.ts.map +1 -0
- package/dist/utils/PromisifiedDelay.js +48 -0
- package/dist/utils/PromisifiedDelay.js.map +1 -0
- package/package.json +36 -26
- package/.eslintrc.yml +0 -16
- package/.github/dependabot.yml +0 -11
- package/.github/workflows/main.yml +0 -74
- package/babel.config.js +0 -13
- package/dist/Config/Dependencies.js +0 -33
- package/dist/DependencyInjection/DependencyAware.class.js +0 -40
- package/dist/DependencyInjection/DependencyInjection.class.js +0 -115
- package/dist/Model/CloudEvent.model.js +0 -138
- package/dist/Model/Model.model.js +0 -39
- package/dist/Model/Response.model.js +0 -135
- package/dist/Model/SQS/MarketingPreference.constraints.json +0 -28
- package/dist/Model/SQS/MarketingPreference.model.js +0 -583
- package/dist/Model/SQS/Message.model.js +0 -92
- package/dist/Model/Status.model.js +0 -71
- package/dist/Service/BaseConfig.service.js +0 -204
- package/dist/Service/HTTP.service.js +0 -57
- package/dist/Service/Logger.service.js +0 -256
- package/dist/Service/Request.service.js +0 -294
- package/dist/Service/SQS.service.js +0 -367
- package/dist/Service/Timer.service.js +0 -46
- package/dist/Wrapper/LambdaTermination.js +0 -34
- package/dist/Wrapper/LambdaWrapper.js +0 -143
- package/dist/Wrapper/PromisifiedDelay.js +0 -55
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.STATUS_TYPES = void 0;
|
|
7
|
-
var _Model = _interopRequireDefault(require("./Model.model"));
|
|
8
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
-
const STATUS_TYPES = {
|
|
10
|
-
OK: 'OK',
|
|
11
|
-
ACCEPTABLE_FAILURE: 'ACCEPTABLE_FAILURE',
|
|
12
|
-
APPLICATION_FAILURE: 'APPLICATION_FAILURE'
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* StatusModel Class
|
|
17
|
-
*/
|
|
18
|
-
exports.STATUS_TYPES = STATUS_TYPES;
|
|
19
|
-
class StatusModel extends _Model.default {
|
|
20
|
-
/**
|
|
21
|
-
* StatusModel constructor
|
|
22
|
-
*
|
|
23
|
-
* @param service
|
|
24
|
-
* @param status
|
|
25
|
-
*/
|
|
26
|
-
constructor(service, status) {
|
|
27
|
-
super();
|
|
28
|
-
this.setService(service);
|
|
29
|
-
this.setStatus(status);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Get Service
|
|
34
|
-
*
|
|
35
|
-
* @returns {*}
|
|
36
|
-
*/
|
|
37
|
-
getService() {
|
|
38
|
-
return this.service;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Set Service
|
|
43
|
-
*
|
|
44
|
-
* @param service
|
|
45
|
-
*/
|
|
46
|
-
setService(service) {
|
|
47
|
-
this.service = service;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Set the status
|
|
52
|
-
*
|
|
53
|
-
* @param status
|
|
54
|
-
*/
|
|
55
|
-
setStatus(status) {
|
|
56
|
-
if (typeof STATUS_TYPES[status] === 'undefined') {
|
|
57
|
-
throw new TypeError(`${StatusModel.name} - ${status} is not a valid status type`);
|
|
58
|
-
}
|
|
59
|
-
this.status = status;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Get status
|
|
64
|
-
*
|
|
65
|
-
* @returns {string|*}
|
|
66
|
-
*/
|
|
67
|
-
getStatus() {
|
|
68
|
-
return this.status;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.default = StatusModel;
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.ServiceStatesHttpCodes = exports.ServiceStates = exports.S3_NO_SUCH_KEY_ERROR_CODE = void 0;
|
|
7
|
-
var _awsSdk = require("aws-sdk");
|
|
8
|
-
var _DependencyAware = _interopRequireDefault(require("../DependencyInjection/DependencyAware.class"));
|
|
9
|
-
var _LambdaTermination = _interopRequireDefault(require("../Wrapper/LambdaTermination"));
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
/**
|
|
12
|
-
* Error.code for S3 404 errors
|
|
13
|
-
*/
|
|
14
|
-
const S3_NO_SUCH_KEY_ERROR_CODE = 'NoSuchKey';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Represents the service states
|
|
18
|
-
*/
|
|
19
|
-
exports.S3_NO_SUCH_KEY_ERROR_CODE = S3_NO_SUCH_KEY_ERROR_CODE;
|
|
20
|
-
const ServiceStates = {
|
|
21
|
-
OK: 'OK',
|
|
22
|
-
TEMPORARILY_PAUSED: 'TEMPORARILY_PAUSED',
|
|
23
|
-
INDEFINITELY_PAUSED: 'INDEFINITELY_PAUSED'
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Maps service states to HTTP codes
|
|
28
|
-
*/
|
|
29
|
-
exports.ServiceStates = ServiceStates;
|
|
30
|
-
const ServiceStatesHttpCodes = {
|
|
31
|
-
[ServiceStates.OK]: 200,
|
|
32
|
-
[ServiceStates.TEMPORARILY_PAUSED]: 409,
|
|
33
|
-
[ServiceStates.INDEFINITELY_PAUSED]: 409
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* BaseConfigService class
|
|
38
|
-
*
|
|
39
|
-
* This class is to be extended by the implementing services
|
|
40
|
-
* so that `defaultConfig` and possibly `s3Config` can be
|
|
41
|
-
* overriden / extended.
|
|
42
|
-
*/
|
|
43
|
-
exports.ServiceStatesHttpCodes = ServiceStatesHttpCodes;
|
|
44
|
-
class BaseConfigService extends _DependencyAware.default {
|
|
45
|
-
/**
|
|
46
|
-
* Returns the basic config.
|
|
47
|
-
* This getter is used to set the default config
|
|
48
|
-
* should the service not find any
|
|
49
|
-
* on the configured S3 Bucket.
|
|
50
|
-
*
|
|
51
|
-
* See `getOrCreate` and `patch` methods.
|
|
52
|
-
*/
|
|
53
|
-
static get defaultConfig() {
|
|
54
|
-
return {
|
|
55
|
-
state: ServiceStates.OK
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Returns the S3 configuration
|
|
61
|
-
* used to retrieve / update the
|
|
62
|
-
* service configuration.
|
|
63
|
-
*/
|
|
64
|
-
static get s3config() {
|
|
65
|
-
return {
|
|
66
|
-
Bucket: process.env.SERVICE_CONFIG_S3_BUCKET,
|
|
67
|
-
Key: process.env.SERVICE_CONFIG_S3_KEY
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Returns an S3 client
|
|
73
|
-
*
|
|
74
|
-
* @returns {S3}
|
|
75
|
-
*/
|
|
76
|
-
static get client() {
|
|
77
|
-
return new _awsSdk.S3({
|
|
78
|
-
region: process.env.REGION
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Returns an S3 client
|
|
84
|
-
*
|
|
85
|
-
* @returns {S3}
|
|
86
|
-
*/
|
|
87
|
-
get client() {
|
|
88
|
-
return this.constructor.client;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Deletes the configuration stored on S3.
|
|
93
|
-
* Helpful in feature tests.
|
|
94
|
-
*/
|
|
95
|
-
async delete() {
|
|
96
|
-
return this.client.deleteObject(this.constructor.s3config).promise();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Puts the given configuration on S3
|
|
101
|
-
*
|
|
102
|
-
* @param config
|
|
103
|
-
*/
|
|
104
|
-
async put(config) {
|
|
105
|
-
await this.client.putObject({
|
|
106
|
-
...this.constructor.s3config,
|
|
107
|
-
Body: JSON.stringify(config)
|
|
108
|
-
}).promise();
|
|
109
|
-
return config;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Gets the service configuration.
|
|
114
|
-
*/
|
|
115
|
-
async get() {
|
|
116
|
-
const response = await this.client.getObject(this.constructor.s3config).promise();
|
|
117
|
-
const body = String(response.Body);
|
|
118
|
-
if (!body) {
|
|
119
|
-
// Empty strings are not valid configurations
|
|
120
|
-
throw new Error('Configuration file is empty');
|
|
121
|
-
}
|
|
122
|
-
try {
|
|
123
|
-
return JSON.parse(body);
|
|
124
|
-
} catch {
|
|
125
|
-
throw new Error('Invalid configuration file');
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Gets or creates the service configuration.
|
|
131
|
-
*
|
|
132
|
-
* If the configuration is not found on S3
|
|
133
|
-
* the default configuration
|
|
134
|
-
* is uploaded and returned instead.
|
|
135
|
-
*/
|
|
136
|
-
async getOrCreate() {
|
|
137
|
-
try {
|
|
138
|
-
return await this.get();
|
|
139
|
-
} catch (error) {
|
|
140
|
-
if (error.code !== S3_NO_SUCH_KEY_ERROR_CODE) {
|
|
141
|
-
// Throw directly any other error
|
|
142
|
-
throw error;
|
|
143
|
-
}
|
|
144
|
-
return this.put(this.constructor.defaultConfig);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Patches the existing configuration
|
|
150
|
-
* or the default configuration
|
|
151
|
-
* with the provided partial configuration
|
|
152
|
-
*
|
|
153
|
-
* @param partialConfig
|
|
154
|
-
*/
|
|
155
|
-
async patch(partialConfig) {
|
|
156
|
-
let base = this.constructor.defaultConfig;
|
|
157
|
-
try {
|
|
158
|
-
base = await this.get();
|
|
159
|
-
} catch (error) {
|
|
160
|
-
if (error.code !== S3_NO_SUCH_KEY_ERROR_CODE) {
|
|
161
|
-
// Throw directly any other error
|
|
162
|
-
throw error;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Config doesn't exist
|
|
166
|
-
// keep using `this.constructor.defaultConfig`
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return this.put({
|
|
170
|
-
...base,
|
|
171
|
-
...partialConfig
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Performs a health check
|
|
177
|
-
* given the currentConfig.
|
|
178
|
-
*
|
|
179
|
-
* If currentConfig is not supplied
|
|
180
|
-
* it uses `getOrCreate` to fetch it.
|
|
181
|
-
*
|
|
182
|
-
* @param currentConfig
|
|
183
|
-
*/
|
|
184
|
-
async healthCheck(currentConfig = null) {
|
|
185
|
-
const config = currentConfig || (await this.getOrCreate());
|
|
186
|
-
return ServiceStatesHttpCodes[config.state] || 500;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Ensures that the application is healthy
|
|
191
|
-
* or throws a LambdaTermination
|
|
192
|
-
*
|
|
193
|
-
* @param currentConfig
|
|
194
|
-
*/
|
|
195
|
-
async ensureHealthy(currentConfig = null) {
|
|
196
|
-
const statusCode = await this.healthCheck(currentConfig);
|
|
197
|
-
if (statusCode < 400) {
|
|
198
|
-
return statusCode;
|
|
199
|
-
}
|
|
200
|
-
const message = 'Application is not healthy.';
|
|
201
|
-
throw new _LambdaTermination.default(message, statusCode, message, message);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
exports.default = BaseConfigService;
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.DEFAULT_HTTP_TIMEOUT = exports.COMICRELIEF_TEST_METADATA_HEADER = void 0;
|
|
7
|
-
var _axios = _interopRequireWildcard(require("axios"));
|
|
8
|
-
var _DependencyAware = _interopRequireDefault(require("../DependencyInjection/DependencyAware.class"));
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
12
|
-
const COMICRELIEF_TEST_METADATA_HEADER = 'x-comicrelief-test-metadata';
|
|
13
|
-
exports.COMICRELIEF_TEST_METADATA_HEADER = COMICRELIEF_TEST_METADATA_HEADER;
|
|
14
|
-
const DEFAULT_HTTP_TIMEOUT = 10 * 1000;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* HTTPService class
|
|
18
|
-
*/
|
|
19
|
-
exports.DEFAULT_HTTP_TIMEOUT = DEFAULT_HTTP_TIMEOUT;
|
|
20
|
-
class HTTPService extends _DependencyAware.default {
|
|
21
|
-
constructor(di) {
|
|
22
|
-
super(di);
|
|
23
|
-
this.config = {
|
|
24
|
-
timeout: DEFAULT_HTTP_TIMEOUT
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Sets the default timeout
|
|
30
|
-
*
|
|
31
|
-
* @param {number} ms
|
|
32
|
-
*/
|
|
33
|
-
setDefaultTimeout(ms) {
|
|
34
|
-
this.config.timeout = ms;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Performs and HTTP Request
|
|
39
|
-
*
|
|
40
|
-
* @param config
|
|
41
|
-
*/
|
|
42
|
-
async request(config) {
|
|
43
|
-
const mergedConfig = {
|
|
44
|
-
timeout: this.config.timeout,
|
|
45
|
-
headers: {},
|
|
46
|
-
...config
|
|
47
|
-
};
|
|
48
|
-
const lambdaRequest = this.getContainer().get(this.definitions.REQUEST);
|
|
49
|
-
const testMetadata = lambdaRequest.getHeader(COMICRELIEF_TEST_METADATA_HEADER);
|
|
50
|
-
if (testMetadata) {
|
|
51
|
-
mergedConfig.headers = mergedConfig.headers || {};
|
|
52
|
-
mergedConfig.headers[COMICRELIEF_TEST_METADATA_HEADER] = testMetadata;
|
|
53
|
-
}
|
|
54
|
-
return _axios.default.request(mergedConfig);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.default = HTTPService;
|
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
var Sentry = _interopRequireWildcard(require("@sentry/node"));
|
|
8
|
-
var _epsagon = _interopRequireDefault(require("epsagon"));
|
|
9
|
-
var _winston = _interopRequireDefault(require("winston"));
|
|
10
|
-
var _DependencyAware = _interopRequireDefault(require("../DependencyInjection/DependencyAware.class"));
|
|
11
|
-
var _DependencyInjection = _interopRequireDefault(require("../DependencyInjection/DependencyInjection.class"));
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
-
// Instantiate the sentry client
|
|
16
|
-
const sentryIsAvailable = typeof process.env.RAVEN_DSN !== 'undefined' && typeof process.env.RAVEN_DSN === 'string' && process.env.RAVEN_DSN !== 'undefined';
|
|
17
|
-
if (sentryIsAvailable) {
|
|
18
|
-
Sentry.init({
|
|
19
|
-
dsn: process.env.RAVEN_DSN,
|
|
20
|
-
shutdownTimeout: 5,
|
|
21
|
-
environment: process.env.STAGE
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* LoggerService class
|
|
27
|
-
*/
|
|
28
|
-
class LoggerService extends _DependencyAware.default {
|
|
29
|
-
constructor(di) {
|
|
30
|
-
super(di);
|
|
31
|
-
this.sentry = null;
|
|
32
|
-
this.winston = null;
|
|
33
|
-
const container = this.getContainer();
|
|
34
|
-
const event = container.getEvent();
|
|
35
|
-
const context = container.getContext();
|
|
36
|
-
|
|
37
|
-
// Set sentry client context
|
|
38
|
-
if (sentryIsAvailable && !container.isOffline) {
|
|
39
|
-
Sentry.configureScope(scope => {
|
|
40
|
-
scope.setTags({
|
|
41
|
-
Event: event,
|
|
42
|
-
Context: context
|
|
43
|
-
});
|
|
44
|
-
scope.setExtras({
|
|
45
|
-
lambda: context.functionName,
|
|
46
|
-
memory_size: context.memoryLimitInMB,
|
|
47
|
-
log_group: context.log_group_name,
|
|
48
|
-
log_stream: context.log_stream_name,
|
|
49
|
-
stage: process.env.STAGE,
|
|
50
|
-
path: event.path,
|
|
51
|
-
httpMethod: event.httpMethod
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
this.sentry = Sentry;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Returns a Winston logger object
|
|
60
|
-
* configured for our lambdas.
|
|
61
|
-
*
|
|
62
|
-
* Note:
|
|
63
|
-
*
|
|
64
|
-
* If the lambda is executed
|
|
65
|
-
* in a `serverless-offline` context
|
|
66
|
-
* the log output to console will be pretty printed.
|
|
67
|
-
*/
|
|
68
|
-
getLogger() {
|
|
69
|
-
const loggerFormats = [_winston.default.format.json({
|
|
70
|
-
replacer: (key, value) => {
|
|
71
|
-
if (value instanceof Buffer) {
|
|
72
|
-
return value.toString('base64');
|
|
73
|
-
}
|
|
74
|
-
if (value instanceof Error) {
|
|
75
|
-
const error = {};
|
|
76
|
-
Object.getOwnPropertyNames(value).forEach(objectKey => {
|
|
77
|
-
error[objectKey] = value[objectKey];
|
|
78
|
-
});
|
|
79
|
-
return error;
|
|
80
|
-
}
|
|
81
|
-
return value;
|
|
82
|
-
}
|
|
83
|
-
})];
|
|
84
|
-
if (this.getContainer().isOffline) {
|
|
85
|
-
loggerFormats.push(_winston.default.format.prettyPrint());
|
|
86
|
-
}
|
|
87
|
-
return _winston.default.createLogger({
|
|
88
|
-
level: 'info',
|
|
89
|
-
format: _winston.default.format.combine(...loggerFormats),
|
|
90
|
-
transports: [new _winston.default.transports.Console()]
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Returns the logger.
|
|
96
|
-
*
|
|
97
|
-
* Uses a cached `Winston` object
|
|
98
|
-
* if it has been already generated,
|
|
99
|
-
* otherwise it generates one.
|
|
100
|
-
*/
|
|
101
|
-
get logger() {
|
|
102
|
-
if (!this.winston) {
|
|
103
|
-
this.winston = this.getLogger();
|
|
104
|
-
}
|
|
105
|
-
return this.winston;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* While handling an error, lambda wrapper should
|
|
110
|
-
* recognise axios errors and trim down the information.
|
|
111
|
-
*
|
|
112
|
-
* Keep the following keys:
|
|
113
|
-
* - message.config
|
|
114
|
-
* - message.message
|
|
115
|
-
* - message.response?.status
|
|
116
|
-
* - message.response?.data
|
|
117
|
-
*
|
|
118
|
-
* @param {object} error
|
|
119
|
-
*/
|
|
120
|
-
static processAxiosError(error) {
|
|
121
|
-
const processed = {
|
|
122
|
-
config: error.config,
|
|
123
|
-
message: error.message
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
// It's pretty common for axios errors
|
|
127
|
-
// to not have.response e.g.when there's
|
|
128
|
-
// a network error or timeout.
|
|
129
|
-
// These errors will have .request but not .response.
|
|
130
|
-
if (error.response) {
|
|
131
|
-
processed.response = {
|
|
132
|
-
status: error.response.status,
|
|
133
|
-
data: error.response.data
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
return processed;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Transform the original message
|
|
141
|
-
* before it is passed to the winston logger
|
|
142
|
-
*
|
|
143
|
-
* @param {string|object} message
|
|
144
|
-
*/
|
|
145
|
-
processMessage(message = '') {
|
|
146
|
-
let processed = message;
|
|
147
|
-
if (processed && processed.isAxiosError) {
|
|
148
|
-
processed = this.constructor.processAxiosError(processed);
|
|
149
|
-
}
|
|
150
|
-
return processed;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Log Error Message
|
|
155
|
-
*
|
|
156
|
-
* @param error object
|
|
157
|
-
* @param message string
|
|
158
|
-
*/
|
|
159
|
-
error(error, message = '') {
|
|
160
|
-
if (sentryIsAvailable && error instanceof Error) {
|
|
161
|
-
Sentry.captureException(error);
|
|
162
|
-
}
|
|
163
|
-
if (typeof process.env.EPSAGON_TOKEN === 'string' && process.env.EPSAGON_TOKEN !== 'undefined' && typeof process.env.EPSAGON_SERVICE_NAME === 'string' && process.env.EPSAGON_SERVICE_NAME !== 'undefined' && error instanceof Error) {
|
|
164
|
-
_epsagon.default.setError(error);
|
|
165
|
-
}
|
|
166
|
-
this.logger.log('error', message, {
|
|
167
|
-
error: this.processMessage(error)
|
|
168
|
-
});
|
|
169
|
-
this.label('error', true);
|
|
170
|
-
this.metric('error', 'error', true);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Get sentry client
|
|
175
|
-
*
|
|
176
|
-
* @returns {null|*}
|
|
177
|
-
*/
|
|
178
|
-
getSentry() {
|
|
179
|
-
return this.sentry;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Log Information Message
|
|
184
|
-
*
|
|
185
|
-
* @param message string
|
|
186
|
-
*/
|
|
187
|
-
info(message) {
|
|
188
|
-
this.logger.log('info', this.processMessage(message));
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Logs an error, using `LoggerService.error`
|
|
193
|
-
* or `LoggerService.info` based on
|
|
194
|
-
* `process.env.LOGGER_SOFT_WARNING`.
|
|
195
|
-
*
|
|
196
|
-
* Please note that `LoggerService.error` and `LoggerService.info`
|
|
197
|
-
* have different signatures. The function uses the shared argument
|
|
198
|
-
* instead of introducing ambiguity.
|
|
199
|
-
*
|
|
200
|
-
* @param error
|
|
201
|
-
*/
|
|
202
|
-
warning(error) {
|
|
203
|
-
const softWarningValues = ['true', '1'];
|
|
204
|
-
if (softWarningValues.includes(process.env.LOGGER_SOFT_WARNING)) {
|
|
205
|
-
return this.info(error);
|
|
206
|
-
}
|
|
207
|
-
return this.error(error);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Add a label
|
|
212
|
-
*
|
|
213
|
-
* @param descriptor string
|
|
214
|
-
* @param silent boolean
|
|
215
|
-
*/
|
|
216
|
-
label(descriptor, silent = false) {
|
|
217
|
-
if (typeof process.env.EPSAGON_TOKEN === 'string' && process.env.EPSAGON_TOKEN !== 'undefined' && typeof process.env.EPSAGON_SERVICE_NAME === 'string' && process.env.EPSAGON_SERVICE_NAME !== 'undefined') {
|
|
218
|
-
_epsagon.default.label(descriptor, true);
|
|
219
|
-
}
|
|
220
|
-
if (silent === false) {
|
|
221
|
-
this.logger.log('info', `label - ${descriptor}`);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Add a metric
|
|
227
|
-
*
|
|
228
|
-
* @param descriptor string
|
|
229
|
-
* @param stat integer | string
|
|
230
|
-
* @param silent boolean
|
|
231
|
-
*/
|
|
232
|
-
metric(descriptor, stat, silent = false) {
|
|
233
|
-
if (typeof process.env.EPSAGON_TOKEN === 'string' && process.env.EPSAGON_TOKEN !== 'undefined' && typeof process.env.EPSAGON_SERVICE_NAME === 'string' && process.env.EPSAGON_SERVICE_NAME !== 'undefined') {
|
|
234
|
-
_epsagon.default.label(descriptor, stat);
|
|
235
|
-
}
|
|
236
|
-
if (silent === false) {
|
|
237
|
-
this.logger.log('info', `metric - ${descriptor} - ${stat}`);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Logs an object so that it can be inspected
|
|
243
|
-
*
|
|
244
|
-
* @param action - What are we doing with the object, i.e. 'Processing'
|
|
245
|
-
* @param object - The object to be stored in logs
|
|
246
|
-
* @param level - 'error', 'warning' or 'info'
|
|
247
|
-
*/
|
|
248
|
-
object(action, object, level = 'info') {
|
|
249
|
-
if (!['error', 'warning', 'info'].includes(level)) {
|
|
250
|
-
throw new Error('Unrecognised log level');
|
|
251
|
-
}
|
|
252
|
-
const payload = JSON.stringify(object, null, 4);
|
|
253
|
-
return this[level](`${action}: '${payload}'`);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
exports.default = LoggerService;
|