diff --git a/router/api/telemetry/telemetry.js b/router/api/telemetry/telemetry.js index 4b1a5b6..70b0c67 100644 --- a/router/api/telemetry/telemetry.js +++ b/router/api/telemetry/telemetry.js @@ -1,6 +1,8 @@ const _ = require('lodash') var LOG = require('../../log/logger') const Telemetry = require('sb_telemetry_util') +const TelemetryLogger = require('sb_logger_util') +var UUID = require('uuid') const telemetry = new Telemetry() module.exports = { ver : '', @@ -89,5 +91,33 @@ module.exports = { } catch(e) { LOG.error('Error while initilising telemetry') } + }, + + /** + * for log event + * data object have these properties {'edata', context', 'object', 'tags'} + */ +telemetryLog : function (data, edata) { + try { + var channelId = data.channelId + var appId = data.appId + var env = data.env + var rollup = telemetry.getRollUpData([]) + var did = data.deviceId + var mid = 'LOG:' + UUID(); + let logData = { + eid: "LOG", + ets: new Date().getTime(), + ver: "3.0", + mid: mid, + actor: telemetry.getActorData(data.userId, 'user'), + context: telemetry.getContextData({ channel: channelId, env: env, pdata: {id: 'org.sunbird.chatbot.router'},cdata: [], rollup: rollup, did: did }), + object: {}, + edata: edata + }; + TelemetryLogger.info(logData); + } catch (e) { + LOG.error('Error while logger info event') } +} } \ No newline at end of file diff --git a/router/appRest.js b/router/appRest.js index 3b6f144..6a3bc31 100644 --- a/router/appRest.js +++ b/router/appRest.js @@ -45,7 +45,8 @@ appBot.post('/bot', function (req, res) { env: req.body.appId + '.bot', sessionId: '', channelId: req.body.channel, - uaspec: getUserSpec(req) + uaspec: getUserSpec(req), + requestid: req.headers["x-request-id"] ? req.headers["x-request-id"] :"", } } handler(req, res, data) @@ -66,12 +67,23 @@ appBot.post('/whatsapp', function (req, res) { env: config.TELEMETRY_DATA_ENV_WHATSAPP, channelId: config.TELEMETRY_DATA_CHANNELID_WHATSAPP, sessionId: '', - uaspec: getUserSpec(req) + uaspec: getUserSpec(req), + requestid: req.headers["x-request-id"] ? req.headers["x-request-id"] :"", } } handler(req, res, data) } else { - sendErrorResponse(res) + var customData= { + userId: crypto.createHash('sha256').update(req.body.incoming_message[0].from).digest("hex"), + deviceId: crypto.createHash('sha256').update(req.body.incoming_message[0].from).digest("hex"), + appId: config.TELEMETRY_DATA_PID_WHATSAPP, + env: config.TELEMETRY_DATA_ENV_WHATSAPP, + channelId: config.TELEMETRY_DATA_CHANNELID_WHATSAPP, + sessionId: '', + uaspec: getUserSpec(req), + requestid: req.headers["x-request-id"] ? req.headers["x-request-id"] :"", + } + sendErrorResponse(res, customData) } }) @@ -83,6 +95,13 @@ function handler(req, res, data) { redisSessionData = {}; if (!data.customData.deviceId) { + var edata = { + type: "system", + level: "INFO", + requestid: data.customData.requestid, + message: "Attribute missing from request body" + } + telemetry.telemetryLog(data.customData, edata); sendResponse(data.customData.deviceId, res, "From attribute missing", 400); } else { redisClient.get(REDIS_KEY_PREFIX + data.customData.deviceId, (err, redisValue) => { @@ -133,7 +152,15 @@ function handler(req, res, data) { function freeFlowLogic(data, res, chatflowConfig) { RasaCoreController.processUserData(data, (err, resp) => { var response = ''; + var edata = { + type: "system", + level: "INFO", + requestid: data.customData.requestid, + message: "" + } if (err) { + edata.message = "Sorry: Core RASA controller failed to load"; + telemetry.telemetryLog(data.customData, edata); sendChannelResponse(data.customData.deviceId, res, data, 'SORRY') } else { var responses = resp.res; @@ -156,8 +183,12 @@ function freeFlowLogic(data, res, chatflowConfig) { } telemetry.logInteraction(telemetryData); if (data.channel == config.WHATSAPP) { + edata.message = "Free flow whats app response"; + telemetry.telemetryLog(data.customData, edata); sendResponseWhatsapp(res, response, data.recipient, "freeFlow") } else { + edata.message = "Free flow web poartal response"; + telemetry.telemetryLog(data.customData, edata); sendResponse(res, response) } @@ -198,6 +229,14 @@ function menuDrivenLogic(data, res, chatflowConfig) { } else { responseKey = getErrorMessageForInvalidInput(currentFlowStep, chatflowConfig, true) menuIntentKnown = false + var edata = { + type: "system", + level: "INFO", + requestid: data.customData.requestid, + message: "" + } + edata.message = "UNKNOWN OPTION"; + telemetry.telemetryLog(data.customData, edata); // TODO : Don't call function inside each if/else if it should be called once. telemetryData = createInteractionData({ currentStep: currentFlowStep + '_UNKNOWN_OPTION' }, data, false) } @@ -237,8 +276,16 @@ function consolidatedLog(data, responseKey, menuIntentKnown, isMenuDriven) { intent = config.FREEFLOW_INTENT_NOT_DETECTED } } - - LOG.info("UserId: " + data.customData.userId + "," + " DeviceId: " + data.customData.deviceId + "," + " UserQuery: " + data.message + "," + " Bot_Response_identifier: " + intent + "," + " BotResponse: " + responseKey) + var message = "UserId: " + data.customData.userId + "," + " DeviceId: " + data.customData.deviceId + "," + " UserQuery: " + data.message + "," + " Bot_Response_identifier: " + intent + "," + " BotResponse: " + responseKey; + + var edata = { + type: "system", + level: "INFO", + requestid: data.customData.requestid, + message: message + } + telemetry.telemetryLog(data.customData, edata) + LOG.info(message) } function setRedisKeyValue(key, value) { @@ -300,7 +347,14 @@ function sendResponse(response, responseBody, responseCode) { response.send(responseBody) } -function sendErrorResponse(response){ +function sendErrorResponse(response, data){ + var edata = { + type: "system", + level: "INFO", + requestid: data.customData.requestid, + message: "401 invalid request" + } + telemetry.telemetryLog(data, edata) response.status(401); response.send('invalid request'); } @@ -342,7 +396,16 @@ function sendResponseWhatsapp(response,responseBody, recipient, textContent) { } function sendChannelResponse(response, responseKey, data, responseCode) { response.set('Content-Type', 'application/json') - if (responseCode) response.status(responseCode) + if (responseCode){ + response.status(responseCode) + var edata = { + type: "system", + level: "INFO", + requestid: data.customData.requestid, + message: "404 not found" + } + telemetry.telemetryLog(data.customData, edata) + } //version check var channelResponse = literals.message[responseKey + '_' + data.channel]; diff --git a/router/libs/sb_logger_util/index.js b/router/libs/sb_logger_util/index.js new file mode 100644 index 0000000..546d7b1 --- /dev/null +++ b/router/libs/sb_logger_util/index.js @@ -0,0 +1,74 @@ +/* + * @file: index.js + * @author: Jagadish P + * @desc: using log4s, enables application wide logging. + */ +var log4js = require('log4js') +var fs = require('fs') +var dir = '../../log' +var configUtil = require('../sb-config-util') + +if (!fs.existsSync(dir)) { + fs.mkdirSync(dir) +} + +var options = { + 'appenders': [{ + 'type': 'clustered', + 'appenders': [ + { + 'type': 'console' + }, { + 'type': 'logLevelFilter', + 'level': 'TRACE', + 'category': 'api', + 'appender': { + 'type': 'file', + 'maxLogSize': 26214400, + 'filename': '../../log/microService.log' + } + } + ] + }] +} + +log4js.configure(options) +var logger = log4js.getLogger('api') + +var isLoggerEnabled = function () { + var loggerEnabled = configUtil.getConfig('ENABLE_LOGGING') + return (loggerEnabled !== undefined && loggerEnabled === 'true') +} + +var info = function (data) { + if (data) { + logger.setLevel('INFO') + logger.info(JSON.stringify(data)) + } +} + +var error = function (data) { + if (data && isLoggerEnabled()) { + logger.setLevel('ERROR') + logger.error(JSON.stringify(data)) + } +} + +var warn = function (data) { + if (data && isLoggerEnabled()) { + logger.setLevel('WARN') + logger.warn(JSON.stringify(data)) + } +} + +var trace = function (data) { + if (data && isLoggerEnabled()) { + logger.setLevel('TRACE') + logger.trace(JSON.stringify(data)) + } +} + +module.exports.info = info +module.exports.error = error +module.exports.warn = warn +module.exports.trace = trace \ No newline at end of file diff --git a/router/libs/sb_logger_util/package.json b/router/libs/sb_logger_util/package.json new file mode 100644 index 0000000..34cdd6d --- /dev/null +++ b/router/libs/sb_logger_util/package.json @@ -0,0 +1,14 @@ +{ + "name": "sb_logger_util", + "version": "1.0.0", + "description": "this package serves as log", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Jagadish", + "license": "ISC", + "dependencies": { + "log4js": "^0.6.29" + } +} \ No newline at end of file diff --git a/router/package.json b/router/package.json index 6319ab7..aa73a92 100644 --- a/router/package.json +++ b/router/package.json @@ -20,6 +20,7 @@ "ua-parser-js": "^0.7.13", "sb_telemetry_util": "file:libs/sb_telemetry_util", "sb_config_util": "file:libs/sb-config-util", + "sb_logger_util": "file:libs/sb_logger_util", "redis": "^3.0.2", "request": "^2.88.0", "socket.io": "^2.3.0",