apminsight
Version:
monitor nodejs applications
87 lines (73 loc) • 2.31 kB
JavaScript
var constants = require("./../../constants");
var utils = require("./../../util/utils");
var componentName = "MONGODB";
/*
Issue : https://github.com/nodejs/node/issues/22360
Missing txn context in below statement due async hooks
mongoose internally create exec().then() in own then implementation
Tracker and operation will not be captured
const result = await Model.find();
The following statment will capture opn and create tracker
const result = await Model.find().exec();
*/
var moduleInfo = {
functions: [
{
functionName: [
"Model.countDocuments",
"Model.createIndexes",
"Model.findOneAndRemove",
"Model.geoSearch",
"Model.listIndexes",
"Model.ensureIndexes"
],
trackerType: constants.dbTracker,
component: componentName,
extractInfo: getOpnInfoFromModel
},
{
functionName: [
"Model.init",
"Model.startSession",
"Model.estimatedDocumentCount"
],
component: componentName,
extractInfo: getOpnInfoFromModel
}
]
};
function getOpnInfoFromCol(coll) {
let opnInfo = {};
if (!coll) {
return opnInfo;
}
if (coll.collectionName) {
opnInfo.object = coll.collectionName;
}
if (coll.conn) {
opnInfo.host = coll.conn.host;
opnInfo.port = coll.conn.port;
}
return opnInfo;
}
function getOpnInfoFromModel(invoker, params, returnObj, tracker) {
var coll;
if (invoker.__proto__) {
coll = invoker.__proto__.collection;
}
if (!coll && invoker.prototype) {
coll = invoker.prototype.collection;
}
tracker.updateInfo(getOpnInfoFromCol(coll));
}
/* eslint-disable no-unused-vars */
function getOpnInfoFromQuery(invoker, params, returnObj, tracker) {
if (invoker._collection) {
var coll = invoker._collection.collection;
var info = getOpnInfoFromCol(coll);
if (utils.isNonEmptyString(invoker.op)) info.opn = invoker.op;
tracker.updateInfo(info);
}
}
/* eslint-enable no-unused-vars */
module.exports = moduleInfo;