UNPKG

apminsight

Version:

monitor nodejs applications

178 lines (164 loc) 4.51 kB
// supported from v2.2.19 to v3.0.10 var constants = require("./../../constants"); const utils = require("./../../util/utils"); var componentName = "MONGODB"; var dbOperations = [ "createCollection", "stats", "command", "eval", "collection", "renameCollection", "dropCollection", "dropDatabase", "collections", "createIndex", "ensureIndex", "addUser", "removeUser", "authenticate", "indexInformation" ]; var collectionOpn = [ "insertOne", "insertMany", "bulkWrite", "insert", "updateOne", "replaceOne", "updateMany", "update", "deleteOne", "deleteMany", "remove", "save", "rename", "drop", "options", "isCapped", "createIndex", "createIndexes", "dropIndex", "dropIndexes", "reIndex", "ensureIndex", "indexExists", "indexInformation", "count", "distinct", "indexes", "stats", "findOne", "findOneAndDelete", "findOneAndReplace", "findOneAndUpdate", "findAndModify", "findAndRemove", "aggregate", "parallelCollectionScan", "geoNear", "geoHaystackSearch", "group", "mapReduce" ]; var moduleInfo = { functions: [ { functionName: "MongoClient.connect", component: componentName }, { functionName: [ "Db.prototype.open", "Db.prototype.close", "Db.prototype.logout", "Db.prototype.executeDbAdminCommand" ], component: componentName }, { functionName: "Collection.prototype.find", trackerType: constants.dbTracker, component: componentName, extractInfo: listenCursorEvents } ] }; updateModuleInfo("Db", dbOperations, getOpnInfoFromDb); updateModuleInfo("Collection", collectionOpn, getOpnInfoFromCollection); function updateModuleInfo(type, methods, extractInfo) { methods.forEach(function (each) { var qualifiedName = type + ".prototype." + each; var methodInfo = { functionName: qualifiedName, component: componentName, extractInfo: extractInfo, trackerType: constants.dbTracker }; moduleInfo.functions.push(methodInfo); }); } function getOpnInfoFromDb(invoker, params, returnObj, tracker) { var info = {}; if (invoker.databaseName) { info.object = invoker.databaseName; } if (isHostDetailsPresent(invoker.topology)) { info.host = invoker.topology.host; info.port = invoker.topology.port; } tracker.updateInfo(info); } function getOpnInfoFromCollection(invoker, params, returnObj, tracker) { var info = {}; if (invoker.namespace) { info.object = invoker.namespace; } if (invoker.s && isHostDetailsPresent(invoker.s.topology)) { info.host = invoker.s.topology.host; info.port = invoker.s.topology.port; } if ( invoker.s && invoker.s.topology && invoker.s.topology.s && Array.isArray(invoker.s.topology.s.seedlist) && isHostDetailsPresent(invoker.s.topology.s.seedlist[0]) ) { info.host = invoker.s.topology.s.seedlist[0].host; info.port = invoker.s.topology.s.seedlist[0].port; } tracker.updateInfo(info); } /* eslint-disable no-unused-vars */ function getOpnInfoFromCursor(invoker, params, returnObj, tracker) { var info = {}; if (invoker.ns) { info.object = invoker.ns; } if (invoker.s && isHostDetailsPresent(invoker.s.topologyOptions)) { info.host = invoker.s.topologyOptions.host; info.port = invoker.s.topologyOptions.port; } tracker.updateInfo(info); } /* eslint-enable no-unused-vars */ function listenCursorEvents(invoker, params, cursorObj, tracker) { let txn = apmInsightAgentInstance.getCurTxn(); getOpnInfoFromCollection(invoker, params, cursorObj, tracker); if (cursorObj && utils.isFunction(cursorObj.on)) { cursorObj.on("end", function () { tracker.endTracker(txn); }); cursorObj.on("close", function () { tracker.endTracker(txn); }); cursorObj.on("error", function (error) { tracker.setError(error, txn); }); } } function isHostDetailsPresent(hostInfo) { return hostInfo && hostInfo.host && hostInfo.port; } module.exports = moduleInfo;