@@ -132,13 +132,18 @@ var Instrumentation = function(core, options) {
132
132
133
133
// Get the callback
134
134
var callback = args . pop ( ) ;
135
- args . push ( function ( err , r ) {
136
- // Return to caller
137
- callback ( err , r ) ;
138
- } ) ;
139
-
140
- // Apply the call
141
- func . apply ( this , args ) ;
135
+ // If we have a callback use this
136
+ if ( typeof callback == 'function' ) {
137
+ args . push ( function ( err , r ) {
138
+ // Return to caller
139
+ callback ( err , r ) ;
140
+ } ) ;
141
+
142
+ // Apply the call
143
+ func . apply ( this , args ) ;
144
+ } else {
145
+ return func . apply ( this , args ) ;
146
+ }
142
147
}
143
148
} ) ;
144
149
} ) ;
@@ -174,14 +179,14 @@ var Instrumentation = function(core, options) {
174
179
var ourOpId = operationIdGenerator . next ( ) ;
175
180
var parts = this . ns . split ( '.' ) ;
176
181
var db = parts [ 0 ] ;
182
+
177
183
// Get the collection
178
184
parts . shift ( ) ;
179
185
var collection = parts . join ( '.' ) ;
180
186
181
187
// If we have a find method, set the operationId on the cursor
182
188
if ( x == '_find' ) {
183
- this . operationId = ourOpId ;
184
- this . startTime = new Date ( ) ;
189
+ cursor . operationId = ourOpId ;
185
190
}
186
191
187
192
// Set the command
@@ -242,6 +247,18 @@ var Instrumentation = function(core, options) {
242
247
}
243
248
}
244
249
250
+ // console.log("#############################################################")
251
+ // console.dir(this)
252
+
253
+ // Set up the connection
254
+ var connectionId = null ;
255
+ // Set local connection
256
+ if ( this . connection ) connectionId = this . connection ;
257
+ if ( ! connectionId && this . server && this . server . getConnection ) connectionId = this . server . getConnection ( ) ;
258
+
259
+ // var connections = this.connections();
260
+ // var connectionId = connections.length > 0 ? connections[0] : null;
261
+
245
262
// Emit the start event for the command
246
263
var command = {
247
264
// Returns the command.
@@ -258,50 +275,96 @@ var Instrumentation = function(core, options) {
258
275
// Returns the connection id for the command. For languages that do not have this,
259
276
// this MUST return the driver equivalent which MUST include the server address and port.
260
277
// The name of this field is flexible to match the object that is returned from the driver.
261
- connectionId : this . server . getConnection ( )
278
+ connectionId : connectionId
262
279
} ;
263
280
264
- // Emit the started event
265
- self . emit ( 'started' , command )
266
-
267
281
// Get the aruments
268
282
var args = Array . prototype . slice . call ( arguments , 0 ) ;
283
+
269
284
// Get the callback
270
285
var callback = args . pop ( ) ;
271
- args . push ( function ( err , r ) {
272
- if ( err ) {
273
- // Command
274
- var command = {
275
- duration : ( new Date ( ) . getTime ( ) - cursor . startTime . getTime ( ) ) ,
276
- commandName : commandTranslation [ x ] ,
277
- requestId : requestId ,
278
- operationId : ourOpId ,
279
- connectionId : cursor . server . getConnection ( ) ,
280
- failure : err } ;
281
-
282
- // Emit the command
283
- self . emit ( 'failed' , command )
284
- } else {
285
- // cursor id is zero, we can issue success command
286
- var command = {
287
- duration : ( new Date ( ) . getTime ( ) - cursor . startTime . getTime ( ) ) ,
288
- commandName : commandTranslation [ x ] ,
289
- requestId : requestId ,
290
- operationId : cursor . operationId ,
291
- connectionId : cursor . server . getConnection ( ) ,
292
- reply : cursor . cursorState . documents
293
- } ;
294
-
295
- // Emit the command
296
- self . emit ( 'succeeded' , command )
297
- }
298
286
299
- // Return to caller
300
- callback ( err , r ) ;
301
- } ) ;
302
-
303
- // Apply the call
304
- func . apply ( this , args ) ;
287
+ // We do not have a callback but a Promise
288
+ if ( typeof callback == 'function' ) {
289
+ var startTime = new Date ( ) ;
290
+ // Emit the started event
291
+ self . emit ( 'started' , command )
292
+ // Add our callback handler
293
+ args . push ( function ( err , r ) {
294
+ if ( err ) {
295
+ // Command
296
+ var command = {
297
+ duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
298
+ commandName : commandTranslation [ x ] ,
299
+ requestId : requestId ,
300
+ operationId : ourOpId ,
301
+ connectionId : cursor . server . getConnection ( ) ,
302
+ failure : err } ;
303
+
304
+ // Emit the command
305
+ self . emit ( 'failed' , command )
306
+ } else {
307
+ // cursor id is zero, we can issue success command
308
+ var command = {
309
+ duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
310
+ commandName : commandTranslation [ x ] ,
311
+ requestId : requestId ,
312
+ operationId : cursor . operationId ,
313
+ connectionId : cursor . server . getConnection ( ) ,
314
+ reply : cursor . cursorState . documents
315
+ } ;
316
+
317
+ // Emit the command
318
+ self . emit ( 'succeeded' , command )
319
+ }
320
+
321
+ // Return to caller
322
+ callback ( err , r ) ;
323
+ } ) ;
324
+
325
+ // Apply the call
326
+ func . apply ( this , args ) ;
327
+ } else {
328
+ // Assume promise, push back the missing value
329
+ args . push ( callback ) ;
330
+ // Get the promise
331
+ var promise = func . apply ( this , args ) ;
332
+ // Return a new promise
333
+ return new cursor . s . promiseLibrary ( function ( resolve , reject ) {
334
+ var startTime = new Date ( ) ;
335
+ // Emit the started event
336
+ self . emit ( 'started' , command )
337
+ // Execute the function
338
+ promise . then ( function ( r ) {
339
+ // cursor id is zero, we can issue success command
340
+ var command = {
341
+ duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
342
+ commandName : commandTranslation [ x ] ,
343
+ requestId : requestId ,
344
+ operationId : cursor . operationId ,
345
+ connectionId : cursor . server . getConnection ( ) ,
346
+ reply : cursor . cursorState . documents
347
+ } ;
348
+
349
+ // Emit the command
350
+ self . emit ( 'succeeded' , command )
351
+ } ) . catch ( function ( err ) {
352
+ // Command
353
+ var command = {
354
+ duration : ( new Date ( ) . getTime ( ) - startTime . getTime ( ) ) ,
355
+ commandName : commandTranslation [ x ] ,
356
+ requestId : requestId ,
357
+ operationId : ourOpId ,
358
+ connectionId : cursor . server . getConnection ( ) ,
359
+ failure : err } ;
360
+
361
+ // Emit the command
362
+ self . emit ( 'failed' , command )
363
+ // reject the promise
364
+ reject ( err ) ;
365
+ } ) ;
366
+ } ) ;
367
+ }
305
368
}
306
369
} ) ;
307
370
} ) ;
0 commit comments