-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aff360d
commit d7635eb
Showing
4 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,35 @@ | ||
node-datadog-mongodb | ||
==================== | ||
# node-datadog-mongodb | ||
|
||
MongoDB query performance monitoring with DataDog | ||
|
||
## Usage | ||
|
||
```javascript | ||
var mongoDb = require("mongodb"); | ||
require("datadog-mongodb")(mongoDb); | ||
``` | ||
|
||
Or with `Mongoose` | ||
|
||
```javascript | ||
var mongoose = require("mongoose"); | ||
require("datadog-mongodb")(mongoose.mongo); | ||
``` | ||
|
||
Or reusing `node-dogstatsd` client | ||
|
||
```javascript | ||
var mongoDb = require("mongodb"); | ||
require("datadog-mongodb")(mongoDb, {dogstatsd: statsD}); | ||
``` | ||
|
||
## Options | ||
|
||
All options are optional. | ||
|
||
* `dogstatsd`: `node-dogstatsd` client. `default = new require("node-dogstatsd").StatsD()` | ||
|
||
## License | ||
|
||
View the [LICENSE](https://github.com/AppPress/node-datadog-mongodb/blob/master/LICENSE) file. | ||
|
||
MongoDB query performance monitoring with DataDog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("./lib"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
var traceback = require("traceback"); | ||
var DD = require("node-dogstatsd").StatsD; | ||
|
||
module.exports = function (mongodb, options) { | ||
options = options || {}; | ||
var datadog = options.dogstatsd || new DD(); | ||
var stat = options.stat || "node.mongodb"; | ||
|
||
var wrapFunction = function (object, fnName) { | ||
var fn = object[fnName]; | ||
|
||
object[fnName] = function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
var tags = []; | ||
|
||
tags.push("collection:" + this.collectionName); | ||
tags.push("operation:" + fnName); | ||
|
||
var stack = traceback(); | ||
|
||
// look for first file that is not in `node_modules` and not this module | ||
var stackLength = stack.length; | ||
for (var i = 0; i < stackLength; i++) { | ||
var path = stack[i].path; | ||
|
||
if ( | ||
path.indexOf("node_modules") == -1 && | ||
path.substr(0, 1) == "/" && | ||
path.indexOf(__dirname) == -1 | ||
) { | ||
var calledBy = stack[i]; | ||
tags.push("caller:" + calledBy.path + ":" + calledBy.line); | ||
break; | ||
} | ||
} | ||
|
||
// look for query fields | ||
if (fnName != "insert" && typeof(args[0]) == "object") { | ||
// sort and comma delimit | ||
var fields = Object.keys(args[0]).sort().join(","); | ||
|
||
// only add the tag if we found fields | ||
if (fields != "") { | ||
tags.push("fields:" + fields); | ||
} | ||
} | ||
|
||
// callback is always last so grab it | ||
var cb = args[args.length - 1]; | ||
|
||
if (typeof(cb) == "function") { | ||
// hold on to start time | ||
var start = new Date(); | ||
|
||
args[args.length - 1] = function (err) { | ||
// calc time to run operation | ||
var time = (new Date() - start); | ||
|
||
if (err) { | ||
tags.push("error:" + err.message); | ||
} | ||
|
||
// if update or remove grab the count argument | ||
if (arguments[1] && ( | ||
fnName == "update" || | ||
fnName == "remove" | ||
)) { | ||
tags.push("count:" + arguments[1]); | ||
} | ||
|
||
// if insert grab the length of the docs argument | ||
if (arguments[1] && fnName == "insert") { | ||
tags.push("count:" + arguments[1].length); | ||
} | ||
|
||
// send histogram data | ||
datadog.histogram(stat, time, 1, tags); | ||
datadog.set(stat + ".count", fnName, tags); | ||
|
||
// return and call callback | ||
return cb.apply(this, arguments); | ||
}; | ||
} else { | ||
// no callback just send the data | ||
datadog.set(stat + ".count", fnName, tags); | ||
} | ||
|
||
// return and call the function we are wrapping | ||
return fn.apply(this, args); | ||
}; | ||
}; | ||
|
||
// wrapp mongodb functions | ||
["find", "update", "insert", "remove"].forEach(function (fnName) { | ||
wrapFunction(mongodb.Collection.prototype, fnName); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "datadog-mongodb", | ||
"version": "0.0.1", | ||
"description": "MongoDB query performance monitoring with DataDog", | ||
"main": "lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/AppPress/node-datadog-mongodb.git" | ||
}, | ||
"keywords": [ | ||
"MongoDB", | ||
"DataDog" | ||
], | ||
"author": "App Press", | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"dependencies": { | ||
"traceback": "0.3.0", | ||
"node-dogstatsd": "0.0.4" | ||
} | ||
} |