Skip to content

Commit

Permalink
feat(RPM): add average server response time
Browse files Browse the repository at this point in the history
The collector sends average response time with the existing
rpm metrics
  • Loading branch information
peteyycz authored and gergelyke committed Nov 17, 2015
1 parent 7bb687e commit ce3e36c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 16 additions & 4 deletions lib/providers/httpTransaction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function HttpTransaction(eventBus, options) {
this.traces = [];
this.rpmMetrics = {};
this.totalRequestCount = 0;
this.avgResponseTime = 0;
this.avgRequestCount = 0;

this.on(HttpTransaction.CLIENT_RECV, this.onClientReceive);
this.on(HttpTransaction.CLIENT_SEND, this.onClientSend);
Expand Down Expand Up @@ -160,9 +162,15 @@ HttpTransaction.prototype.onServerSend = function (data) {
}

this.collectStatusCode(trace);
this.avgResponseTime = this.calculateAvgResponseTime(trace.responseTime);
delete this.partials[data.id];
};

HttpTransaction.prototype.calculateAvgResponseTime = function (responseTime) {
return (this.avgResponseTime * this.avgRequestCount) +
responseTime / this.avgRequestCount++ || null;
};

HttpTransaction.prototype.collectStatusCode = function (trace) {
if (!this.rpmMetrics[trace.statusCode]) {
this.rpmMetrics[trace.statusCode] = 1;
Expand Down Expand Up @@ -209,13 +217,17 @@ HttpTransaction.prototype.report = function (data) {

HttpTransaction.prototype._sendRpm = function () {
debug('sending rpm metrics to the trace service');
if (Object.keys(this.rpmMetrics).length > 0) {
var dataBag = {};
dataBag.requests = cloneDeep(this.rpmMetrics);
dataBag.timestamp = new Date().toISOString();
if (Object.keys(this.rpmMetrics).length > 0 || this.avgResponseTime) {
var dataBag = {
requests: cloneDeep(this.rpmMetrics),
timestamp: new Date().toISOString(),
avgResponseTime: this.avgResponseTime
};

this.eventBus.emit(this.eventBus.RPM_METRICS, dataBag);
this.rpmMetrics = {};
this.avgRequestCount = 0;
this.avgResponseTime = 0;
}
};

Expand Down
8 changes: 6 additions & 2 deletions lib/providers/httpTransaction/wraps/http.Server.prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function wrapListener(listener, collector, config) {

return function (request, response) {
var mustCollect;
var serverRecieveTime;

var headers = request.headers;

Expand Down Expand Up @@ -42,12 +43,13 @@ function wrapListener(listener, collector, config) {
}

var method = request.method;
serverRecieveTime = microtime.now();

var collectorDataBag = {
id: requestId,
host: headers.host,
url: requestUrl.pathname,
time: microtime.now(),
time: serverRecieveTime,
method: method,
headers: headers
};
Expand All @@ -64,6 +66,7 @@ function wrapListener(listener, collector, config) {
* @method instrumentedFinish
*/
function instrumentedFinish () {
var responseTime = serverSendTime - serverRecieveTime;

var collectorDataBag = {
mustCollect: mustCollect,
Expand All @@ -72,7 +75,8 @@ function wrapListener(listener, collector, config) {
url: requestUrl.pathname,
time: serverSendTime,
headers: headers,
statusCode: response.statusCode
statusCode: response.statusCode,
responseTime: responseTime
};

// Collect request ended
Expand Down

0 comments on commit ce3e36c

Please sign in to comment.