From 2dce4c3caafa3ea58a060bdd8314c26411cbdd5f Mon Sep 17 00:00:00 2001 From: jonrobins Date: Thu, 15 Feb 2018 08:26:00 +0000 Subject: [PATCH 1/2] published 0.6.7 - Added request tracing --- README.md | 3 ++- example/index.js | 3 ++- index.js | 1 + lib/bootstrap.js | 30 ++++++++++++++++++------------ lib/middleware/request-tracing.js | 19 +++++++++++++++++++ package-lock.json | 15 ++++++++++++++- package.json | 5 +++-- 7 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 lib/middleware/request-tracing.js diff --git a/README.md b/README.md index 2ca782e..cbe00f1 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,10 @@ app.use(micro([options])); - `monitorsPath`: `String` Path to load monitors. Defaults to `monitors`. - `partialResponseQuery`: `String` The query parameter to use for partial reponse. Defaults to `fields`. - `correlationHeaderName`: `String` The name of your correlation header. Defaults to `X-CorrelationID`. -- `enableBodyParsing`: `boolean` Enable or disable body parsing, useful to disable when dealign with content other than JSON. Enables express-validator. Defaults to `true`. +- `enableBodyParsing`: `boolean` Enable or disable body parsing, useful to disable when dealing with content other than JSON. Enables express-validator. Defaults to `true`. - `validatorOptions`: `object` Enable express-validator with these options. Defaults to `null`. - `etag`: `boolean` Activate etag. +- `requestTracing`: `boolean` Enabled request log trace. Defaults to `false`. ## SWAGGER Integration diff --git a/example/index.js b/example/index.js index e7eed2e..44dc04d 100644 --- a/example/index.js +++ b/example/index.js @@ -12,7 +12,8 @@ app.use(micro({ discoverable: true, debug: true, controllersPath: './controllers', - monitorsPath: './monitors' + monitorsPath: './monitors', + requestTracing: true })); app.on('service:registered', function (data) { diff --git a/index.js b/index.js index 7dc8443..7d337db 100644 --- a/index.js +++ b/index.js @@ -59,6 +59,7 @@ var buildOptions = function(options) { options.enableBodyParsing = options.enableBodyParsing || true; options.validatorOptions = options.validatorOptions || null; options.etag = options.etag || false; + options.requestTracing = options.requestTracing || false; // Return now if we have no config if (!config.app) { diff --git a/lib/bootstrap.js b/lib/bootstrap.js index a3c990f..39b8b84 100644 --- a/lib/bootstrap.js +++ b/lib/bootstrap.js @@ -3,18 +3,19 @@ /* * Dependencies */ -var path = require('path'); -var fs = require('fs'); -var enrouten = require('express-enrouten'); -var bodyParser = require('body-parser'); -var cors = require('cors'); -var cache = require('express-cache-response-directive'); -var zoologist = require('./zoologist'); -var log = require('./logger'); -var partialRes = require('express-partial-response'); -var validator = require('express-validator'); -var correlate = require('./middleware/correlate'); -var swagger = require('./swagger'); +var path = require('path'); +var fs = require('fs'); +var enrouten = require('express-enrouten'); +var bodyParser = require('body-parser'); +var cors = require('cors'); +var cache = require('express-cache-response-directive'); +var zoologist = require('./zoologist'); +var log = require('./logger'); +var partialRes = require('express-partial-response'); +var validator = require('express-validator'); +var correlate = require('./middleware/correlate'); +var requestTracing = require('./middleware/request-tracing'); +var swagger = require('./swagger'); /** * Bootstrap the microservice. @@ -56,6 +57,11 @@ module.exports = function (app, options) { // Correlation app.use(correlate({ headerName: options.correlationHeaderName })); + // Enable request Tracing + if (options.requestTracing) { + app.use(requestTracing); + } + // Starter routes (e.g. actuator) app.use('/' + options.serviceName, enrouten({ directory: '../controllers' })); diff --git a/lib/middleware/request-tracing.js b/lib/middleware/request-tracing.js new file mode 100644 index 0000000..7caa8ad --- /dev/null +++ b/lib/middleware/request-tracing.js @@ -0,0 +1,19 @@ +const ip = require('request-ip'); + +/** + * Middleware to Log Request Detail + */ +module.exports = function(req, res, next) { + + var meta = { + request: { + client: {ip: ip.getClientIp(req)}, + method: req.method, + path: req.originalUrl, + headers: req.headers + } + }; + + req.log.info(meta, 'Request Trace'); + next(); +}; diff --git a/package-lock.json b/package-lock.json index abe5d05..b7ff2d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "express-microservice-starter", - "version": "0.6.3", + "version": "0.6.6", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1153,6 +1153,11 @@ "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", "dev": true }, + "is_js": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/is_js/-/is_js-0.9.0.tgz", + "integrity": "sha1-CrlFQFArp6+iTIVqqYVWFmnpxS0=" + }, "isarray": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", @@ -2042,6 +2047,14 @@ } } }, + "request-ip": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/request-ip/-/request-ip-2.0.2.tgz", + "integrity": "sha1-3urm1K8hdoSX24zQX6NxQ/jxJX4=", + "requires": { + "is_js": "0.9.0" + } + }, "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", diff --git a/package.json b/package.json index 54c936b..b507402 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "express-microservice-starter", - "version": "0.6.6", + "version": "0.6.7", "description": "An express-based Node.js API bootstrapping module for microservices.", "main": "index.js", "scripts": { - "test": "NODE_ENV=test ./node_modules/.bin/mocha 'test/**/*.test.js'", + "test": "NODE_ENV=test ./node_modules/.bin/mocha 'test/**/*.test.js'", "security": "node ./node_modules/nsp/bin/nsp check --output checkstyle" }, "repository": { @@ -40,6 +40,7 @@ "ip": "1.1.5", "konfig": "0.2.1", "optional": "0.1.4", + "request-ip": "2.0.2", "swagger-tools": "0.10.3", "uuid": "3.1.0", "vitalsigns": "0.4.3", From c79c20e0ad2356188e5283aa07d298bae0d7a539 Mon Sep 17 00:00:00 2001 From: jonrobins Date: Thu, 15 Feb 2018 08:33:32 +0000 Subject: [PATCH 2/2] published 0.6.7 - (updated) Added request tracing --- README.md | 6 +++--- example/index.js | 2 +- index.js | 8 +++++--- lib/bootstrap.js | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cbe00f1..510ae00 100644 --- a/README.md +++ b/README.md @@ -105,10 +105,10 @@ app.use(micro([options])); - `monitorsPath`: `String` Path to load monitors. Defaults to `monitors`. - `partialResponseQuery`: `String` The query parameter to use for partial reponse. Defaults to `fields`. - `correlationHeaderName`: `String` The name of your correlation header. Defaults to `X-CorrelationID`. -- `enableBodyParsing`: `boolean` Enable or disable body parsing, useful to disable when dealing with content other than JSON. Enables express-validator. Defaults to `true`. - `validatorOptions`: `object` Enable express-validator with these options. Defaults to `null`. -- `etag`: `boolean` Activate etag. -- `requestTracing`: `boolean` Enabled request log trace. Defaults to `false`. +- `enableBodyParsing`: `boolean` Enable or disable body parsing, useful to disable when dealing with content other than JSON. Enables express-validator. Defaults to `true`. +- `enableEtag`: `boolean` Activate etag. Defaults to `false`. +- `enableRequestTracing`: `boolean` Enabled request log trace. Defaults to `false`. ## SWAGGER Integration diff --git a/example/index.js b/example/index.js index 44dc04d..94d2d0c 100644 --- a/example/index.js +++ b/example/index.js @@ -13,7 +13,7 @@ app.use(micro({ debug: true, controllersPath: './controllers', monitorsPath: './monitors', - requestTracing: true + enableRequestTracing: true })); app.on('service:registered', function (data) { diff --git a/index.js b/index.js index 7d337db..d66ffa9 100644 --- a/index.js +++ b/index.js @@ -56,10 +56,12 @@ var buildOptions = function(options) { options.zookeeper = { connectionString: 'localhost:2181', retry: { count: 5 } }; options.partialResponseQuery = options.partialResponseQuery || 'fields'; options.correlationHeaderName = options.correlationHeaderName || 'X-CorrelationID'; - options.enableBodyParsing = options.enableBodyParsing || true; options.validatorOptions = options.validatorOptions || null; - options.etag = options.etag || false; - options.requestTracing = options.requestTracing || false; + + // Feature Flags + options.enableBodyParsing = options.enableBodyParsing || true; + options.enableEtag = options.enableEtag || false; + options.enableRequestTracing = options.enableRequestTracing || false; // Return now if we have no config if (!config.app) { diff --git a/lib/bootstrap.js b/lib/bootstrap.js index 39b8b84..f84fc99 100644 --- a/lib/bootstrap.js +++ b/lib/bootstrap.js @@ -58,7 +58,7 @@ module.exports = function (app, options) { app.use(correlate({ headerName: options.correlationHeaderName })); // Enable request Tracing - if (options.requestTracing) { + if (options.enableRequestTracing) { app.use(requestTracing); }