mongodb.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS207: Consider shorter variations of null checks
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. module.exports = {
  8. monitor(mongodb_require_path, logger) {
  9. let mongodb, mongodbCore;
  10. try {
  11. // for the v1 driver the methods to wrap are in the mongodb
  12. // module in lib/mongodb/db.js
  13. mongodb = require(`${mongodb_require_path}`);
  14. } catch (error) {}
  15. try {
  16. // for the v2 driver the relevant methods are in the mongodb-core
  17. // module in lib/topologies/{server,replset,mongos}.js
  18. const v2_path = mongodb_require_path.replace(/\/mongodb$/, '/mongodb-core');
  19. mongodbCore = require(v2_path);
  20. } catch (error1) {}
  21. const Metrics = require("./index");
  22. const monitorMethod = function(base, method, type) {
  23. let _method;
  24. if (base == null) { return; }
  25. if ((_method = base[method]) == null) { return; }
  26. const arglen = _method.length;
  27. const mongo_driver_v1_wrapper = function(db_command, options, callback) {
  28. let query;
  29. if (typeof callback === 'undefined') {
  30. callback = options;
  31. options = {};
  32. }
  33. const collection = db_command.collectionName;
  34. if (collection.match(/\$cmd$/)) {
  35. // Ignore noisy command methods like authenticating, ismaster and ping
  36. return _method.call(this, db_command, options, callback);
  37. }
  38. let key = `mongo-requests.${collection}.${type}`;
  39. if (db_command.query != null) {
  40. query = Object.keys(db_command.query).sort().join("_");
  41. key += "." + query;
  42. }
  43. const timer = new Metrics.Timer("mongo", {collection, query});
  44. const start = new Date();
  45. return _method.call(this, db_command, options, function() {
  46. timer.done();
  47. const time = new Date() - start;
  48. logger.log({
  49. query: db_command.query,
  50. query_type: type,
  51. collection,
  52. "response-time": new Date() - start
  53. },
  54. "mongo request");
  55. return callback.apply(this, arguments);
  56. });
  57. };
  58. const mongo_driver_v2_wrapper = function(ns, ops, options, callback) {
  59. let query;
  60. if (typeof callback === 'undefined') {
  61. callback = options;
  62. options = {};
  63. }
  64. if (ns.match(/\$cmd$/)) {
  65. // Ignore noisy command methods like authenticating, ismaster and ping
  66. return _method.call(this, ns, ops, options, callback);
  67. }
  68. let key = `mongo-requests.${ns}.${type}`;
  69. if (ops[0].q != null) { // ops[0].q
  70. query = Object.keys(ops[0].q).sort().join("_");
  71. key += "." + query;
  72. }
  73. const timer = new Metrics.Timer(key);
  74. const start = new Date();
  75. return _method.call(this, ns, ops, options, function() {
  76. timer.done();
  77. const time = new Date() - start;
  78. logger.log({
  79. query: ops[0].q,
  80. query_type: type,
  81. collection: ns,
  82. "response-time": new Date() - start
  83. },
  84. "mongo request");
  85. return callback.apply(this, arguments);
  86. });
  87. };
  88. if (arglen === 3) {
  89. return base[method] = mongo_driver_v1_wrapper;
  90. } else if (arglen === 4) {
  91. return base[method] = mongo_driver_v2_wrapper;
  92. }
  93. };
  94. monitorMethod(mongodb != null ? mongodb.Db.prototype : undefined, "_executeQueryCommand", "query");
  95. monitorMethod(mongodb != null ? mongodb.Db.prototype : undefined, "_executeRemoveCommand", "remove");
  96. monitorMethod(mongodb != null ? mongodb.Db.prototype : undefined, "_executeInsertCommand", "insert");
  97. monitorMethod(mongodb != null ? mongodb.Db.prototype : undefined, "_executeUpdateCommand", "update");
  98. monitorMethod(mongodbCore != null ? mongodbCore.Server.prototype : undefined, "command", "command");
  99. monitorMethod(mongodbCore != null ? mongodbCore.Server.prototype : undefined, "remove", "remove");
  100. monitorMethod(mongodbCore != null ? mongodbCore.Server.prototype : undefined, "insert", "insert");
  101. monitorMethod(mongodbCore != null ? mongodbCore.Server.prototype : undefined, "update", "update");
  102. monitorMethod(mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined, "command", "command");
  103. monitorMethod(mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined, "remove", "remove");
  104. monitorMethod(mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined, "insert", "insert");
  105. monitorMethod(mongodbCore != null ? mongodbCore.ReplSet.prototype : undefined, "update", "update");
  106. monitorMethod(mongodbCore != null ? mongodbCore.Mongos.prototype : undefined, "command", "command");
  107. monitorMethod(mongodbCore != null ? mongodbCore.Mongos.prototype : undefined, "remove", "remove");
  108. monitorMethod(mongodbCore != null ? mongodbCore.Mongos.prototype : undefined, "insert", "insert");
  109. return monitorMethod(mongodbCore != null ? mongodbCore.Mongos.prototype : undefined, "update", "update");
  110. }
  111. };