Skip to content

Commit

Permalink
trace_events: trace net connect event
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
theanarkh authored and danielleadams committed Jul 26, 2022
1 parent 0783ddf commit 19e8876
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/api/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The available categories are:
* `node.console`: Enables capture of `console.time()` and `console.count()`
output.
* `node.dns.native`: Enables capture of trace data for DNS queries.
* `node.net.native`: Enables capture of trace data for network.
* `node.environment`: Enables capture of Node.js Environment milestones.
* `node.fs.sync`: Enables capture of trace data for file system sync methods.
* `node.perf`: Enables capture of [Performance API][] measurements.
Expand Down
6 changes: 6 additions & 0 deletions src/connection_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ void ConnectionWrap<WrapType, UVType>::AfterConnect(uv_connect_t* req,
Boolean::New(env->isolate(), writable)
};

TRACE_EVENT_NESTABLE_ASYNC_END1(TRACING_CATEGORY_NODE2(net, native),
"connect",
req_wrap.get(),
"status",
status);

req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
}

Expand Down
6 changes: 6 additions & 0 deletions src/pipe_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
*name,
AfterConnect);

TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE2(net, native),
"connect",
req_wrap,
"pipe_path",
TRACE_STR_COPY(*name));

args.GetReturnValue().Set(0); // uv_pipe_connect() doesn't return errors.
}

Expand Down
12 changes: 11 additions & 1 deletion src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,18 @@ void TCPWrap::Connect(const FunctionCallbackInfo<Value>& args,
&wrap->handle_,
reinterpret_cast<const sockaddr*>(&addr),
AfterConnect);
if (err)
if (err) {
delete req_wrap;
} else {
int port = args[2]->Uint32Value(env->context()).FromJust();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(TRACING_CATEGORY_NODE2(net, native),
"connect",
req_wrap,
"ip",
TRACE_STR_COPY(*ip_address),
"port",
port);
}
}

args.GetReturnValue().Set(err);
Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-trace-events-net.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');

const CODE = `
const net = require('net');
const socket = net.connect('${common.PIPE}');
socket.on('error', () => {});
const server = net.createServer((socket) => {
socket.destroy();
server.close();
}).listen(0, () => {
net.connect(server.address().port);
});
`;

tmpdir.refresh();
const FILE_NAME = path.join(tmpdir.path, 'node_trace.1.log');

const proc = cp.spawn(process.execPath,
[ '--trace-events-enabled',
'--trace-event-categories', 'node.net.native',
'-e', CODE ],
{ cwd: tmpdir.path });

proc.once('exit', common.mustCall(() => {
assert(fs.existsSync(FILE_NAME));
fs.readFile(FILE_NAME, common.mustCall((err, data) => {
const traces = JSON.parse(data.toString()).traceEvents;
assert(traces.length > 0);
let count = 0;
traces.forEach((trace) => {
if (trace.cat === 'node,node.net,node.net.native' &&
trace.name === 'connect') {
count++;
}
});
// Two begin, two end
assert.strictEqual(count, 4);
}));
}));

0 comments on commit 19e8876

Please sign in to comment.