Skip to content

Commit 4904bd0

Browse files
committed
trace_events: trace net connect event
1 parent 95d9140 commit 4904bd0

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
108108
Boolean::New(env->isolate(), writable)
109109
};
110110

111+
TRACE_EVENT_NESTABLE_ASYNC_END1(
112+
TRACING_CATEGORY_NODE2(net, native),
113+
"connect", req_wrap.get(), "status", status);
114+
111115
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
112116
}
113117

src/pipe_wrap.cc

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

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

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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
`;
28+
29+
tmpdir.refresh();
30+
const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');
31+
32+
const proc = cp.spawn(process.execPath,
33+
[ '--trace-events-enabled',
34+
'--trace-event-categories', 'node.net.native',
35+
'-e', CODE ],
36+
{ cwd: tmpdir.path });
37+
38+
proc.once('exit', common.mustCall(() => {
39+
assert(fs.existsSync(FILE_NAME));
40+
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
41+
const traces = JSON.parse(data.toString()).traceEvents;
42+
assert(traces.length > 0);
43+
let count = 0;
44+
traces.forEach((trace) => {
45+
if (trace.cat === 'node,node.net,node.net.native' &&
46+
trace.name === 'connect') {
47+
count++;
48+
}
49+
});
50+
// Two begin, two end
51+
assert.strictEqual(count, 4);
52+
}));
53+
}));

0 commit comments

Comments
 (0)