Skip to content

Commit

Permalink
chore: access log, optional validity count
Browse files Browse the repository at this point in the history
  • Loading branch information
ppedziwiatr authored and Tadeuchi committed Apr 15, 2024
1 parent 6f02090 commit ab29692
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ const zlib = require('zlib');
const router = require('./router');
const { logConfig, config } = require('./config');
const { drePool } = require('./db/nodeDb');
const accessLogMiddleware = require('./routes/accessLogMiddleware');

const logger = require('./logger')('listener');
const accessLogger = require('./logger')('access');
const exitHook = require('async-exit-hook');
const { pgClient, warp } = require('./warp');
const { postEvalQueue, registerQueue, updateQueue } = require('./bullQueue');
Expand All @@ -21,6 +23,7 @@ async function runListener() {

const app = new Koa();
app
.use(accessLogMiddleware)
.use(corsConfig())
.use(compress(compressionSettings))
.use(bodyParser())
Expand All @@ -33,6 +36,7 @@ async function runListener() {
app.context.registerQueue = registerQueue;
app.context.updateQueue = updateQueue;
app.context.postEvalQueue = postEvalQueue;
app.context.accessLogger = accessLogger;

app.listen(port);
}
Expand Down
24 changes: 24 additions & 0 deletions src/routes/accessLogMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const util = require('node:util');

const LOG_FORMAT = '%s %s "%s %s HTTP/%s" %d %s %s[ms]';

module.exports = async (ctx, next) => {
const t0 = performance.now();
await next();
const t1 = performance.now();
try {
ctx.accessLogger.debug(util.format(
LOG_FORMAT,
ctx.ip,
ctx.method,
`${ctx.path}${ctx.search}`,
ctx.req.httpVersion,
ctx.status,
ctx.length ? ctx.length.toString() : '-',
(t1 - t0).toFixed(3)
)
);
} catch (err) {
console.error(err);
}
};
5 changes: 4 additions & 1 deletion src/routes/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = async (ctx) => {
const showValidity = ctx.query.validity === 'true';
const showErrorMessages = ctx.query.errorMessages === 'true';
const showErrors = ctx.query.errors === 'true';
const showValidityCount = ctx.query.validityCount === 'true';

logger.info("New 'contract' request", {
contractId,
Expand Down Expand Up @@ -64,7 +65,9 @@ module.exports = async (ctx) => {
page: parsedPage
};
}
response.validityCount = await getContractValidityTotalCount(contractId, result.sortKey);
if (showValidityCount) {
response.validityCount = await getContractValidityTotalCount(contractId, result.sortKey);
}
if (showErrorMessages) {
const contractErrorMessages = await getContractErrorMessages(contractId, result.sortKey, parsedLimit, offset);
response.errorMessages = contractErrorMessages.errorMessages;
Expand Down

0 comments on commit ab29692

Please sign in to comment.