UNPKG

3.96 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
29module.exports = function(obj) {
30 // server probe
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 // client error probe
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 // client probe
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