UNPKG

3.75 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 // Native, reinitialize probe
32 proxy.getter(obj, 'native', function(obj, ret) {
33 proxy.after(ret, 'Client', function(obj, args, ret) {
34 probe(ret.__proto__);
35 });
36 });
37
38 probe(obj.Client.prototype);
39};
40
41
42var probe = function(obj) {
43 if(obj.__probeInstalled__) return;
44 obj.__probeInstalled__ = true;
45
46 // Callback API
47 proxy.before(obj, 'query', function(obj, args, ret) {
48 var client = obj;
49 var trace = samples.stackTrace();
50 var command = args.length > 0 ? args[0] : undefined;
51 var params = args.length > 1 && Array.isArray(args[1]) ? args[1] : undefined;
52 var time = samples.time("PostgreSQL", "query");
53
54 proxy.callback(args, -1, function(obj, args) {
55 if(!time.done()) return;
56 if(nt.paused) return;
57
58 var error = args.length > 0 && args[0] ? args[0].message : undefined;
59 var obj = {'Type': 'PostgreSQL',
60 'Connection': {host: client.host, port: client.port, user: client.user, database: client.database ? client.database : undefined},
61 'Command': samples.truncate(command),
62 'Arguments': samples.truncate(params),
63 'Stack trace': trace,
64 'Error': error};
65
66 samples.add(time, obj, 'PostgreSQL: ' + obj['Command']);
67 });
68 });
69
70
71 // Evented API
72 proxy.after(obj, 'query', function(obj, args, ret) {
73 // If has a callback, ignore
74 if(args.length > 0 && typeof args[args.length - 1] === 'function') return;
75
76 var client = obj;
77 var trace = samples.stackTrace();
78 var command = args.length > 0 ? args[0] : undefined;
79 var params = args.length > 1 && Array.isArray(args[1]) ? args[1] : undefined;
80 var time = samples.time("PostgreSQL", "query");
81
82 proxy.before(ret, 'on', function(obj, args) {
83 var event = args[0];
84 if(event !== 'end' && event !== 'error') return;
85
86 proxy.callback(args, -1, function(obj, args) {
87 if(!time.done()) return;
88 if(nt.paused) return;
89
90 var error = (event === 'error' && args.length > 0) ? args[0].message : undefined;
91 var obj = {'Type': 'PostgreSQL',
92 'Connection': {host: client.host, port: client.port, user: client.user, database: client.database ? client.database : undefined},
93 'Command': samples.truncate(command),
94 'Params': samples.truncate(params),
95 'Stack trace': trace,
96 'Error': error};
97
98 samples.add(time, obj, 'PostgreSQL: ' + obj['Command']);
99 });
100 });
101 });
102
103
104};