UNPKG

3.32 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2012 Dmitri Melikyan
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to permit
9 * persons to whom the Software is furnished to do so, subject to the
10 * following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25var nt = require('../nodetime');
26var proxy = require('../proxy');
27var samples = require('../samples');
28
29
30var internalCommands = [
31 '_executeQueryCommand',
32 '_executeInsertCommand',
33 '_executeUpdateCommand',
34 '_executeRemoveCommand'
35];
36
37var commandMap = {
38 '_executeQueryCommand': 'find',
39 '_executeInsertCommand': 'insert',
40 '_executeUpdateCommand': 'update',
41 '_executeRemoveCommand': 'remove'
42};
43
44module.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