Skip to content

Instantly share code, notes, and snippets.

@sebastianhoitz
Last active December 13, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianhoitz/4964446 to your computer and use it in GitHub Desktop.
Save sebastianhoitz/4964446 to your computer and use it in GitHub Desktop.
nodeJS mongoDB instrumentation – This allows you to profile all queries to your mongoDB collection, including collectionName, special find parameters and conditions.
collection = require("mongodb/lib/mongodb/collection").Collection
oldFind = collection.prototype.find
collection.prototype.find = ->
tagName = "#{@db.databaseName}/#{@collectionName}/find"
findParameters = arguments[1]
start = +new Date()
cursor = oldFind.apply @, arguments
selectorParameters = cursor.selector
oldArray = cursor.toArray
cursor.toArray = (cb) ->
instrumentedCb = =>
console.log tagName, +new Date() - start
if findParameters
console.log findParameters
if selectorParameters
console.log selectorParameters
cb.apply cursor, arguments
oldArray.apply cursor, [instrumentedCb]
cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment