Skip to content

Commit b2c9f9e

Browse files
authored
Merge pull request #279 from kenspirit/master
Add collection name and method to emitted data for MongoDB
2 parents 5b689b8 + 569083d commit b2c9f9e

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ Emitted when a MongoDB query is made using the `mongodb` module.
297297
* `time` (Number) the milliseconds when the MongoDB query was made. This can be converted to a Date using `new Date(data.time)`
298298
* `query` (String) the query made of the MongoDB database.
299299
* `duration` (Number) the time taken for the MongoDB query to be responded to in ms.
300+
* `method` (String) the executed method for the query, such as find, update.
301+
* `collection` (String) the MongoDB collection name.
300302

301303
### Event: 'mqtt'
302304
Emitted when a MQTT message is sent or received.
@@ -479,4 +481,4 @@ Non-release versions of this project (for example on github.com/RuntimeTools/app
479481
[3]:https://github.com/RuntimeTools/appmetrics/wiki
480482
[4]:https://docs.npmjs.com/files/folders
481483
[5]:https://github.com/RuntimeTools/appmetrics/issues
482-
[6]:https://github.com/RuntimeTools/appmetrics
484+
[6]:https://github.com/RuntimeTools/appmetrics

probes/mongo-probe.js

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ var util = require('util');
2020
var am = require('../');
2121

2222
function MongoProbe() {
23-
Probe.call(this, 'mongodb');
23+
Probe.call(this, 'mongodb');
2424
}
2525
util.inherits(MongoProbe, Probe);
2626

2727
MongoProbe.prototype.aspectCollectionMethod = function(coll, method) {
28-
var that = this;
28+
var that = this;
2929
var req;
3030
aspect.around( coll, method,
3131
function(target, methodName, methodArgs, probeData) {
32-
that.metricsProbeStart(probeData, target, method, methodArgs);
33-
that.requestProbeStart(probeData, target, method, methodArgs);
32+
var collectionName = target.collectionName;
33+
34+
that.metricsProbeStart(probeData, target, method, methodArgs);
35+
that.requestProbeStart(probeData, target, method, methodArgs);
3436
if (aspect.findCallbackArg(methodArgs) != undefined) {
3537
aspect.aroundCallback( methodArgs, probeData, function(target,args, probeData){
3638

@@ -39,24 +41,26 @@ MongoProbe.prototype.aspectCollectionMethod = function(coll, method) {
3941
if (typeof(callbackPosition) != 'undefined') {
4042
aspect.strongTraceTransactionLink('mongodb: ', methodName, methodArgs[callbackPosition]);
4143
}
42-
43-
that.metricsProbeEnd(probeData, method, methodArgs);
44-
that.requestProbeEnd(probeData, method, methodArgs);
44+
45+
that.metricsProbeEnd(probeData, collectionName, method, methodArgs);
46+
that.requestProbeEnd(probeData, method, methodArgs);
4547
} );
4648
}
47-
},
49+
},
4850
function(target, methodName, methodArgs, probeData, rc) {
51+
var collectionName = target.collectionName;
52+
4953
if (aspect.findCallbackArg(methodArgs) == undefined) {
50-
that.metricsProbeEnd(probeData, method, methodArgs);
51-
that.requestProbeEnd(probeData, method, methodArgs);
54+
that.metricsProbeEnd(probeData, collectionName, method, methodArgs);
55+
that.requestProbeEnd(probeData, method, methodArgs);
5256
}
5357
return rc;
54-
}
58+
}
5559
);
5660
}
5761

5862
MongoProbe.prototype.attach = function(name, target) {
59-
var that = this;
63+
var that = this;
6064
if( name != "mongodb" ) return target;
6165
if(target.__ddProbeAttached__) return target;
6266
target.__ddProbeAttached__ = true;
@@ -65,18 +69,20 @@ MongoProbe.prototype.attach = function(name, target) {
6569
var method = 'find';
6670
aspect.around( coll, "find",
6771
function(target, methodName, methodArgs, probeData){
68-
that.metricsProbeStart(probeData, target, method, methodArgs);
69-
that.requestProbeStart(probeData, target, method, methodArgs);
70-
},
72+
that.metricsProbeStart(probeData, target, method, methodArgs);
73+
that.requestProbeStart(probeData, target, method, methodArgs);
74+
},
7175
function(target, methodName, findArgs, probeData, rc){
76+
var collectionName = target.collectionName;
77+
7278
if (rc == undefined) {
73-
that.metricsProbeEnd(probeData, method, findArgs);
74-
that.requestProbeEnd(probeData, method, findArgs);
79+
that.metricsProbeEnd(probeData, collectionName, method, findArgs);
80+
that.requestProbeEnd(probeData, method, findArgs);
7581
} else {
7682
aspect.before( rc, "toArray", function(target, methodName, args, context){
7783
aspect.aroundCallback( args, probeData, function(target, args, probeData){
78-
that.metricsProbeEnd(probeData, method, findArgs);
79-
that.requestProbeEnd(probeData, method, findArgs);
84+
that.metricsProbeEnd(probeData, collectionName, method, findArgs);
85+
that.requestProbeEnd(probeData, method, findArgs);
8086
});
8187
});
8288
}
@@ -101,24 +107,27 @@ MongoProbe.prototype.attach = function(name, target) {
101107
* Lightweight metrics probe for MongoDB queries
102108
*
103109
* These provide:
104-
* time: time event started
105-
* query: the query itself
106-
* duration: the time for the request to respond
110+
* time: time event started
111+
* query: the query itself
112+
* duration: the time for the request to respond
113+
* method: the executed method for the query, such as find, update
114+
* collection: the mongo collection
107115
*/
108-
MongoProbe.prototype.metricsEnd = function(probeData, method, methodArgs) {
109-
probeData.timer.stop();
110-
am.emit('mongo', {time: probeData.timer.startTimeMillis, query: JSON.stringify(methodArgs[0]), duration: probeData.timer.timeDelta});
116+
MongoProbe.prototype.metricsEnd = function(probeData, collectionName, method, methodArgs) {
117+
probeData.timer.stop();
118+
am.emit('mongo', {time: probeData.timer.startTimeMillis, query: JSON.stringify(methodArgs[0]), duration: probeData.timer.timeDelta,
119+
method: method, collection: collectionName});
111120
};
112121

113122
/*
114123
* Heavyweight request probes for MongoDB queries
115124
*/
116125
MongoProbe.prototype.requestStart = function (probeData, target, method, methodArgs) {
117-
probeData.req = request.startRequest( 'DB', method + "("+target.collectionName+")", false, probeData.timer );
126+
probeData.req = request.startRequest( 'DB', method + "("+target.collectionName+")", false, probeData.timer );
118127
};
119128

120129
MongoProbe.prototype.requestEnd = function (probeData, method, methodArgs) {
121-
probeData.req.stop( { query: JSON.stringify(methodArgs[0]) } );
130+
probeData.req.stop( { query: JSON.stringify(methodArgs[0]) } );
122131
};
123132

124-
module.exports = MongoProbe;
133+
module.exports = MongoProbe;

0 commit comments

Comments
 (0)