Skip to content

Commit 3d532b7

Browse files
committed
trace_events: trace net connect event
1 parent 95d9140 commit 3d532b7

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

doc/api/tracing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The available categories are:
2323
* `node.console`: Enables capture of `console.time()` and `console.count()`
2424
output.
2525
* `node.dns.native`: Enables capture of trace data for DNS queries.
26+
* `node.net.native`: Enables capture of trace data for network.
2627
* `node.environment`: Enables capture of Node.js Environment milestones.
2728
* `node.fs.sync`: Enables capture of trace data for file system sync methods.
2829
* `node.perf`: Enables capture of [Performance API][] measurements.

src/connection_wrap.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
108108
Boolean::New(env->isolate(), writable)
109109
};
110110

111+
TRACE_EVENT_NESTABLE_ASYNC_END1(TRACING_CATEGORY_NODE2(net, native),
112+
"connect",
113+
req_wrap.get(),
114+
"status",
115+
status);
116+
111117
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
112118
}
113119

src/pipe_wrap.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
241241
*name,
242242
AfterConnect);
243243

244+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE2(net, native),
245+
"connect",
246+
req_wrap,
247+
"pipe_path",
248+
TRACE_STR_COPY(*name));
249+
244250
args.GetReturnValue().Set(0); // uv_pipe_connect() doesn't return errors.
245251
}
246252

src/tcp_wrap.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,18 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
335335
&wrap->handle_,
336336
reinterpret_cast<const sockaddr*>(&addr),
337337
AfterConnect);
338-
if (err)
338+
if (err) {
339339
delete req_wrap;
340+
} else {
341+
int port = args[2]->Uint32Value(env->context()).FromJust();
342+
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
343+
"connect",
344+
req_wrap,
345+
"ip",
346+
TRACE_STR_COPY(*ip_address),
347+
"port",
348+
port);
349+
}
340350
}
341351

342352
args.GetReturnValue().Set(err);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cp = require('child_process');
5+
const fs = require('fs');
6+
const path = require('path');
7+
const tmpdir = require('../common/tmpdir');
8+
9+
const CODE = `
10+
const net = require('net');
11+
{
12+
const server = net.createServer((socket) => {
13+
socket.destroy();
14+
server.close();
15+
}).listen('${common.PIPE}', () => {
16+
net.connect('${common.PIPE}');
17+
});
18+
}
19+
{
20+
const server = net.createServer((socket) => {
21+
socket.destroy();
22+
server.close();
23+
}).listen(0, () => {
24+
net.connect(server.address().port);
25+
});
26+
}
27+
// Make sure the data is flushed to file
28+
setTimeout(() => {}, 3000);
29+
`;
30+
31+
tmpdir.refresh();
32+
const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
33+
34+
const proc = cp.spawn(process.execPath,
35+
[ '--trace-events-enabled',
36+
'--trace-event-categories', 'node.net.native',
37+
'-e', CODE ],
38+
{ cwd: tmpdir.path });
39+
40+
proc.once('exit', common.mustCall(() => {
41+
assert(fs.existsSync(FILE_NAME));
42+
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
43+
const traces = JSON.parse(data.toString()).traceEvents;
44+
assert(traces.length > 0);
45+
let count = 0;
46+
traces.forEach((trace) => {
47+
if (trace.cat === 'node,node.net,node.net.native' &&
48+
trace.name === 'connect') {
49+
count++;
50+
}
51+
});
52+
// Two begin, two end
53+
assert.strictEqual(count, 4);
54+
}));
55+
}));

0 commit comments

Comments
 (0)