Skip to content

Commit 6d069bd

Browse files
committed
minor fixes for promises when using APM
1 parent bcc37c9 commit 6d069bd

File tree

2 files changed

+116
-47
lines changed

2 files changed

+116
-47
lines changed

lib/apm.js

Lines changed: 109 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,18 @@ var Instrumentation = function(core, options) {
132132

133133
// Get the callback
134134
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+
}
142147
}
143148
});
144149
});
@@ -174,14 +179,14 @@ var Instrumentation = function(core, options) {
174179
var ourOpId = operationIdGenerator.next();
175180
var parts = this.ns.split('.');
176181
var db = parts[0];
182+
177183
// Get the collection
178184
parts.shift();
179185
var collection = parts.join('.');
180186

181187
// If we have a find method, set the operationId on the cursor
182188
if(x == '_find') {
183-
this.operationId = ourOpId;
184-
this.startTime = new Date();
189+
cursor.operationId = ourOpId;
185190
}
186191

187192
// Set the command
@@ -242,6 +247,18 @@ var Instrumentation = function(core, options) {
242247
}
243248
}
244249

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+
245262
// Emit the start event for the command
246263
var command = {
247264
// Returns the command.
@@ -258,50 +275,96 @@ var Instrumentation = function(core, options) {
258275
// Returns the connection id for the command. For languages that do not have this,
259276
// this MUST return the driver equivalent which MUST include the server address and port.
260277
// 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
262279
};
263280

264-
// Emit the started event
265-
self.emit('started', command)
266-
267281
// Get the aruments
268282
var args = Array.prototype.slice.call(arguments, 0);
283+
269284
// Get the callback
270285
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-
}
298286

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+
}
305368
}
306369
});
307370
});

test/runner.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ var startupOptions = {
5050
, skip: false
5151
}
5252

53+
// var listener = require('../').instrument();
54+
// listener.on('started', function(event) {
55+
// // console.log("-------------------------------------------- started")
56+
// // console.log(JSON.stringify(event, null, 2))
57+
// });
58+
5359
/**
5460
* Standalone MongoDB Configuration
5561
*/
@@ -211,7 +217,7 @@ var createConfiguration = function(options) {
211217

212218
// Set up the runner
213219
var runner = new Runner({
214-
logLevel:'error',
220+
logLevel:'info',
215221
runners: 1,
216222
failFast: true
217223
});

0 commit comments

Comments
 (0)