Skip to content

Commit 19e8876

Browse files
theanarkhdanielleadams
authored andcommittedJul 26, 2022
trace_events: trace net connect event
PR-URL: #43903 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 0783ddf commit 19e8876

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed
 

‎doc/api/tracing.md

+1
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

+6
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

+6
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

+11-1
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);
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
const socket = net.connect('${common.PIPE}');
12+
socket.on('error', () => {});
13+
const server = net.createServer((socket) => {
14+
socket.destroy();
15+
server.close();
16+
}).listen(0, () => {
17+
net.connect(server.address().port);
18+
});
19+
`;
20+
21+
tmpdir.refresh();
22+
const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
23+
24+
const proc = cp.spawn(process.execPath,
25+
[ '--trace-events-enabled',
26+
'--trace-event-categories', 'node.net.native',
27+
'-e', CODE ],
28+
{ cwd: tmpdir.path });
29+
30+
proc.once('exit', common.mustCall(() => {
31+
assert(fs.existsSync(FILE_NAME));
32+
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
33+
const traces = JSON.parse(data.toString()).traceEvents;
34+
assert(traces.length > 0);
35+
let count = 0;
36+
traces.forEach((trace) => {
37+
if (trace.cat === 'node,node.net,node.net.native' &&
38+
trace.name === 'connect') {
39+
count++;
40+
}
41+
});
42+
// Two begin, two end
43+
assert.strictEqual(count, 4);
44+
}));
45+
}));

0 commit comments

Comments
 (0)
Please sign in to comment.