Skip to content

Commit

Permalink
Merge pull request #14 from Zooz/fix-sub-route-express
Browse files Browse the repository at this point in the history
fix sub route for express framework
  • Loading branch information
kobik authored Dec 3, 2018
2 parents 002aed7 + 94945a1 commit 4ad1f63
Show file tree
Hide file tree
Showing 5 changed files with 570 additions and 23 deletions.
23 changes: 15 additions & 8 deletions src/metrics-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('pkginfo')(module, ['name']);
const debug = require('debug')(module.exports.name);
const utils = require('./utils');
const setupOptions = {};
const routers = {};

module.exports = (appVersion, projectName) => {
return (options = {}) => {
Expand Down Expand Up @@ -101,18 +102,23 @@ function _handleResponse (req, res) {
}

function _getRoute(req) {
let res = req.res;
let route = req.baseUrl; // express
if (req.swagger) { // swagger
route = req.swagger.apiPath;
} else if (req.route && route) { // express
} else if (req.route) { // express
if (req.route.path !== '/') {
route = route + req.route.path;
}
} else if (req.url && !route) { // restify
route = req.url;
if (req.route) {
route = req.route.path;

if (route === '') {
route = req.originalUrl;
} else {
const splittedRoute = route.split('/');
const splittedUrl = req.originalUrl.split('/');
const routeIndex = splittedUrl.length - splittedRoute.length + 1;

const baseUrl = splittedUrl.slice(0, routeIndex).join('/');
route = baseUrl + route;
}
}

Expand All @@ -126,8 +132,9 @@ function _getRoute(req) {
// this condition will evaluate to true only in
// express framework and no route was found for the request. if we log this metrics
// we'll risk in a memory leak since the route is not a pattern but a hardcoded string.
if (!req.route && res && res.statusCode === 404) {
return undefined;
if (!route || route === '') {
// if (!req.route && res && res.statusCode === 404) {
route = 'N/A';
}

return route;
Expand Down
Loading

0 comments on commit 4ad1f63

Please sign in to comment.