Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Changed passing of params to node-dogstatsd to object #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Add middleware immediately before your router.

All options are optional.


* `dogstatsd` node-dogstatsd client. `default = new (require("node-dogstatsd")).StatsD()`
* `datadogOptions` *object* configuration for the node-dogstatds client(if you don't pass one yourself). `default = {}`
* `stat` *string* name for the stat. `default = "node.express.router"`
* `tags` *array* of tags to be added to the histogram. `default = []`
* `path` *boolean* include path tag. `default = false`
Expand Down
93 changes: 49 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
var DD = require("node-dogstatsd").StatsD;

module.exports = function (options) {
var datadog = options.dogstatsd || new DD();
var stat = options.stat || "node.express.router";
var tags = options.tags || [];
var path = options.path || false;
var base_url = options.base_url || false;
var response_code = options.response_code || false;

return function (req, res, next) {
if (!req._startTime) {
req._startTime = new Date();
}

var end = res.end;
res.end = function (chunk, encoding) {
res.end = end;
res.end(chunk, encoding);

if (!req.route || !req.route.path) {
return;
}

var baseUrl = (base_url !== false) ? req.baseUrl : '';
var statTags = [
var DD = require("node-dogstatsd")
.StatsD;

module.exports = function(options) {
var datadogOptions = options.datadogOptions;
var datadog = options.dogstatsd || new DD(datadogOptions);
var stat = options.stat || "node.express.router";
var tags = options.tags || [];
var path = options.path || false;
var base_url = options.base_url || false;
var response_code = options.response_code || false;

return function(req, res, next) {
if (!req._startTime) {
req._startTime = new Date();
}

var end = res.end;
res.end = function(chunk, encoding) {
res.end = end;
res.end(chunk, encoding);

if (!req.route || !req.route.path) {
return;
}

var baseUrl = (base_url !== false) ? req.baseUrl : '';
var statTags = [
"route:" + baseUrl + req.route.path
].concat(tags);

if (options.method) {
statTags.push("method:" + req.method.toLowerCase());
}
if (options.method) {
statTags.push("method:" + req.method.toLowerCase());
}

if (options.protocol && req.protocol) {
statTags.push("protocol:" + req.protocol);
}
if (options.protocol && req.protocol) {
statTags.push("protocol:" + req.protocol);
}

if (path !== false) {
statTags.push("path:" + baseUrl + req.path);
}
if (path !== false) {
statTags.push("path:" + baseUrl + req.path);
}

if (response_code) {
statTags.push("response_code:" + res.statusCode);
datadog.increment(stat + '.response_code.' + res.statusCode , 1, statTags);
datadog.increment(stat + '.response_code.all' , 1, statTags);
}
if (response_code) {
statTags.push("response_code:" + res.statusCode);
datadog.increment(stat + '.response_code.' + res.statusCode,
1, statTags);
datadog.increment(stat + '.response_code.all', 1,
statTags);
}

datadog.histogram(stat + '.response_time', (new Date() - req._startTime), 1, statTags);
};
datadog.histogram(stat + '.response_time', (new Date() -
req._startTime), 1, statTags);
};

next();
};
next();
};
};