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 | var commands = [
|
30 | 'get',
|
31 | 'set',
|
32 | 'delete',
|
33 | 'add',
|
34 | 'replace',
|
35 | 'append',
|
36 | 'prepend',
|
37 | 'cas',
|
38 | 'increment',
|
39 | 'decrement',
|
40 | 'samples'
|
41 | ];
|
42 |
|
43 |
|
44 | var findCallback = function(args) {
|
45 | for(var i = 0; i < args.length; i++)
|
46 | if(typeof args[i] === 'function') return i;
|
47 | };
|
48 |
|
49 |
|
50 | module.exports = function(obj) {
|
51 |
|
52 |
|
53 | proxy.after(obj.Client.prototype, 'connect', function(obj, args, ret) {
|
54 | obj.__trace__ = samples.stackTrace();
|
55 | obj.__time__ = samples.time("Memcached", "connect");
|
56 | });
|
57 |
|
58 | proxy.before(obj.Client.prototype, 'on', function(obj, args) {
|
59 | var client = obj;
|
60 | var event = args[0];
|
61 | if(event !== 'connect' && event !== 'timeout' && event !== 'error') return;
|
62 |
|
63 | proxy.callback(args, -1, function(obj, args) {
|
64 | var time = client.__time__;
|
65 | if(!time || !time.done()) return;
|
66 | if(nt.paused) return;
|
67 |
|
68 | var error = undefined;
|
69 | if(event === 'timeout') {
|
70 | error = 'socket timeout';
|
71 | }
|
72 | else if(event === 'error') {
|
73 | error = args.length > 0 ? args[0].message : undefined;
|
74 | }
|
75 |
|
76 | var obj = {'Type': 'Memcached',
|
77 | 'Connection': {host: client.host, port: client.port},
|
78 | 'Command': 'connect',
|
79 | 'Stack trace': client.__trace__,
|
80 | 'Error': error};
|
81 |
|
82 | samples.add(time, obj, 'Memcached: ' + obj['Command']);
|
83 | });
|
84 | });
|
85 |
|
86 |
|
87 |
|
88 | commands.forEach(function(command) {
|
89 | proxy.before(obj.Client.prototype, command, function(obj, args) {
|
90 | var client = obj;
|
91 | var trace = samples.stackTrace();
|
92 | var params = args;
|
93 | var time = samples.time("Memcached", command);
|
94 |
|
95 |
|
96 | var pos = findCallback(args);
|
97 | if(pos == undefined) return;
|
98 |
|
99 | proxy.callback(args, pos, function(obj, args) {
|
100 | if(!time.done()) return;
|
101 | if(nt.paused) return;
|
102 |
|
103 | var error = (args && args.length > 0) ? (args[0] ? args[0].message : undefined) : undefined;
|
104 | var obj = {'Type': 'Memcached',
|
105 | 'Connection': {host: client.host, port: client.port},
|
106 | 'Command': command,
|
107 | 'Arguments': samples.truncate(params),
|
108 | 'Stack trace': trace,
|
109 | 'Error': error};
|
110 |
|
111 | samples.add(time, obj, 'Memcached: ' + obj['Command']);
|
112 | });
|
113 | });
|
114 | });
|
115 | };
|
116 |
|