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 |
|
25 | var nt = require('../nodetime');
|
26 | var proxy = require('../proxy');
|
27 | var samples = require('../samples');
|
28 |
|
29 | module.exports = function(obj) {
|
30 | proxy.after(obj, 'createClient', function(obj, args, ret) {
|
31 | var client = ret;
|
32 |
|
33 | proxy.before(client, 'query', function(obj, args) {
|
34 | var trace = samples.stackTrace();
|
35 | var command = args.length > 0 ? args[0] : undefined;
|
36 | var params = args.length > 1 && Array.isArray(args[1]) ? args[1] : undefined;
|
37 | var time = samples.time("MySQL", "query");
|
38 |
|
39 | proxy.callback(args, -1, function(obj, args) {
|
40 | if(!time.done()) return;
|
41 | if(nt.paused) return;
|
42 |
|
43 | var error = (args && args.length > 0) ? (args[0] ? args[0].message : undefined) : undefined;
|
44 | var obj = {'Type': 'MySQL',
|
45 | 'Connection': {host: client.host, port: client.port, user: client.user, database: client.database !== '' ? client.database : undefined},
|
46 | 'Command': samples.truncate(command),
|
47 | 'Arguments': samples.truncate(params),
|
48 | 'Stack trace': trace,
|
49 | 'Error': error};
|
50 |
|
51 | samples.add(time, obj, 'MySQL: ' + obj['Command']);
|
52 | });
|
53 | });
|
54 | });
|
55 | };
|
56 |
|