mongodb.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. module.exports =
  2. monitor: (mongodb_require_path, logger) ->
  3. try
  4. # for the v1 driver the methods to wrap are in the mongodb
  5. # module in lib/mongodb/db.js
  6. mongodb = require("#{mongodb_require_path}")
  7. try
  8. # for the v2 driver the relevant methods are in the mongodb-core
  9. # module in lib/topologies/{server,replset,mongos}.js
  10. v2_path = mongodb_require_path.replace(/\/mongodb$/, '/mongodb-core')
  11. mongodbCore = require(v2_path)
  12. Metrics = require("./metrics")
  13. monitorMethod = (base, method, type) ->
  14. return unless base?
  15. return unless (_method = base[method])?
  16. arglen = _method.length
  17. mongo_driver_v1_wrapper = (db_command, options, callback) ->
  18. if (typeof callback == 'undefined')
  19. callback = options
  20. options = {}
  21. collection = db_command.collectionName
  22. if collection.match(/\$cmd$/)
  23. # Ignore noisy command methods like authenticating, ismaster and ping
  24. return _method.call this, db_command, options, callback
  25. key = "mongo-requests.#{collection}.#{type}"
  26. if db_command.query?
  27. query = Object.keys(db_command.query).sort().join("_")
  28. key += "." + query
  29. timer = new Metrics.Timer("mongo", {collection: collection, query:query})
  30. start = new Date()
  31. _method.call this, db_command, options, () ->
  32. timer.done()
  33. time = new Date() - start
  34. logger.log
  35. query: db_command.query
  36. query_type: type
  37. collection: collection
  38. "response-time": new Date() - start
  39. "mongo request"
  40. callback.apply this, arguments
  41. mongo_driver_v2_wrapper = (ns, ops, options, callback) ->
  42. if (typeof callback == 'undefined')
  43. callback = options
  44. options = {}
  45. if ns.match(/\$cmd$/)
  46. # Ignore noisy command methods like authenticating, ismaster and ping
  47. return _method.call this, ns, ops, options, callback
  48. key = "mongo-requests.#{ns}.#{type}"
  49. if ops[0].q? # ops[0].q
  50. query = Object.keys(ops[0].q).sort().join("_")
  51. key += "." + query
  52. timer = new Metrics.Timer(key)
  53. start = new Date()
  54. _method.call this, ns, ops, options, () ->
  55. timer.done()
  56. time = new Date() - start
  57. logger.log
  58. query: ops[0].q
  59. query_type: type
  60. collection: ns
  61. "response-time": new Date() - start
  62. "mongo request"
  63. callback.apply this, arguments
  64. if arglen == 3
  65. base[method] = mongo_driver_v1_wrapper
  66. else if arglen == 4
  67. base[method] = mongo_driver_v2_wrapper
  68. monitorMethod(mongodb?.Db.prototype, "_executeQueryCommand", "query")
  69. monitorMethod(mongodb?.Db.prototype, "_executeRemoveCommand", "remove")
  70. monitorMethod(mongodb?.Db.prototype, "_executeInsertCommand", "insert")
  71. monitorMethod(mongodb?.Db.prototype, "_executeUpdateCommand", "update")
  72. monitorMethod(mongodbCore?.Server.prototype, "command", "command")
  73. monitorMethod(mongodbCore?.Server.prototype, "remove", "remove")
  74. monitorMethod(mongodbCore?.Server.prototype, "insert", "insert")
  75. monitorMethod(mongodbCore?.Server.prototype, "update", "update")
  76. monitorMethod(mongodbCore?.ReplSet.prototype, "command", "command")
  77. monitorMethod(mongodbCore?.ReplSet.prototype, "remove", "remove")
  78. monitorMethod(mongodbCore?.ReplSet.prototype, "insert", "insert")
  79. monitorMethod(mongodbCore?.ReplSet.prototype, "update", "update")
  80. monitorMethod(mongodbCore?.Mongos.prototype, "command", "command")
  81. monitorMethod(mongodbCore?.Mongos.prototype, "remove", "remove")
  82. monitorMethod(mongodbCore?.Mongos.prototype, "insert", "insert")
  83. monitorMethod(mongodbCore?.Mongos.prototype, "update", "update")