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 | module.exports = function(obj) {
|
30 |
|
31 | proxy.before(obj.Server.prototype, ['on', 'addListener'], function(obj, args) {
|
32 | if(args[0] !== 'request') return;
|
33 |
|
34 | proxy.callback(args, -1, function(obj, args) {
|
35 | var req = args[0];
|
36 | var res = args[1];
|
37 | var time = samples.time("HTTP Server", req.url, true);
|
38 |
|
39 | proxy.after(res, 'end', function(obj, args) {
|
40 | if(!time.done()) return;
|
41 | if(nt.paused) return;
|
42 |
|
43 | samples.add(time, {'Type': 'HTTP',
|
44 | 'Method': req.method,
|
45 | 'URL': req.url,
|
46 | 'Request headers': req.headers,
|
47 | 'Status code': res.statusCode},
|
48 | samples.truncate(req.url));
|
49 | });
|
50 | });
|
51 | });
|
52 |
|
53 |
|
54 |
|
55 | proxy.after(obj, 'request', function(obj, args, ret) {
|
56 | var time = undefined;
|
57 | var trace = samples.stackTrace();
|
58 | var opts = args[0];
|
59 |
|
60 | proxy.before(ret, 'end', function(obj, args) {
|
61 | time = opts.__time__ = !opts.__time__ ? samples.time("HTTP Client", opts.method || 'GET') : undefined;
|
62 | });
|
63 |
|
64 | proxy.before(ret, ['on', 'addListener'], function(obj, args) {
|
65 | if(args[0] !== 'error') return;
|
66 |
|
67 | proxy.callback(args, -1, function(obj, args) {
|
68 | if(!time || !time.done()) return;
|
69 | if(nt.paused) return;
|
70 |
|
71 | var error = (args && args.length > 0) ? (args[0] ? args[0].message : undefined) : undefined;
|
72 | var obj = {'Type': 'HTTP',
|
73 | 'Method': opts.method,
|
74 | 'URL': (opts.hostname || opts.host) + (opts.port ? ':' + opts.port : '') + (opts.path || '/'),
|
75 | 'Request headers': opts.headers,
|
76 | 'Stack trace': trace,
|
77 | 'Error': error};
|
78 | samples.add(time, obj, 'HTTP Client: ' + obj.URL);
|
79 | });
|
80 | });
|
81 | });
|
82 |
|
83 |
|
84 |
|
85 | proxy.before(obj, 'request', function(obj, args) {
|
86 | var trace = samples.stackTrace();
|
87 | var opts = args[0];
|
88 |
|
89 | proxy.callback(args, -1, function(obj, args) {
|
90 | var res = args[0];
|
91 | proxy.before(res, ['on', 'addListener'], function(obj, args) {
|
92 | if(args[0] !== 'end') return;
|
93 |
|
94 | proxy.callback(args, -1, function(obj, args) {
|
95 | var time = opts.__time__;
|
96 | if(!time || !time.done()) return;
|
97 | if(nt.paused) return;
|
98 |
|
99 | var obj = {'Type': 'HTTP',
|
100 | 'Method': opts.method,
|
101 | 'URL': (opts.hostname || opts.host) + (opts.port ? ':' + opts.port : '') + (opts.path || '/'),
|
102 | 'Request headers': opts.headers,
|
103 | 'Response headers': res.headers,
|
104 | 'Status code': res.statusCode,
|
105 | 'Stack trace': trace};
|
106 | samples.add(time, obj, 'HTTP Client: ' + obj.URL);
|
107 | });
|
108 | });
|
109 | });
|
110 | });
|
111 | };
|
112 |
|
113 |
|