UNPKG

3.57 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
29var 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
44var findCallback = function(args) {
45 for(var i = 0; i < args.length; i++)
46 if(typeof args[i] === 'function') return i;
47};
48
49
50module.exports = function(obj) {
51
52 // connect
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 // commands
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 // there might be args after callback, need to do extra callback search
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