@mimik/init 3.6.2 → 3.7.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/README.md +1 -0
- package/index.js +13 -5
- package/lib/metrics.js +28 -0
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@ The secOptions has the following structure:
|
|
|
23
23
|
exitOps: [function], // functions to process before exiting the micro-service
|
|
24
24
|
secOptions: { securityDefinition: function }, // extra security options to validate the API request
|
|
25
25
|
extractName: string, // ability to extract data from the req and send it to a logging environment
|
|
26
|
+
metrics: object, // to genetrate information for `/metrics`
|
|
26
27
|
}
|
|
27
28
|
```
|
|
28
29
|
The `return` object has the following strucuture:
|
package/index.js
CHANGED
|
@@ -13,9 +13,12 @@ const { getPublic } = require('@mimik/public-helper');
|
|
|
13
13
|
const { getCorrelationId } = require('@mimik/request-helper');
|
|
14
14
|
const { extractLogs } = require('./lib/logs');
|
|
15
15
|
const { sigProcess } = require('./lib/exit');
|
|
16
|
+
const { APIRequestMetrics, startHrTimeSet } = require('./lib/metrics');
|
|
16
17
|
|
|
17
18
|
const SIGINT = 'SIGINT';
|
|
18
19
|
const SIGTERM = 'SIGTERM';
|
|
20
|
+
const LOCAL = 'local';
|
|
21
|
+
const SET_ON = 'on';
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* @module ini
|
|
@@ -44,6 +47,7 @@ const SIGTERM = 'SIGTERM';
|
|
|
44
47
|
* exitOps: [function], // functions to process before exiting the micro-service
|
|
45
48
|
* secOptions: { securityDefinition: function }, // extra security options to validate the API request
|
|
46
49
|
* extractName: string, // ability to extract data from the req and send it to a logging environment
|
|
50
|
+
* metrics: object, // to genetrate information for `/metrics`
|
|
47
51
|
* }
|
|
48
52
|
*```
|
|
49
53
|
* The `return` object has the following strucuture:
|
|
@@ -104,10 +108,14 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
|
|
|
104
108
|
return swagger.getAPIFile(swaggerFilename, correlationIdStart)
|
|
105
109
|
.then((swaggerJson) => swaggerTools.initializeMiddleware(swaggerJson, (middleware) => {
|
|
106
110
|
serverSettings.basePath = swaggerJson.basePath;
|
|
107
|
-
app.use(
|
|
111
|
+
app.use(startHrTimeSet());
|
|
108
112
|
app.use(middleware.swaggerMetadata());
|
|
113
|
+
if (options.metrics) {
|
|
114
|
+
app.use(APIRequestMetrics(options.metrics.APIRequestDuration, serverSettings.basePath));
|
|
115
|
+
}
|
|
116
|
+
app.use(cors({ origin: '*' }));
|
|
109
117
|
app.use(middleware.swaggerValidator());
|
|
110
|
-
if ((config.nodeEnvironment && config.nodeEnvironment.toLowerCase() !==
|
|
118
|
+
if ((config.nodeEnvironment && config.nodeEnvironment.toLowerCase() !== LOCAL) || (serverSettings.securitySet === SET_ON)) {
|
|
111
119
|
const securityOptions = {
|
|
112
120
|
AdminSecurity: oauth.apiTokenAdminSecurityHelper,
|
|
113
121
|
SystemSecurity: oauth.apiTokenSecurityHelper,
|
|
@@ -123,7 +131,7 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
|
|
|
123
131
|
app.use(middleware.swaggerSecurity(securityOptions));
|
|
124
132
|
}
|
|
125
133
|
else logger.warn('security disabled: tokens will not be used and /me and /onbehalf will not work', correlationIdStart);
|
|
126
|
-
if ((config.nodeEnvironment && config.nodeEnvironment.toLowerCase() !==
|
|
134
|
+
if ((config.nodeEnvironment && config.nodeEnvironment.toLowerCase() !== LOCAL) || (config.registration.set === SET_ON)) {
|
|
127
135
|
registration = true;
|
|
128
136
|
}
|
|
129
137
|
else logger.warn('registration disabled: cluster will not work', correlationIdStart);
|
|
@@ -131,12 +139,12 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
|
|
|
131
139
|
app.use(extractLogs(config, extractName));
|
|
132
140
|
}
|
|
133
141
|
app.use(middleware.swaggerRouter({
|
|
134
|
-
useStubs: config.nodeEnvironment ===
|
|
142
|
+
useStubs: config.nodeEnvironment === LOCAL,
|
|
135
143
|
controllers: `${rootDir}/controllers`,
|
|
136
144
|
}));
|
|
137
145
|
app.use(middleware.swaggerUi());
|
|
138
146
|
app.use(swagger.sendError);
|
|
139
|
-
if (serverSettings.interceptError ===
|
|
147
|
+
if (serverSettings.interceptError === SET_ON) {
|
|
140
148
|
app.use(swagger.interceptError);
|
|
141
149
|
}
|
|
142
150
|
return Promise.each(validates, (validate) => validate())
|
package/lib/metrics.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const APIRequestMetrics = (APIRequestDuration, basePath) => (req, res, next) => {
|
|
2
|
+
let apiPath = req.originalUrl;
|
|
3
|
+
|
|
4
|
+
if (req.swagger) apiPath = req.swagger.apiPath;
|
|
5
|
+
const startHrTime = req.metrics ? req.metrics.startHrTime : process.hrtime();
|
|
6
|
+
|
|
7
|
+
res.on('finish', () => {
|
|
8
|
+
if (apiPath !== '/metrics' && apiPath !== '/healthcheck') {
|
|
9
|
+
const elapsedHrTime = process.hrtime(startHrTime);
|
|
10
|
+
const elapsedTimeInMs = elapsedHrTime[0] * 1000 + elapsedHrTime[1] / 1e6;
|
|
11
|
+
|
|
12
|
+
APIRequestDuration
|
|
13
|
+
.labels(req.method, `${basePath}${apiPath}`, req.originalUrl.includes('?'), res.statusCode)
|
|
14
|
+
.observe(elapsedTimeInMs);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
next();
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const startHrTimeSet = () => (req, res, next) => {
|
|
21
|
+
req.metrics = { startHrTime: process.hrtime() };
|
|
22
|
+
next();
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
module.exports = {
|
|
26
|
+
startHrTimeSet,
|
|
27
|
+
APIRequestMetrics,
|
|
28
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mimik/init",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.1",
|
|
4
4
|
"description": "Init process for micro-service",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,23 +29,23 @@
|
|
|
29
29
|
"url": "https://bitbucket.org/mimiktech/init"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@mimik/address-helper": "^1.6.
|
|
33
|
-
"@mimik/healthcheck": "^1.5.
|
|
34
|
-
"@mimik/oauth-helper": "^
|
|
35
|
-
"@mimik/public-helper": "^1.
|
|
36
|
-
"@mimik/request-helper": "^1.7.
|
|
37
|
-
"@mimik/response-helper": "^2.6.
|
|
38
|
-
"@mimik/sumologic-winston-logger": "^1.6.
|
|
39
|
-
"@mimik/swagger-helper": "^2.5.
|
|
32
|
+
"@mimik/address-helper": "^1.6.6",
|
|
33
|
+
"@mimik/healthcheck": "^1.5.10",
|
|
34
|
+
"@mimik/oauth-helper": "^2.0.2",
|
|
35
|
+
"@mimik/public-helper": "^1.6.1",
|
|
36
|
+
"@mimik/request-helper": "^1.7.8",
|
|
37
|
+
"@mimik/response-helper": "^2.6.3",
|
|
38
|
+
"@mimik/sumologic-winston-logger": "^1.6.14",
|
|
39
|
+
"@mimik/swagger-helper": "^2.5.6",
|
|
40
40
|
"bluebird": "3.7.2",
|
|
41
41
|
"cors": "2.8.5",
|
|
42
42
|
"helmet": "6.0.1",
|
|
43
43
|
"swagger-tools": "0.10.4"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@mimik/eslint-plugin-dependencies": "^2.4.
|
|
47
|
-
"@mimik/eslint-plugin-document-env": "^1.0.
|
|
48
|
-
"eslint": "8.
|
|
46
|
+
"@mimik/eslint-plugin-dependencies": "^2.4.5",
|
|
47
|
+
"@mimik/eslint-plugin-document-env": "^1.0.5",
|
|
48
|
+
"eslint": "8.30.0",
|
|
49
49
|
"eslint-config-airbnb": "19.0.4",
|
|
50
50
|
"eslint-plugin-import": "2.26.0",
|
|
51
51
|
"eslint-plugin-jsx-a11y": "6.6.1",
|