-
-
Save vdeturckheim/5bd2c4f74163a3541f048e6b3e220c8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wrapAsync = function (orig, name) { | |
return async function () { | |
const uuid = Uuidv4(); | |
name = name || `mongoose.${this.op}`; // mongose Query.exec specific | |
const finish = function () { // this method will be called after the wrapped method gets executed | |
PerfHook.performance.measure(`${name}-${uuid}`, `start-${uuid}`, `end-${uuid}`); | |
PerfHook.performance.clearMarks(`start-${uuid}`); | |
PerfHook.performance.clearMarks(`end-${uuid}`); | |
PerfHook.performance.clearMeasures(`${name}-${uuid}`); // we remove the marks and the measure to prevent a memory leak here. | |
}; | |
try { | |
PerfHook.performance.mark(`start-${uuid}`); // start measuring time | |
const res = await orig.apply(this, arguments); // calling original method | |
PerfHook.performance.mark(`end-${uuid}`); | |
finish(); | |
return res; | |
} | |
catch (err) { | |
PerfHook.performance.mark(`end-${uuid}`); | |
finish(); | |
throw err; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment