|
| 1 | +var EventEmitter = require('events').EventEmitter, |
| 2 | + inherits = require('util').inherits; |
| 3 | + |
| 4 | +var basicOperationIdGenerator = { |
| 5 | + operationId: 1, |
| 6 | + |
| 7 | + next: function() { |
| 8 | + return this.operationId++; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +var Instrumentation = function(core, options) { |
| 13 | + options = options || {}; |
| 14 | + var operationIdGenerator = options.operationIdGenerator || basicOperationIdGenerator; |
| 15 | + // Extend with event emitter functionality |
| 16 | + EventEmitter.call(this); |
| 17 | + |
| 18 | + // --------------------------------------------------------- |
| 19 | + // |
| 20 | + // Server |
| 21 | + // |
| 22 | + // --------------------------------------------------------- |
| 23 | + |
| 24 | + // Reference |
| 25 | + var self = this; |
| 26 | + // Names of methods we need to wrap |
| 27 | + var methods = ['command']; |
| 28 | + // Prototype |
| 29 | + var proto = core.Server.prototype; |
| 30 | + // Core server method we are going to wrap |
| 31 | + methods.forEach(function(x) { |
| 32 | + var func = proto[x]; |
| 33 | + |
| 34 | + // The actual prototype |
| 35 | + proto[x] = function() { |
| 36 | + var requestId = core.Query.nextRequestId(); |
| 37 | + var ourOpId = operationIdGenerator.next(); |
| 38 | + // Get the aruments |
| 39 | + var args = Array.prototype.slice.call(arguments, 0); |
| 40 | + var ns = args[0]; |
| 41 | + var commandObj = args[1]; |
| 42 | + var keys = Object.keys(commandObj); |
| 43 | + var commandName = keys[0]; |
| 44 | + var db = ns.split('.')[0]; |
| 45 | + |
| 46 | + // Get a connection reference for this server instance |
| 47 | + var connection = this.s.pool.get() |
| 48 | + // Emit the start event for the command |
| 49 | + var command = { |
| 50 | + // Returns the command. |
| 51 | + command: commandObj, |
| 52 | + // Returns the database name. |
| 53 | + databaseName: db, |
| 54 | + // Returns the command name. |
| 55 | + commandName: commandName, |
| 56 | + // Returns the driver generated request id. |
| 57 | + requestId: requestId, |
| 58 | + // Returns the driver generated operation id. |
| 59 | + // This is used to link events together such as bulk write operations. OPTIONAL. |
| 60 | + operationId: ourOpId, |
| 61 | + // Returns the connection id for the command. For languages that do not have this, |
| 62 | + // this MUST return the driver equivalent which MUST include the server address and port. |
| 63 | + // The name of this field is flexible to match the object that is returned from the driver. |
| 64 | + connectionId: connection |
| 65 | + }; |
| 66 | + |
| 67 | + // Emit the started event |
| 68 | + self.emit('started', command) |
| 69 | + |
| 70 | + // Start time |
| 71 | + var startTime = new Date().getTime(); |
| 72 | + |
| 73 | + // Get the callback |
| 74 | + var callback = args.pop(); |
| 75 | + args.push(function(err, r) { |
| 76 | + var endTime = new Date().getTime(); |
| 77 | + var command = { |
| 78 | + duration: (endTime - startTime), |
| 79 | + commandName: commandName, |
| 80 | + requestId: requestId, |
| 81 | + operationId: ourOpId, |
| 82 | + connectionId: connection |
| 83 | + }; |
| 84 | + |
| 85 | + // If we have an error |
| 86 | + if(err) { |
| 87 | + command.failure = err; |
| 88 | + self.emit('failed', command); |
| 89 | + } else { |
| 90 | + command.reply = r; |
| 91 | + self.emit('succeeded', command); |
| 92 | + } |
| 93 | + |
| 94 | + // Return to caller |
| 95 | + callback(err, r); |
| 96 | + }); |
| 97 | + |
| 98 | + // Apply the call |
| 99 | + func.apply(this, args); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + // --------------------------------------------------------- |
| 104 | + // |
| 105 | + // Cursor |
| 106 | + // |
| 107 | + // --------------------------------------------------------- |
| 108 | + |
| 109 | + // Inject ourselves into the Cursor methods |
| 110 | + var methods = ['_find', '_getmore', '_killcursor']; |
| 111 | + var prototypes = [ |
| 112 | + require('./cursor').prototype, require('./command_cursor').prototype, |
| 113 | + require('./aggregation_cursor').prototype |
| 114 | + ] |
| 115 | + |
| 116 | + // Command name translation |
| 117 | + var commandTranslation = { |
| 118 | + '_find': 'find', '_getmore': 'getMore', '_killcursor': 'killCursors' |
| 119 | + } |
| 120 | + |
| 121 | + prototypes.forEach(function(proto) { |
| 122 | + // Core server method we are going to wrap |
| 123 | + methods.forEach(function(x) { |
| 124 | + var func = proto[x]; |
| 125 | + |
| 126 | + // The actual prototype |
| 127 | + proto[x] = function() { |
| 128 | + var cursor = this; |
| 129 | + var requestId = core.Query.nextRequestId(); |
| 130 | + var ourOpId = operationIdGenerator.next(); |
| 131 | + var db = this.ns.split('.')[0]; |
| 132 | + |
| 133 | + // If we have a find method, set the operationId on the cursor |
| 134 | + if(x == '_find') { |
| 135 | + this.operationId = ourOpId; |
| 136 | + this.startTime = new Date(); |
| 137 | + } |
| 138 | + |
| 139 | + // Emit the start event for the command |
| 140 | + var command = { |
| 141 | + // Returns the command. |
| 142 | + command: this.query, |
| 143 | + // Returns the database name. |
| 144 | + databaseName: db, |
| 145 | + // Returns the command name. |
| 146 | + commandName: commandTranslation[x], |
| 147 | + // Returns the driver generated request id. |
| 148 | + requestId: requestId, |
| 149 | + // Returns the driver generated operation id. |
| 150 | + // This is used to link events together such as bulk write operations. OPTIONAL. |
| 151 | + operationId: this.operationId, |
| 152 | + // Returns the connection id for the command. For languages that do not have this, |
| 153 | + // this MUST return the driver equivalent which MUST include the server address and port. |
| 154 | + // The name of this field is flexible to match the object that is returned from the driver. |
| 155 | + connectionId: this.server.getConnection() |
| 156 | + }; |
| 157 | + |
| 158 | + // Emit the started event |
| 159 | + self.emit('started', command) |
| 160 | + |
| 161 | + // Get the aruments |
| 162 | + var args = Array.prototype.slice.call(arguments, 0); |
| 163 | + // Get the callback |
| 164 | + var callback = args.pop(); |
| 165 | + args.push(function(err, r) { |
| 166 | + if(err) { |
| 167 | + // Command |
| 168 | + var command = { |
| 169 | + duration: (new Date().getTime() - cursor.startTime.getTime()), |
| 170 | + commandName: commandTranslation[x], |
| 171 | + requestId: requestId, |
| 172 | + operationId: ourOpId, |
| 173 | + connectionId: cursor.server.getConnection(), |
| 174 | + failure: err }; |
| 175 | + // Emit the command |
| 176 | + self.emit('failed', command) |
| 177 | + } else { //if(connect.Long.ZERO.equals(cursor.cursorState.cursorId)) { |
| 178 | + // cursor id is zero, we can issue success command |
| 179 | + var command = { |
| 180 | + duration: (new Date().getTime() - cursor.startTime.getTime()), |
| 181 | + commandName: commandTranslation[x], |
| 182 | + requestId: requestId, |
| 183 | + operationId: cursor.operationId, |
| 184 | + connectionId: cursor.server.getConnection() }; |
| 185 | + // Emit the command |
| 186 | + self.emit('succeeded', command) |
| 187 | + } |
| 188 | + |
| 189 | + // Return to caller |
| 190 | + callback(err, r); |
| 191 | + }); |
| 192 | + |
| 193 | + // Apply the call |
| 194 | + func.apply(this, args); |
| 195 | + } |
| 196 | + }); |
| 197 | + }); |
| 198 | +} |
| 199 | + |
| 200 | +inherits(Instrumentation, EventEmitter); |
| 201 | + |
| 202 | +module.exports = Instrumentation; |
0 commit comments