diff --git a/install/package.json b/install/package.json index cb5eb4e4ea..579928e3d4 100644 --- a/install/package.json +++ b/install/package.json @@ -76,6 +76,7 @@ "html-to-text": "9.0.5", "imagesloaded": "5.0.0", "ipaddr.js": "2.2.0", + "iroh": "^0.3.0", "jquery": "3.7.1", "jquery-deserialize": "2.0.0", "jquery-form": "4.3.0", diff --git a/iroh/iroh-monitor.js b/iroh/iroh-monitor.js new file mode 100644 index 0000000000..64c6c2b863 --- /dev/null +++ b/iroh/iroh-monitor.js @@ -0,0 +1,56 @@ +'use strict'; + +const Iroh = require('iroh'); + +// Asked help from co-pilot for this function as I needed to simplify parsing for Iroh. +function monitorFunction(fn, name) { + console.log(`Setting up Iroh monitoring for ${name}`); + + const stage = new Iroh.Stage(` + function monitored() { + try { + return target(); + } catch(e) { + console.error(e); + throw e; + } + } + `); + + // Using Iroh's listeners + stage.addListener(Iroh.CALL) + .on('before', (e) => { + console.log(`[IROH] ${name} called`); + e.setData('time', process.hrtime()); + }) + .on('after', (e) => { + const diff = process.hrtime(e.getData('time')); + const time = ((diff[0] * 1e9) + diff[1]) / 1e6; + console.log(`[IROH] ${name} took ${time}ms`); + }); + + // Return the original function with timing + return async function (...args) { + const start = process.hrtime(); + console.log(`[START] ${name}`); + + try { + const result = await fn.apply(this, args); + const [s, ns] = process.hrtime(start); + const ms = (s * 1000) + (ns / 1e6); + console.log(`[TIME] ${name} took ${ms.toFixed(2)}ms`); + console.log(`[END] ${name}`); + return result; + } catch (error) { + console.error(`[ERROR] ${name}:`, error); + throw error; + } + }; +} + +module.exports = { + monitorFunction: monitorFunction, + CALL: Iroh.CALL, + FUNCTION: Iroh.FUNCTION, + TRY: Iroh.TRY, +}; diff --git a/iroh/iroh-output.txt b/iroh/iroh-output.txt new file mode 100644 index 0000000000..f021790e47 --- /dev/null +++ b/iroh/iroh-output.txt @@ -0,0 +1,39 @@ +(base) jullia@jullias-air nodebb-f24-sweethearts % node app.js +2024-10-24T21:00:25.995Z [4567/64147] - info: NodeBB v3.8.4 Copyright (C) 2013-2024 NodeBB Inc. +2024-10-24T21:00:25.996Z [4567/64147] - info: This program comes with ABSOLUTELY NO WARRANTY. +2024-10-24T21:00:25.996Z [4567/64147] - info: This is free software, and you are welcome to redistribute it under certain conditions. +2024-10-24T21:00:25.997Z [4567/64147] - info: +Initializing Iroh monitoring... +Setting up Iroh monitoring for Topics.create +Setting up Iroh monitoring for Topics.reply +Iroh monitoring successfully initialized!! +2024-10-24T21:00:26.685Z [4567/64147] - info: Initializing NodeBB v3.8.4 http://localhost:4567 +(node:64147) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. +(Use `node --trace-deprecation ...` to show where the warning was created) +2024-10-24T21:00:26.892Z [4567/64147] - info: [socket.io] Restricting access to origin: http://localhost:* +2024-10-24T21:00:27.004Z [4567/64147] - info: [api] Adding 0 route(s) to `api/v3/plugins` +2024-10-24T21:00:27.007Z [4567/64147] - info: [router] Routes added +Tag "homework" successfully created! +Tag "assignment" successfully created! +2024-10-24T21:00:27.011Z [4567/64147] - info: 🎉 NodeBB Ready +2024-10-24T21:00:27.012Z [4567/64147] - info: 🤝 Enabling 'trust proxy' +2024-10-24T21:00:27.013Z [4567/64147] - info: 📡 NodeBB is now listening on: 0.0.0.0:4567 +2024-10-24T21:00:27.013Z [4567/64147] - info: 🔗 Canonical URL: http://localhost:4567 +WARNING: The keyword 'none' must be used as a single argument. + ../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow() + ../../../bootstrap/scss/forms/_form-control.scss 40:7 @import + bootstrap/scss/_forms.scss 3:9 @import + - 19:9 root stylesheet + +WARNING: The keyword 'none' must be used as a single argument. + ../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow() + ../../../bootstrap/scss/forms/_form-select.scss 32:7 @import + bootstrap/scss/_forms.scss 4:9 @import + - 19:9 root stylesheet + +[START] Topics.create +[TIME] Topics.create took 2.12ms +[END] Topics.create +[START] Topics.reply +[TIME] Topics.reply took 21.73ms +[END] Topics.reply \ No newline at end of file diff --git a/src/start.js b/src/start.js index cba1fc8f9c..c57726f418 100644 --- a/src/start.js +++ b/src/start.js @@ -2,6 +2,7 @@ const nconf = require('nconf'); const winston = require('winston'); +const irohMonitor = require('../iroh/iroh-monitor'); const start = module.exports; @@ -9,8 +10,19 @@ const db = require('./database'); const Topics = require('./topics'); start.start = async function () { - printStartupInfo(); + try { + console.log('Initializing Iroh monitoring...'); + // I chose to monitor core nodeBB topic functions (create and reply). + Topics.create = irohMonitor.monitorFunction(Topics.create, 'Topics.create'); + Topics.reply = irohMonitor.monitorFunction(Topics.reply, 'Topics.reply'); + + console.log('Iroh monitoring successfully initialized!!'); + } catch (err) { + console.error('Failed to initialize Iroh monitoring:', err); + } + + printStartupInfo(); addProcessHandlers(); try {