Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RequestID for each request #543

Merged
merged 4 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/gateway/context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const vm = require('vm');

function EgContextBase () {}

const uuid62 = require('uuid-base62');
function EgContextBase () {
this.requestID = uuid62.v4();
}
Object.defineProperty(EgContextBase.prototype, 'egContext', {
get () {
return this;
}
});
EgContextBase.prototype.evaluateAsTemplateString = function (expression) {
return this.run('`' + expression + '`', this);
};
Expand All @@ -10,8 +16,12 @@ EgContextBase.prototype.match = function (expression) {
// TODO: now it is Specifically converting to bool, but it may be needed to extend to support
// strings like 'yes', 'true',
};
EgContextBase.prototype.run = function (code) {
return vm.runInNewContext(code, { egContext: this, req: this.req, res: this.res });
EgContextBase.prototype.run = function (code, ctx) {
// Note: It must be some standard object like `this`
// Do not construct new object each time
// In that case it will have to build new contextified object
// So subsequent calls will take longer time then agains some already contextified object
return vm.runInNewContext(code, ctx || this);
};

module.exports = EgContextBase;
5 changes: 1 addition & 4 deletions lib/policies/log/log.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict';

const logger = require('./winston-logger');
const ConfigurationError = require('../../errors').ConfigurationError;

module.exports = function (params) {
if (!params || !params.message) {
throw new ConfigurationError('Log middleware requires "message" param');
throw new Error('Log middleware requires "message" param');
}

return function (req, res, next) {
Expand Down
24 changes: 23 additions & 1 deletion test/policies/log/log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const logger = require('../../../lib/policies/log/winston-logger');
const sinon = require('sinon');
const assert = require('assert');

describe('logging policy', () => {
describe('@log policy', () => {
const res = {
test: 'text'
};
Expand All @@ -30,6 +30,28 @@ describe('logging policy', () => {
assert.equal(logger.info.getCall(0).args[0], '/test GET text');
assert.ok(next.calledOnce);
});
it('should log requestID', () => {
const next = sinon.spy();
const logMiddleware = logPolicy({
// eslint-disable-next-line no-template-curly-in-string
message: '${requestID}'
});

logMiddleware(req, {}, next);
assert.ok(logger.info.getCall(0).args[0].length > 10);
assert.ok(next.calledOnce);
});
it('should log egContext.requestID', () => {
const next = sinon.spy();
const logMiddleware = logPolicy({
// eslint-disable-next-line no-template-curly-in-string
message: '${egContext.requestID}'
});

logMiddleware(req, {}, next);
assert.ok(logger.info.getCall(0).args[0].length > 10);
assert.ok(next.calledOnce);
});
it('should fail to access global context', () => {
const next = sinon.spy();
const logMiddleware = logPolicy({
Expand Down