Skip to content

Commit

Permalink
Prevent HTTP resource actions from recording two timing metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
arobson committed Mar 9, 2016
1 parent fa92e3b commit efea4a9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
## 0.5.x

### Next
* Minor tweak to prevent posting two timing metrics for resource actions called via HTTP

### 0.5.8
* Add differentiated handle feature (to support adding versioned handlers in hyped)

### 0.5.7

* Fixed issue with data objects having no hasOwnProperty function
* Fixed issue with data objects having no hasOwnProperty function

### 0.5.6
* Provide a means to customize socket.io configuration
Expand Down
8 changes: 8 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var path = require( "path" );
var autohost = require( path.resolve( __dirname, "../src/index" ) );
var host = autohost( {
resources: path.resolve( __dirname, "./resource" ),
static: path.resolve( __dirname, "./public" )
} );

host.start();
8 changes: 8 additions & 0 deletions demo/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE=html>
<html>
<head>
</head>
<body>
<h3>Hello world!</h3>
</body>
</html>
14 changes: 14 additions & 0 deletions demo/resource/hello/resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = function() {
return {
name: "hello",
actions: {
default: {
url: "/",
method: "GET",
handle: function() {
return "hello world";
}
}
}
};
};
5 changes: 1 addition & 4 deletions src/http/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,7 @@ function wireupAction( state, resource, actionName, action, metadata, resources
req._metricKey = meta.metricKey;
req._resource = resource.name;
req._action = actionName;
var timer = meta.getTimer();
res.once( 'finish', function() {
timer.record( { name: 'HTTP_API_DURATION' } );
} );
req._timer = meta.getTimer();
action.handle = getHandler( action.handle );
respond( state, meta, req, res, resource, action );
} );
Expand Down
6 changes: 4 additions & 2 deletions src/http/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ function requestMetrics( state, req, res, next ) {
var sent = req.connection._bytesDispatched;
var sentKB = sent ? sent / 1024 : 0;
var url = req.url;
var elapsed = timer.record( { name: 'HTTP_REQUEST_DURATION' } );

var elapsed;
var metricKey = req._metricKey;
if ( metricKey ) {
var resourceRequests = state.metrics.meter( 'requests', 'count', metricKey );
Expand All @@ -117,13 +117,15 @@ function requestMetrics( state, req, res, next ) {
resourceRequests.record( 1, { name: 'HTTP_API_REQUESTS' } );
resourceIngress.record( read, { name: 'HTTP_API_INGRESS' } );
resourceEgress.record( sent, { name: 'HTTP_API_EGRESS' } );
elapsed = req._timer.record( { name: 'HTTP_API_DURATION' } );
} else {
var httpRequests = state.metrics.meter( [ urlKey, 'requests' ] );
var httpIngress = state.metrics.meter( [ urlKey, 'ingress' ], 'bytes' );
var httpEgress = state.metrics.meter( [ urlKey, 'egress' ], 'bytes' );
httpRequests.record( 1, { name: 'HTTP_REQUESTS' } );
httpIngress.record( read, { name: 'HTTP_INGRESS' } );
httpEgress.record( sent, { name: 'HTTP_EGRESS' } );
elapsed = timer.record( { name: 'HTTP_REQUEST_DURATION' } );
}

log.info( '%s@%s %s (%d ms) [%s] %s %s (%d bytes) %s %s (%d bytes)',
Expand Down

0 comments on commit efea4a9

Please sign in to comment.