Browse files

add responseTimeHeader and responseTimeFormatter as options

these get passed to Server. Response time formatter must be a function
(lambda) and accept 1 arg (the runtime in milliseconds)

Example:
responseTimeHeader: 'X-Runtime',
responseTimeFormatter: function(durationInMilliseconds) {
  return durationInMilliseconds / 1000;
}

This will change the header to X-Runtime and the duration will be
divided by 1000 to get seconds
  • Loading branch information...
1 parent af03870 commit d3b660491e0b503ae20ecc82f1acbe3d6c461d1e @sberryman sberryman committed Mar 2, 2012
Showing with 15 additions and 7 deletions.
  1. +6 −6 lib/response.js
  2. +9 −1 lib/server.js
View
12 lib/response.js
@@ -30,8 +30,7 @@ var ALLOW_HEADERS = [
var EXPOSE_HEADERS = [
'X-Api-Version',
- 'X-Request-Id',
- 'X-Response-Time'
+ 'X-Request-Id'
].join(', ');
var DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
@@ -211,6 +210,8 @@ function Response(options) {
this.res = options.response;
this.serverName = options.serverName || 'restify';
this.version = options.version || false;
+ this.responseTimeHeader = options.responseTimeHeader || 'X-Response-Time';
+ this.responseTimeFormatter = options.responseTimeFormatter || function(durationInMilliseconds) { return durationInMilliseconds; }
// Generate the formatters
if (options.formatters) {
@@ -476,9 +477,9 @@ Response.prototype.finishHeaders = function finishHeaders(data) {
if (!this.header('Date'))
this.header('Date', httpDate(now));
-
- if (!this.header('X-Response-Time'))
- this.header('X-Response-Time', now.getTime() - this.req.time.getTime());
+
+ if (!this.header(this.responseTimeHeader))
+ this.header(this.responseTimeHeader, this.responseTimeFormatter(now.getTime() - this.req.time.getTime()));
};
@@ -537,7 +538,6 @@ Response.prototype.json = function json(obj) {
};
-
Response.prototype.format = function format(body) {
var log = this.log;
View
10 lib/server.js
@@ -122,6 +122,8 @@ function Server(options) {
this.preChain = [];
this.routes = [];
this.version = options.version || false;
+ this.responseTimeHeader = options.responseTimeHeader || 'X-Response-Time';
+ this.responseTimeFormatter = options.responseTimeFormatter;
var secure = false;
if (options.certificate && options.key) {
@@ -194,6 +196,10 @@ function Server(options) {
str += self.address().port;
return str;
});
+
+ this.__defineGetter__('responseTimeHeader', function () {
+ return options.responseTimeHeader || 'X-Response-Time';
+ });
}
util.inherits(Server, EventEmitter);
module.exports = Server;
@@ -435,7 +441,9 @@ Server.prototype._request = function _request(request, response, expect100) {
request: req,
response: response,
formatters: self.formatters,
- serverName: self.name
+ serverName: self.name,
+ responseTimeHeader: self.responseTimeHeader,
+ responseTimeFormatter: self.responseTimeFormatter,
});
logRequest(req);

0 comments on commit d3b6604

Please sign in to comment.