1
1
var EventEmitter = require ( 'events' ) . EventEmitter ,
2
2
inherits = require ( 'util' ) . inherits ;
3
3
4
+ // Get prototypes
5
+ var AggregationCursor = require ( './aggregation_cursor' ) ,
6
+ CommandCursor = require ( './command_cursor' ) ,
7
+ OrderedBulkOperation = require ( './bulk/ordered' ) . OrderedBulkOperation ,
8
+ UnorderedBulkOperation = require ( './bulk/unordered' ) . UnorderedBulkOperation ,
9
+ GridStore = require ( './gridfs/grid_store' ) ,
10
+ Server = require ( './server' ) ,
11
+ ReplSet = require ( './replset' ) ,
12
+ Mongos = require ( './mongos' ) ,
13
+ Cursor = require ( './cursor' ) ,
14
+ Collection = require ( './collection' ) ,
15
+ Db = require ( './db' ) ,
16
+ Admin = require ( './admin' ) ;
17
+
4
18
var basicOperationIdGenerator = {
5
19
operationId : 1 ,
6
20
@@ -9,12 +23,58 @@ var basicOperationIdGenerator = {
9
23
}
10
24
}
11
25
12
- var Instrumentation = function ( core , options ) {
26
+ var basicTimestampGenerator = {
27
+ current : function ( ) {
28
+ return new Date ( ) . getTime ( ) ;
29
+ } ,
30
+ duration : function ( start , end ) {
31
+ return end - start ;
32
+ }
33
+ }
34
+
35
+ var Instrumentation = function ( core , options , callback ) {
13
36
options = options || { } ;
37
+
38
+ // Optional id generators
14
39
var operationIdGenerator = options . operationIdGenerator || basicOperationIdGenerator ;
40
+ // Optional timestamp generator
41
+ var timestampGenerator = options . timestampGenerator || basicTimestampGenerator ;
15
42
// Extend with event emitter functionality
16
43
EventEmitter . call ( this ) ;
17
44
45
+ // Contains all the instrumentation overloads
46
+ this . overloads = [ ] ;
47
+
48
+ // ---------------------------------------------------------
49
+ //
50
+ // Instrument prototype
51
+ //
52
+ // ---------------------------------------------------------
53
+
54
+ var instrumentPrototype = function ( callback ) {
55
+ var instrumentations = [ ]
56
+
57
+ // Classes to support
58
+ var classes = [ GridStore , Server , ReplSet , Mongos ,
59
+ OrderedBulkOperation , UnorderedBulkOperation , CommandCursor , AggregationCursor ,
60
+ Cursor , Collection , Db ] ;
61
+
62
+ // Add instrumentations to the available list
63
+ for ( var i = 0 ; i < classes . length ; i ++ ) {
64
+ if ( classes [ i ] . define ) {
65
+ instrumentations . push ( classes [ i ] . define . generate ( ) ) ;
66
+ }
67
+ }
68
+
69
+ // Return the list of instrumentation points
70
+ callback ( null , instrumentations ) ;
71
+ }
72
+
73
+ // Did the user want to instrument the prototype
74
+ if ( typeof callback == 'function' ) {
75
+ instrumentPrototype ( callback ) ;
76
+ }
77
+
18
78
// ---------------------------------------------------------
19
79
//
20
80
// Server
@@ -31,6 +91,9 @@ var Instrumentation = function(core, options) {
31
91
methods . forEach ( function ( x ) {
32
92
var func = proto [ x ] ;
33
93
94
+ // Add to overloaded methods
95
+ self . overloads . push ( { proto : proto , name :x , func :func } ) ;
96
+
34
97
// The actual prototype
35
98
proto [ x ] = function ( ) {
36
99
var requestId = core . Query . nextRequestId ( ) ;
@@ -50,6 +113,7 @@ var Instrumentation = function(core, options) {
50
113
51
114
// Get a connection reference for this server instance
52
115
var connection = this . s . pool . get ( )
116
+
53
117
// Emit the start event for the command
54
118
var command = {
55
119
// Returns the command.
@@ -73,13 +137,13 @@ var Instrumentation = function(core, options) {
73
137
self . emit ( 'started' , command )
74
138
75
139
// Start time
76
- var startTime = new Date ( ) . getTime ( ) ;
140
+ var startTime = timestampGenerator . current ( ) ;
77
141
78
142
// Push our handler callback
79
143
args . push ( function ( err , r ) {
80
- var endTime = new Date ( ) . getTime ( ) ;
144
+ var endTime = timestampGenerator . current ( ) ;
81
145
var command = {
82
- duration : ( endTime - startTime ) ,
146
+ duration : timestampGenerator . duration ( startTime , endTime ) ,
83
147
commandName : commandName ,
84
148
requestId : requestId ,
85
149
operationId : ourOpId ,
@@ -122,6 +186,9 @@ var Instrumentation = function(core, options) {
122
186
methods . forEach ( function ( x ) {
123
187
var func = proto [ x ] ;
124
188
189
+ // Add to overloaded methods
190
+ self . overloads . push ( { proto : proto , name :x , func :func } ) ;
191
+
125
192
// The actual prototype
126
193
proto [ x ] = function ( ) {
127
194
var bulk = this ;
@@ -168,10 +235,14 @@ var Instrumentation = function(core, options) {
168
235
}
169
236
170
237
prototypes . forEach ( function ( proto ) {
238
+
171
239
// Core server method we are going to wrap
172
240
methods . forEach ( function ( x ) {
173
241
var func = proto [ x ] ;
174
242
243
+ // Add to overloaded methods
244
+ self . overloads . push ( { proto : proto , name :x , func :func } ) ;
245
+
175
246
// The actual prototype
176
247
proto [ x ] = function ( ) {
177
248
var cursor = this ;
@@ -247,18 +318,12 @@ var Instrumentation = function(core, options) {
247
318
}
248
319
}
249
320
250
- // console.log("#############################################################")
251
- // console.dir(this)
252
-
253
321
// Set up the connection
254
322
var connectionId = null ;
255
323
// Set local connection
256
324
if ( this . connection ) connectionId = this . connection ;
257
325
if ( ! connectionId && this . server && this . server . getConnection ) connectionId = this . server . getConnection ( ) ;
258
326
259
- // var connections = this.connections();
260
- // var connectionId = connections.length > 0 ? connections[0] : null;
261
-
262
327
// Emit the start event for the command
263
328
var command = {
264
329
// Returns the command.
@@ -286,15 +351,15 @@ var Instrumentation = function(core, options) {
286
351
287
352
// We do not have a callback but a Promise
288
353
if ( typeof callback == 'function' ) {
289
- var startTime = new Date ( ) ;
354
+ var startTime = timestampGenerator . current ( ) ;
290
355
// Emit the started event
291
356
self . emit ( 'started' , command )
292
357
// Add our callback handler
293
358
args . push ( function ( err , r ) {
294
359
if ( err ) {
295
360
// Command
296
361
var command = {
297
- duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
362
+ duration : timestampGenerator . duration ( startTime , timestampGenerator . current ( ) ) ,
298
363
commandName : commandTranslation [ x ] ,
299
364
requestId : requestId ,
300
365
operationId : ourOpId ,
@@ -306,7 +371,7 @@ var Instrumentation = function(core, options) {
306
371
} else {
307
372
// cursor id is zero, we can issue success command
308
373
var command = {
309
- duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
374
+ duration : timestampGenerator . duration ( startTime , timestampGenerator . current ( ) ) ,
310
375
commandName : commandTranslation [ x ] ,
311
376
requestId : requestId ,
312
377
operationId : cursor . operationId ,
@@ -331,14 +396,14 @@ var Instrumentation = function(core, options) {
331
396
var promise = func . apply ( this , args ) ;
332
397
// Return a new promise
333
398
return new cursor . s . promiseLibrary ( function ( resolve , reject ) {
334
- var startTime = new Date ( ) ;
399
+ var startTime = timestampGenerator . current ( ) ;
335
400
// Emit the started event
336
401
self . emit ( 'started' , command )
337
402
// Execute the function
338
403
promise . then ( function ( r ) {
339
404
// cursor id is zero, we can issue success command
340
405
var command = {
341
- duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
406
+ duration : timestampGenerator . duration ( startTime , timestampGenerator . current ( ) ) ,
342
407
commandName : commandTranslation [ x ] ,
343
408
requestId : requestId ,
344
409
operationId : cursor . operationId ,
@@ -351,7 +416,7 @@ var Instrumentation = function(core, options) {
351
416
} ) . catch ( function ( err ) {
352
417
// Command
353
418
var command = {
354
- duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
419
+ duration : timestampGenerator . duration ( startTime , timestampGenerator . current ( ) ) ,
355
420
commandName : commandTranslation [ x ] ,
356
421
requestId : requestId ,
357
422
operationId : ourOpId ,
@@ -372,4 +437,11 @@ var Instrumentation = function(core, options) {
372
437
373
438
inherits ( Instrumentation , EventEmitter ) ;
374
439
375
- module . exports = Instrumentation ;
440
+ Instrumentation . prototype . uninstrument = function ( ) {
441
+ for ( var i = 0 ; i < this . overloads . length ; i ++ ) {
442
+ var obj = this . overloads [ i ] ;
443
+ obj . proto [ obj . name ] = obj . func ;
444
+ }
445
+ }
446
+
447
+ module . exports = Instrumentation ;
0 commit comments