/*
 * Return express middleware that measures overall performance.
 */
function factory(parentClient) {
    return function (prefix, options) {
        var client = parentClient.getChildClient(prefix);
        options = options || {};

        return function (req, res, next) {
            var startTime = new Date();

            // Shadow end request
            var end = res.end;
            res.end = function (chunk, encoding) {
                end.apply(res, arguments);

                client.timing('response_time', startTime);
                client.increment('response.' + res.statusCode);
            };
            next();
        };
    };
}

module.exports = factory;

/*
 * Return express middleware that measures overall performance.
 */
function factory(parentClient) {
    return function (prefix, options) {
        var client = parentClient.getChildClient(prefix);
        options = options || {};
        var timeByUrl = options.timeByUrl || false
        var onResponeEnd = options.onResponseEnd

        return function (req, res, next) {
            var startTime = new Date();

            // Shadow end request
            var end = res.end;
            res.end = function () {
                end.apply(res, arguments);

                client.timing('response_time', startTime);
                client.increment('response.' + res.statusCode);

                if (timeByUrl && req.route && req.route.path) {
                    var routeName = req.route.path;
                    // need to get rid of : in route names, remove first /, and replace rest with _
                    routeName = routeName.replace(/:/g, "").replace(/\//, "").replace(/\//g, "_");
                    client.timing('response_time.by_url.' + routeName, startTime);
                };

                if (onResponseEnd) {
                    onResponseEnd(client, startTime, req, res);
                };
            };
            next();
        };
    };
}

module.exports = factory;

add in suppport for timing each express route and make the middleware more extendable