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

feat: suppress health check logs #328

Merged
merged 1 commit into from
Jul 4, 2022
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
6 changes: 4 additions & 2 deletions src/InboundServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const router = require('../lib/router');
const handlers = require('./handlers');
const middlewares = require('./middlewares');

const logExcludePaths = ['/'];

class InboundApi extends EventEmitter {
constructor(conf, logger, cache, validator, wso2) {
super({ captureExceptions: true });
Expand Down Expand Up @@ -99,7 +101,7 @@ class InboundApi extends EventEmitter {
api.use(middlewares.createJwsValidator(logger, jwsVerificationKeys, jwsExclusions));
}

api.use(middlewares.applyState({ cache, wso2, conf }));
api.use(middlewares.applyState({ cache, wso2, conf, logExcludePaths }));
api.use(middlewares.createLogger(logger));
api.use(middlewares.createRequestValidator(validator));
api.use(middlewares.assignFspiopIdentifier());
Expand Down Expand Up @@ -133,7 +135,7 @@ class InboundServer extends EventEmitter {
constructor(conf, logger, cache, wso2) {
super({ captureExceptions: true });
this._conf = conf;
this._validator = new Validate();
this._validator = new Validate({ logExcludePaths });
this._logger = logger;
this._api = new InboundApi(
conf,
Expand Down
16 changes: 12 additions & 4 deletions src/InboundServer/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,17 @@ const createLogger = (logger) => async (ctx, next) => {
path: ctx.path,
method: ctx.method
}});
ctx.state.logger.push({ body: ctx.request.body }).log('Request received');
if (!ctx.state.logExcludePaths.includes(ctx.path)) {
ctx.state.logger.push({body: ctx.request.body}).log('Request received');
}
try {
await next();
} catch (err) {
ctx.state.logger.push(err).log('Error');
}
await ctx.state.logger.log('Request processed');
if (!ctx.state.logExcludePaths.includes(ctx.path)) {
await ctx.state.logger.log('Request processed');
}
};


Expand All @@ -369,10 +373,14 @@ const createLogger = (logger) => async (ctx, next) => {
* @return {Function}
*/
const createRequestValidator = (validator) => async (ctx, next) => {
ctx.state.logger.log('Validating request');
if (!ctx.state.logExcludePaths.includes(ctx.path)) {
ctx.state.logger.log('Validating request');
}
try {
ctx.state.path = validator.validateRequest(ctx, ctx.state.logger);
ctx.state.logger.log('Request passed validation');
if (!ctx.state.logExcludePaths.includes(ctx.path)) {
ctx.state.logger.log('Request passed validation');
}
await next();
} catch (err) {
ctx.state.logger.push({ err }).log('Request failed validation.');
Expand Down
5 changes: 3 additions & 2 deletions src/OutboundServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const handlers = require('./handlers');
const middlewares = require('./middlewares');

const endpointRegex = /\/.*/g;
const logExcludePaths = ['/'];

class OutboundApi extends EventEmitter {
constructor(conf, logger, cache, validator, metricsClient, wso2) {
Expand All @@ -36,7 +37,7 @@ class OutboundApi extends EventEmitter {
this._api.use(middlewares.createErrorHandler(this._logger));
this._api.use(middlewares.createRequestIdGenerator());
this._api.use(koaBody()); // outbound always expects application/json
this._api.use(middlewares.applyState({ cache, wso2, conf, metricsClient }));
this._api.use(middlewares.applyState({ cache, wso2, conf, metricsClient, logExcludePaths }));
this._api.use(middlewares.createLogger(this._logger));

//Note that we strip off any path on peerEndpoint config after the origin.
Expand Down Expand Up @@ -69,7 +70,7 @@ class OutboundApi extends EventEmitter {
class OutboundServer extends EventEmitter {
constructor(conf, logger, cache, metricsClient, wso2) {
super({ captureExceptions: true });
this._validator = new Validate();
this._validator = new Validate({ logExcludePaths });
this._conf = conf;
this._logger = logger;
this._server = null;
Expand Down
8 changes: 6 additions & 2 deletions src/OutboundServer/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ const { applyState, createErrorHandler, createLogger, createRequestIdGenerator }
* @return {Function}
*/
const createRequestValidator = (validator) => async (ctx, next) => {
ctx.state.logger.log('Validating request');
if (!ctx.state.logExcludePaths.includes(ctx.path)) {
ctx.state.logger.log('Validating request');
}
try {
ctx.state.path = validator.validateRequest(ctx, ctx.state.logger);
ctx.state.logger.log('Request passed validation');
if (!ctx.state.logExcludePaths.includes(ctx.path)) {
ctx.state.logger.log('Request passed validation');
}
await next();
} catch (err) {
ctx.state.logger.push({ err }).log('Request failed validation.');
Expand Down
6 changes: 4 additions & 2 deletions src/TestServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const router = require('../lib/router');
const handlers = require('./handlers');
const middlewares = require('../InboundServer/middlewares');

const logExcludePaths = ['/'];

const getWsIp = (req) => [
req.socket.remoteAddress,
...(
Expand All @@ -36,7 +38,7 @@ class TestApi {

this._api.use(middlewares.createErrorHandler(logger));
this._api.use(middlewares.createRequestIdGenerator());
this._api.use(middlewares.applyState({ cache }));
this._api.use(middlewares.applyState({ cache, logExcludePaths }));
this._api.use(middlewares.createLogger(logger));

this._api.use(middlewares.createRequestValidator(validator));
Expand Down Expand Up @@ -174,7 +176,7 @@ class TestServer {
constructor({ port, logger, cache }) {
this._port = port;
this._logger = logger;
this._validator = new Validate();
this._validator = new Validate({ logExcludePaths });
this._api = new TestApi(this._logger.push({ component: 'api' }), this._validator, cache);
this._server = http.createServer(this._api.callback());
// TODO: why does this appear to need to be called after creating this._server (try reorder
Expand Down
2 changes: 0 additions & 2 deletions src/lib/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ class MetricsServer {

result.use(koaBody());
result.use(async ctx => {
this._logger.log('Metrics request received');

ctx.response.set('Content-Type', this._prometheusRegister.contentType);
ctx.response.body = await this._prometheusRegister.metrics();
});
Expand Down
4 changes: 3 additions & 1 deletion src/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ module.exports = (handlerMap) => async (ctx, next) => {
ctx.response.body = { statusCode: 404, message: 'Not found' };
}
else {
ctx.state.logger.push({ handler }).log('Found handler');
if (!ctx.state.logExcludePaths.includes(ctx.path)) {
ctx.state.logger.push({handler}).log('Found handler');
}
await handler(ctx);
}
await next();
Expand Down
11 changes: 10 additions & 1 deletion src/lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ const transformApiDoc = apiDoc => ({
});

class Validator {
/**
* @param {{logExcludePaths: string[]}} [opts]
*/

constructor(opts) {
this.logExcludePaths = opts?.logExcludePaths || [];
}
// apiDoc
// POJO representing apiDoc API spec. Example:
// const v = new Validator(require('./apiDoc.json'));
Expand Down Expand Up @@ -163,7 +170,9 @@ class Validator {
}
result.params = Object.assign({}, ...path.match(result.matcher.regex).slice(1).map((m, i) => ({ [result.matcher.params[i]]: m})));

logger.push({ path, result }).log('Matched path');
if (!this.logExcludePaths.includes(path)) {
logger.push({path, result}).log('Matched path');
}
return result;
}

Expand Down