@mimik/init 3.7.4 → 3.8.0

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 CHANGED
@@ -32,7 +32,8 @@ The `return` object has the following strucuture:
32
32
  config: The configuration object,
33
33
  }
34
34
  ```
35
- For the preOps, postOps and exitOps the function are executed with 3 parameters (correlationId, config, server)
35
+ For the preOps, postOps and exitOps the function are executed with 3 parameters (correlationId, config, server).
36
+ The following routes are reserved: /healthcheck and /metrics
36
37
 
37
38
  | Param | Type | Description |
38
39
  | --- | --- | --- |
package/index.js CHANGED
@@ -9,16 +9,20 @@ const logger = require('@mimik/sumologic-winston-logger');
9
9
  const swagger = require('@mimik/swagger-helper');
10
10
  const oauthHelper = require('@mimik/oauth-helper');
11
11
  const { startupHealthInfo } = require('@mimik/healthcheck');
12
+ const { healthInfo } = require('@mimik/systeminfo');
12
13
  const { getPublic } = require('@mimik/public-helper');
13
14
  const { getCorrelationId } = require('@mimik/request-helper');
14
15
  const { extractLogs } = require('./lib/logs');
15
16
  const { sigProcess } = require('./lib/exit');
16
17
  const { APIRequestMetrics, startHrTimeSet } = require('./lib/metrics');
18
+ const { HEALTHCHECK_ROUTE, METRICS_ROUTE } = require('./lib/common');
17
19
 
18
20
  const SIGINT = 'SIGINT';
19
21
  const SIGTERM = 'SIGTERM';
20
22
  const LOCAL = 'local';
21
23
  const SET_ON = 'on';
24
+ const CONTENT_TYPE = 'Content-Type';
25
+ const JSON_CONTENT = 'application/json';
22
26
 
23
27
  /**
24
28
  * @module ini
@@ -56,7 +60,8 @@ const SET_ON = 'on';
56
60
  * config: The configuration object,
57
61
  * }
58
62
  * ```
59
- * For the preOps, postOps and exitOps the function are executed with 3 parameters (correlationId, config, server)
63
+ * For the preOps, postOps and exitOps the function are executed with 3 parameters (correlationId, config, server).
64
+ * The following routes are reserved: /healthcheck and /metrics
60
65
  */
61
66
  module.exports = (app, rootDir, config, validates, cluster, options) => {
62
67
  const fatalError = (error, correlationId) => {
@@ -94,6 +99,27 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
94
99
  'connect-src': ['\'self\'', '*.swagger.io'],
95
100
  },
96
101
  }));
102
+ app.use(cors({ origin: '*' }));
103
+ app.use(HEALTHCHECK_ROUTE, (req, res) => {
104
+ res.setHeader(CONTENT_TYPE, JSON_CONTENT);
105
+ res.end(JSON.stringify(healthInfo(), null, 2));
106
+ });
107
+ if (options.metrics && options.metrics.register) {
108
+ app.use(METRICS_ROUTE, (req, res) => options.metrics.register.metrics()
109
+ .then((result) => {
110
+ res.setHeader(CONTENT_TYPE, options.metrics.register.contentType);
111
+ res.end(result);
112
+ })
113
+ .catch((err) => {
114
+ res.setHeader(CONTENT_TYPE, JSON_CONTENT);
115
+ const errResponse = {
116
+ statusCode: 500,
117
+ title: http.STATUS_CODES[500],
118
+ message: err.message,
119
+ };
120
+ res.end(JSON.stringify(errResponse), null, 2);
121
+ }));
122
+ }
97
123
  if (options && options.extractName) {
98
124
  ({ extractName } = options);
99
125
  if (!dependencies || !dependencies.mLG) {
@@ -113,7 +139,6 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
113
139
  if (options && options.metrics) {
114
140
  app.use(APIRequestMetrics(options.metrics.APIRequestDuration, serverSettings.basePath));
115
141
  }
116
- app.use(cors({ origin: '*' }));
117
142
  app.use(middleware.swaggerValidator());
118
143
  if ((config.nodeEnvironment && config.nodeEnvironment.toLowerCase() !== LOCAL) || (serverSettings.securitySet === SET_ON)) {
119
144
  const securityOptions = {
package/lib/common.js ADDED
@@ -0,0 +1,7 @@
1
+ const METRICS_ROUTE = '/metrics';
2
+ const HEALTHCHECK_ROUTE = '/healthcheck';
3
+
4
+ module.exports = {
5
+ METRICS_ROUTE,
6
+ HEALTHCHECK_ROUTE,
7
+ };
package/lib/logs.js CHANGED
@@ -37,7 +37,10 @@ const extractLogs = (config, extractName, options) => (req, res, next) => {
37
37
  },
38
38
  };
39
39
 
40
- if (options && options.metrics) opts.metrics = options.metrics;
40
+ if (options && options.metrics) {
41
+ opts.metrics = options.metrics;
42
+ opts.metrics.url = url;
43
+ }
41
44
  rpAuth('mLG', opts)
42
45
  .catch((err) => getRichError('System', 'could not send extracted data', { url, extractName }, err, 'warn', correlationId));
43
46
  delete req.body[extractName];
package/lib/metrics.js CHANGED
@@ -1,3 +1,5 @@
1
+ const { HEALTHCHECK_ROUTE, METRICS_ROUTE } = require('./common');
2
+
1
3
  const APIRequestMetrics = (APIRequestDuration, basePath) => (req, res, next) => {
2
4
  let apiPath = req.originalUrl;
3
5
 
@@ -5,7 +7,7 @@ const APIRequestMetrics = (APIRequestDuration, basePath) => (req, res, next) =>
5
7
  const startHrTime = req.metrics ? req.metrics.startHrTime : process.hrtime();
6
8
 
7
9
  res.on('finish', () => {
8
- if (apiPath !== '/metrics' && apiPath !== '/healthcheck') {
10
+ if (apiPath !== METRICS_ROUTE && apiPath !== HEALTHCHECK_ROUTE) {
9
11
  const elapsedHrTime = process.hrtime(startHrTime);
10
12
  const elapsedTimeInMs = elapsedHrTime[0] * 1000 + elapsedHrTime[1] / 1e6;
11
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mimik/init",
3
- "version": "3.7.4",
3
+ "version": "3.8.0",
4
4
  "description": "Init process for micro-service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -36,7 +36,8 @@
36
36
  "@mimik/request-helper": "^1.7.8",
37
37
  "@mimik/response-helper": "^2.6.3",
38
38
  "@mimik/sumologic-winston-logger": "^1.6.14",
39
- "@mimik/swagger-helper": "^2.5.6",
39
+ "@mimik/swagger-helper": "^2.5.7",
40
+ "@mimik/systeminfo": "^2.3.11",
40
41
  "bluebird": "3.7.2",
41
42
  "cors": "2.8.5",
42
43
  "helmet": "6.0.1",
@@ -45,11 +46,11 @@
45
46
  "devDependencies": {
46
47
  "@mimik/eslint-plugin-dependencies": "^2.4.5",
47
48
  "@mimik/eslint-plugin-document-env": "^1.0.5",
48
- "eslint": "8.31.0",
49
+ "eslint": "8.33.0",
49
50
  "eslint-config-airbnb": "19.0.4",
50
- "eslint-plugin-import": "2.26.0",
51
- "eslint-plugin-jsx-a11y": "6.6.1",
52
- "eslint-plugin-react": "7.31.11",
51
+ "eslint-plugin-import": "2.27.5",
52
+ "eslint-plugin-jsx-a11y": "6.7.1",
53
+ "eslint-plugin-react": "7.32.2",
53
54
  "eslint-plugin-react-hooks": "4.6.0",
54
55
  "husky": "8.0.3",
55
56
  "jsdoc-to-markdown": "8.0.0"