Skip to content

Commit

Permalink
chore(mojaloop/#3750): optimize logging (#467)
Browse files Browse the repository at this point in the history
* chore(mojaloop/#3750): optimize logging

* tests
  • Loading branch information
kleyow committed Feb 19, 2024
1 parent 065b65a commit abd88aa
Show file tree
Hide file tree
Showing 49 changed files with 616 additions and 568 deletions.
9 changes: 5 additions & 4 deletions modules/api-svc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@
"@mojaloop/central-services-shared": "18.2.0",
"@mojaloop/event-sdk": "^14.0.0",
"@mojaloop/sdk-scheme-adapter-private-shared-lib": "workspace:^",
"@mojaloop/sdk-standard-components": "v17.2.0",
"@mojaloop/sdk-standard-components": "v17.3.0",
"ajv": "8.12.0",
"axios": "^1.6.7",
"co-body": "^6.1.0",
"dotenv": "^16.4.3",
"dotenv": "^16.4.4",
"env-var": "^7.4.1",
"express": "^4.18.2",
"fast-json-patch": "^3.1.1",
"fast-safe-stringify": "^2.1.1",
"javascript-state-machine": "^3.1.0",
"js-yaml": "^4.1.0",
"json-schema-ref-parser": "^9.0.9",
Expand All @@ -103,10 +104,10 @@
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-jest": "^27.9.0",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"nock": "^13.5.1",
"nock": "^13.5.3",
"npm-check-updates": "^16.7.10",
"openapi-response-validator": "^12.1.3",
"openapi-typescript": "^6.7.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ module.exports.handleSDKOutboundBulkAcceptPartyInfoRequestedDmEvt = async (
currentState: BulkTransactionState.WAITING_FOR_PARTY_ACCEPTANCE,
});
} catch (err) {
logger.push({ err }).log('Error in handleSDKOutboundBulkAcceptPartyInfoRequested');
logger.push({ err }).error('Error in handleSDKOutboundBulkAcceptPartyInfoRequested');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ module.exports.handleSDKOutboundBulkAcceptQuoteRequestedDmEvt = async (
currentState: BulkTransactionState.WAITING_FOR_QUOTE_ACCEPTANCE,
});
} catch (err) {
logger.push({ err }).log('Error in handleSDKOutboundBulkAcceptQuoteRequested');
logger.push({ err }).error('Error in handleSDKOutboundBulkAcceptQuoteRequested');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ module.exports.handleSDKOutboundBulkResponsePreparedDmEvt = async (
});
await options.producer.sendDomainEvent(sdkOutboundBulkResponseSentDmEvt);
} catch (err) {
logger.push({ err }).log('Error in handleSDKOutboundBulkResponsePreparedDmEvt');
logger.push({ err }).error('Error in handleSDKOutboundBulkResponsePreparedDmEvt');
}
};
14 changes: 7 additions & 7 deletions modules/api-svc/src/ControlAgent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,22 @@ class Client extends ws {

async send(msg) {
const data = typeof msg === 'string' ? msg : serialise(msg);
this._logger.push({ data }).log('Sending message');
this._logger.push({ data }).debug('Sending message');
return new Promise((resolve) => super.send.call(this, data, resolve));
}

// Receive a single message
async receive() {
return new Promise((resolve) => this.once('message', (data) => {
const msg = deserialise(data);
this._logger.push({ msg }).log('Received');
this._logger.push({ msg }).debug('Received');
resolve(msg);
}));
}

// Close connection
async stop() {
this._logger.log('Control client shutting down...');
this._logger.info('Control client shutting down...');
this.close();
}

Expand All @@ -171,24 +171,24 @@ class Client extends ws {
try {
msg = deserialise(data);
} catch (err) {
this._logger.push({ data }).log('Couldn\'t parse received message');
this._logger.push({ data }).console.error();('Couldn\'t parse received message');
this.send(build.ERROR.NOTIFY.JSON_PARSE_ERROR());
}
this._logger.push({ msg }).log('Handling received message');
this._logger.push({ msg }).debug('Handling received message');
switch (msg.msg) {
case MESSAGE.CONFIGURATION:
switch (msg.verb) {
case VERB.NOTIFY: {
const dup = JSON.parse(JSON.stringify(this._appConfig)); // fast-json-patch explicitly mutates
_.merge(dup, msg.data);
this._logger.push({ oldConf: this._appConfig, newConf: dup }).log('Emitting new configuration');
this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this.emit(EVENT.RECONFIGURE, dup);
break;
}
case VERB.PATCH: {
const dup = JSON.parse(JSON.stringify(this._appConfig)); // fast-json-patch explicitly mutates
jsonPatch.applyPatch(dup, msg.data);
this._logger.push({ oldConf: this._appConfig, newConf: dup }).log('Emitting new configuration');
this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this.emit(EVENT.RECONFIGURE, dup);
break;
}
Expand Down
24 changes: 12 additions & 12 deletions modules/api-svc/src/ControlServer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ class Client extends ws {

async send(msg) {
const data = typeof msg === 'string' ? msg : serialise(msg);
this._logger.push({ data }).log('Sending message');
this._logger.push({ data }).debug('Sending message');
return new Promise((resolve) => super.send.call(this, data, resolve));
}

// Receive a single message
async receive() {
return new Promise((resolve) => this.once('message', (data) => {
const msg = deserialise(data);
this._logger.push({ msg }).log('Received');
this._logger.push({ msg }).debug('Received');
resolve(msg);
}));
}
Expand Down Expand Up @@ -187,7 +187,7 @@ class Server extends ws.Server {

this.on('error', err => {
this._logger.push({ err })
.log('Unhandled websocket error occurred. Shutting down.');
.error('Unhandled websocket error occurred. Shutting down.');
process.exit(1);
});

Expand All @@ -197,18 +197,18 @@ class Server extends ws.Server {
ip: getWsIp(req),
remoteAddress: req.socket.remoteAddress,
});
logger.log('Websocket connection received');
logger.info('Websocket connection received');
this._clientData.set(socket, { ip: req.connection.remoteAddress, logger });

socket.on('close', (code, reason) => {
logger.push({ code, reason }).log('Websocket connection closed');
logger.push({ code, reason }).info('Websocket connection closed');
this._clientData.delete(socket);
});

socket.on('message', this._handle(socket, logger));
});

this._logger.push(this.address()).log('running on');
this._logger.push(this.address()).info('running on');
}

// Close the server then wait for all the client sockets to close
Expand All @@ -218,7 +218,7 @@ class Server extends ws.Server {
client.terminate();
}
await closing;
this._logger.log('Control server shutdown complete');
this._logger.info('Control server shutdown complete');
}


Expand All @@ -227,7 +227,7 @@ class Server extends ws.Server {
const logError = (socket, message) => (err) =>
this._logger
.push({ message, ip: this._clientData.get(socket).ip, err })
.log('Error sending reconfigure notification to client');
.error('Error sending reconfigure notification to client');
const sendToAllClients = (msg) => Promise.all(
[...this.clients.values()].map((socket) =>
(new Promise((resolve) => socket.send(msg, resolve))).catch(logError(socket, msg))
Expand All @@ -244,10 +244,10 @@ class Server extends ws.Server {
try {
msg = deserialise(data);
} catch (err) {
logger.push({ data }).log('Couldn\'t parse received message');
logger.push({ data }).error('Couldn\'t parse received message');
client.send(build.ERROR.NOTIFY.JSON_PARSE_ERROR());
}
logger.push({ msg }).log('Handling received message');
logger.push({ msg }).debug('Handling received message');
switch (msg.msg) {
case MESSAGE.CONFIGURATION:
switch (msg.verb) {
Expand All @@ -257,7 +257,7 @@ class Server extends ws.Server {
case VERB.NOTIFY: {
const dup = structuredClone(this._appConfig); // fast-json-patch explicitly mutates
_.merge(dup, msg.data);
this._logger.push({ oldConf: this._appConfig, newConf: dup }).log('Emitting new configuration');
this._logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this.emit(EVENT.RECONFIGURE, dup);
break;
}
Expand All @@ -266,7 +266,7 @@ class Server extends ws.Server {
// client library?
const dup = structuredClone(this._appConfig); // fast-json-patch explicitly mutates
jsonPatch.applyPatch(dup, msg.data);
logger.push({ oldConf: this._appConfig, newConf: dup }).log('Emitting new configuration');
logger.push({ oldConf: this._appConfig, newConf: dup }).debug('Emitting new configuration');
this.emit(EVENT.RECONFIGURE, dup);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports.handleBulkQuotesRequestedDmEvt = async (
await options.producer.sendDomainEvent(bulkQuotesCallbackReceivedDmEvt);
}
catch (err) {
logger.push({ err }).log('Error in handleBulkQuotesRequestedDmEvt');
logger.push({ err }).error('Error in handleBulkQuotesRequestedDmEvt');
const bulkQuotesCallbackReceivedDmEvt = new BulkQuotesCallbackReceivedDmEvt({
bulkId: event.getKey(),
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports.handleBulkTransfersRequestedDmEvt = async (

await options.producer.sendDomainEvent(bulkTransfersCallbackReceivedDmEvt);
} catch (err) {
logger.push({ err }).log('Error in handleBulkTransfersRequestedDmEvt');
logger.push({ err }).error('Error in handleBulkTransfersRequestedDmEvt');
const bulkTransfersCallbackReceivedDmEvt = new BulkTransfersCallbackReceivedDmEvt({
bulkId: event.getKey(),
content: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports.handlePartyInfoRequestedDmEvt = async (
});
await options.producer.sendDomainEvent(partyInfoCallbackReceivedDmEvt);
} catch (err) {
logger.push({ err }).log('Error in handlePartyInfoRequestedDmEvt');
logger.push({ err }).error('Error in handlePartyInfoRequestedDmEvt');
const { code, message } = Errors.MojaloopApiErrorCodes.SERVER_TIMED_OUT;
const partyInfoCallbackReceivedDmEvt = new PartyInfoCallbackReceivedDmEvt({
bulkId: event.getKey(),
Expand Down

0 comments on commit abd88aa

Please sign in to comment.