Skip to content

Commit 101b892

Browse files
authored
Merge pull request #266 from DanCunnington/express_probe
Fix for express probe
2 parents 095ecbe + bbe729c commit 101b892

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Node Application Metrics provides the following built-in data collection sources
1717
Memory | Process and system memory usage
1818
GC | Node/V8 garbage collection statistics
1919
Event Loop | Event loop latency information
20-
Express | Express Web Framework application request monitoring
20+
Express | Express 4.x Web Framework application request monitoring
2121
Loop | Event loop timing metrics
2222
Function profiling | Node/V8 function profiling (disabled by default)
2323
HTTP | HTTP request calls made of the application
@@ -397,7 +397,7 @@ Emitted when a PostgreSQL query is made to the `pg` module.
397397
* `duration` (Number) the time taken for the PostgreSQL query to be responded to in ms.
398398

399399
### Event: 'express'
400-
Emitted when an express request finishes its response.
400+
Emitted when an express request finishes its response. Note. appmetrics has only been tested with express 4.x, support is not guaranteed for lower versions.
401401
* `data` (Object) the data from the Express request/response.
402402
* `method` (String) The HTTP method for this request.
403403
* `url` (String) The target URL for this request.

probes/express-probe.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,28 @@ ExpressProbe.prototype.attach = function(name, target) {
4141
var newTarget = aspect.afterConstructor(target, {});
4242

4343
// Map the application object to the newTarget
44-
newTarget = newTarget.application;
44+
if (newTarget.application) {
45+
newTarget = newTarget.application;
46+
47+
// Ensure we are only attaching the probe to this target once
48+
if (!newTarget.__ddProbeAttached__) {
49+
newTarget.__ddProbeAttached__ = true;
4550

46-
// Ensure we are only attaching the probe to this target once
47-
if (!newTarget.__ddProbeAttached__) {
48-
newTarget.__ddProbeAttached__ = true;
51+
// Before we make the call to an applicaton method
52+
aspect.before(newTarget, applicationMethods, function(target, methodName, methodArgs, probeData) {
53+
54+
// Patch the callback - i.e. the user's function when someone vists an application URL
55+
aspect.aroundCallback(methodArgs, probeData, function(target, args, probeData) {
56+
that.metricsProbeStart(probeData, methodName, methodArgs);
57+
that.requestProbeStart(probeData, methodName, methodArgs);
4958

50-
// Before we make the call to an applicaton method
51-
aspect.before(newTarget, applicationMethods, function(target, methodName, methodArgs, probeData) {
52-
53-
// Patch the callback - i.e. the user's function when someone vists an application URL
54-
aspect.aroundCallback(methodArgs, probeData, function(target, args, probeData) {
55-
that.metricsProbeStart(probeData, methodName, methodArgs);
56-
that.requestProbeStart(probeData, methodName, methodArgs);
57-
58-
}, function(target, args, probeData, ret) {
59-
methodArgs.statusCode = args[1].statusCode;
60-
that.metricsProbeEnd(probeData, methodName, methodArgs);
61-
that.requestProbeEnd(probeData, methodName, methodArgs);
59+
}, function(target, args, probeData, ret) {
60+
methodArgs.statusCode = args[1].statusCode;
61+
that.metricsProbeEnd(probeData, methodName, methodArgs);
62+
that.requestProbeEnd(probeData, methodName, methodArgs);
63+
});
6264
});
63-
});
65+
}
6466
}
6567
return target;
6668
}

0 commit comments

Comments
 (0)