UNPKG

3.6 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
30module.exports = function(obj) {
31 // connect
32 [obj.Connection.prototype, obj.PooledConnection.prototype].forEach(function(proto) {
33 proxy.before(proto, 'connect', function(obj, args) {
34 var client = obj;
35 var trace = samples.stackTrace();
36 var time = samples.time("Cassandra", "connect");
37
38 proxy.callback(args, -1, function(obj, args) {
39 if(!time.done()) return;
40 if(nt.paused) return;
41
42 var error = args.length > 0 ? (args[0] ? args[0].message : undefined) : undefined;
43 var obj = {'Type': 'Cassandra',
44 'Connection': connection(client),
45 'Command': 'connect',
46 'Stack trace': trace,
47 'Error': error};
48
49 samples.add(time, obj, 'Cassandra: ' + obj['Command']);
50 });
51 });
52 });
53
54
55 // execute
56 [obj.Connection.prototype, obj.PooledConnection.prototype].forEach(function(proto) {
57 proxy.before(proto, 'execute', function(obj, args) {
58 var client = obj;
59 var trace = samples.stackTrace();
60 var command = args.length > 0 ? args[0] : undefined;
61 var params = args.length > 1 && Array.isArray(args[1]) ? args[1] : undefined;
62 var time = samples.time("Cassandra", "execute");
63
64 proxy.callback(args, -1, function(obj, args) {
65 if(!time.done()) return;
66 if(nt.paused) return;
67
68 var error = args.length > 0 ? (args[0] ? args[0].message : undefined) : undefined;
69 var obj = {'Type': 'Cassandra',
70 'Connection': connection(client),
71 'Command': command,
72 'Arguments': samples.truncate(params),
73 'Stack trace': trace,
74 'Error': error};
75 samples.add(time, obj, 'Cassandra: ' + obj['Command']);
76 });
77 });
78 });
79};
80
81
82var connection = function(client) {
83 var connection = undefined;
84
85 if(client.connectionInfo) {
86 connection = {
87 host: client.connectionInfo.host,
88 port: client.connectionInfo.port,
89 keyspace: client.connectionInfo.keyspace,
90 user: client.connectionInfo.user
91 };
92 }
93 else if(client.connections && client.connections.length > 0) {
94 connection = [];
95 conn.connections.forEach(function(conn) {
96 connection.push({
97 host: conn.host,
98 port: conn.port,
99 keyspace: conn.keyspace,
100 user: conn.user
101 });
102 });
103 }
104
105 return connection;
106};
107