Skip to content

Commit

Permalink
starting point
Browse files Browse the repository at this point in the history
  • Loading branch information
respectTheCode committed Apr 24, 2013
1 parent aff360d commit d7635eb
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 3 deletions.
37 changes: 34 additions & 3 deletions README.md
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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./lib");
97 changes: 97 additions & 0 deletions lib/index.js
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);
});
};
21 changes: 21 additions & 0 deletions package.json
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"
}
}

0 comments on commit d7635eb

Please sign in to comment.