-
Notifications
You must be signed in to change notification settings - Fork 28
Description
There are some operations such as hooks and search indexing that have a heavy performance impact on API. We purposefully allow asynchronous methods inside hooks, such as model updates that don't effect the resulting payload, or third party endpoint requests that are again not crucial to the resulting payload.
Search indexing is always asynchronous which has shown to have a heavy impact when updating a large number of documents at once. The measurement of this operation is unfortunately not particularly transparent, but there may be a resolution for both hooks and search that is flexible enough not to impact performance when in production.
Stats handler
One potential is to introduce /workspace/handlers to house lightweight stats methods for logging and alerting, like an extension of consul.time/timeEnd.
Search usage
"settings": {
"compose": true,
"cache": false,
"authenticate": true,
"searchStatsHandler": "searchStats"
}Hook usage
const Stats = require('@dadi/api').Stats
module.exports = async (obj, type, data) => {
const stats = Stats("myStatsHandler").key(obj._id)
someSlowAsyncPromise()
.then(() => {
stats.end()
})
return obj
}Example handler
module.exports.settings = {
maxExecTime: 100
}
module.exports.onExceedLimit = async (stats) => {
console.log(`Time to execute ${stats.key} exceeded ${this.settings.maxExecTime}`)
}
module.exports.onEnd = async (stats) => {
console.log(`Time to execute ${stats.key} was ${stats.duration}`)
}