Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(dtrace): route probes (#1659)
  • Loading branch information
hekike committed May 15, 2018
1 parent fce0453 commit 84bcded
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 60 deletions.
60 changes: 0 additions & 60 deletions lib/router.js
Expand Up @@ -11,7 +11,6 @@ var uuid = require('uuid');

var Chain = require('./chain');
var RouterRegistryRadix = require('./routerRegistryRadix');
var dtrace = require('./dtrace');

///--- Globals

Expand Down Expand Up @@ -71,13 +70,10 @@ util.inherits(Router, EventEmitter);
* @returns {Chain|undefined} handler or undefined
*/
Router.prototype.lookup = function lookup(req, res) {
var self = this;
var pathname = req.getUrl().pathname;

// Find route
self._dtraceStart(req);
var registryRoute = this._registry.lookup(req.method, pathname);
self._dtraceEnd(req, res);

// Not found
if (!registryRoute) {
Expand Down Expand Up @@ -324,60 +320,4 @@ Router.prototype._getRouteName = function _getRouteName(name, method, path) {
return name;
};

/**
* Setup request and calls _onRequest to run middlewares and call router
*
* @private
* @memberof Router
* @instance
* @function _dtraceStart
* @param {Request} req - the request object
* @returns {undefined} no return value
* @fires Request,Response#request
*/
Router.prototype._dtraceStart = function _dtraceStart(req) {
if (!req.dtrace) {
return;
}

dtrace._rstfy_probes['route-start'].fire(function fire() {
return [
req.serverName,
req.route.name,
req._dtraceId,
req.method,
req.href(),
req.headers
];
});
};

/**
* Setup request and calls _onRequest to run middlewares and call router
*
* @private
* @memberof Router
* @instance
* @function _dtraceEnd
* @param {Request} req - the request object
* @param {Response} res - the response object
* @returns {undefined} no return value
* @fires Request,Response#request
*/
Router.prototype._dtraceEnd = function _dtraceEnd(req, res) {
if (!req.dtrace) {
return;
}

dtrace._rstfy_probes['route-done'].fire(function fire() {
return [
req.serverName,
req.route.name,
req._dtraceId,
res.statusCode || 200,
res.headers()
];
});
};

module.exports = Router;
28 changes: 28 additions & 0 deletions lib/server.js
Expand Up @@ -914,10 +914,38 @@ Server.prototype._runRoute = function _runRoute(req, res) {
self.emit('routed', req, res, req.route);

self._runUse(req, res, function afterUse() {
// DTrace
if (self.dtrace) {
dtrace._rstfy_probes['route-start'].fire(function fire() {
return [
self.name,
req.route.name,
req._dtraceId,
req.method,
req.href(),
req.headers
];
});
}

req._timeRouteStart = process.hrtime();
routeHandler(req, res, function afterRouter(err) {
// Execution time of a handler with error can be significantly lower
req._timeRouteEnd = process.hrtime();

// DTrace
if (self.dtrace) {
dtrace._rstfy_probes['route-done'].fire(function fire() {
return [
self.name,
req.route.name,
req._dtraceId,
res.statusCode || 200,
res.headers
];
});
}

self._afterRoute(err, req, res);
});
});
Expand Down

0 comments on commit 84bcded

Please sign in to comment.