1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var nt = require('../nodetime');
|
26 | var proxy = require('../proxy');
|
27 | var samples = require('../samples');
|
28 |
|
29 |
|
30 | var internalCommands = [
|
31 | '_executeQueryCommand',
|
32 | '_executeInsertCommand',
|
33 | '_executeUpdateCommand',
|
34 | '_executeRemoveCommand'
|
35 | ];
|
36 |
|
37 | var commandMap = {
|
38 | '_executeQueryCommand': 'find',
|
39 | '_executeInsertCommand': 'insert',
|
40 | '_executeUpdateCommand': 'update',
|
41 | '_executeRemoveCommand': 'remove'
|
42 | };
|
43 |
|
44 | module.exports = function(obj) {
|
45 | internalCommands.forEach(function(internalCommand) {
|
46 | proxy.before(obj.Db.prototype, internalCommand, function(obj, args) {
|
47 | var trace = samples.stackTrace();
|
48 | var command = (args && args.length > 0) ? args[0] : undefined;
|
49 | var time = samples.time("MongoDB", commandMap[internalCommand]);
|
50 |
|
51 | proxy.callback(args, -1, function(obj, args) {
|
52 | if(!time.done()) return;
|
53 | if(nt.paused) return;
|
54 |
|
55 | var conn = {};
|
56 | if(command.db) {
|
57 | var servers = command.db.serverConfig;
|
58 | if(servers) {
|
59 | if(Array.isArray(servers)) {
|
60 | conn.servers = [];
|
61 | servers.forEach(function(server) {
|
62 | conn.servers.push({host: server.host, port: server.port});
|
63 | });
|
64 | }
|
65 | else {
|
66 | conn.host = servers.host;
|
67 | conn.port = servers.port;
|
68 | }
|
69 | }
|
70 |
|
71 | conn.database = command.db.databaseName;
|
72 | }
|
73 |
|
74 | var commandName = commandMap[internalCommand];
|
75 | var query = command.query ? samples.truncate(JSON.stringify(command.query)) : '{}';
|
76 | var error = (args && args.length > 0) ? (args[0] ? args[0].message : undefined) : undefined;
|
77 | var obj = {'Type': 'MongoDB',
|
78 | 'Connection': conn,
|
79 | 'Command': {collectionName: command.collectionName,
|
80 | commandName: commandName,
|
81 | query: query,
|
82 | queryOptions: command.queryOptions,
|
83 | numberToSkip: command.numberToSkip,
|
84 | numberToReturn: command.numberToReturn},
|
85 | 'Stack trace': trace,
|
86 | 'Error': error};
|
87 |
|
88 | samples.add(time, obj, 'MongoDB: ' + commandName);
|
89 | });
|
90 | });
|
91 | });
|
92 | };
|
93 |
|