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 |
|
30 | module.exports = function(obj) {
|
31 |
|
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 |
|
42 | var probe = function(obj) {
|
43 | if(obj.__probeInstalled__) return;
|
44 | obj.__probeInstalled__ = true;
|
45 |
|
46 |
|
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 |
|
72 | proxy.after(obj, 'query', function(obj, args, ret) {
|
73 |
|
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 | };
|