From 3d532b71c37cf352719d47051744f5ea3498b56e Mon Sep 17 00:00:00 2001 From: theanarkh Date: Tue, 19 Jul 2022 23:08:16 +0800 Subject: [PATCH] trace_events: trace net connect event --- doc/api/tracing.md | 1 + src/connection_wrap.cc | 6 +++ src/pipe_wrap.cc | 6 +++ src/tcp_wrap.cc | 12 +++++- test/parallel/test-trace-events-net.js | 55 ++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-trace-events-net.js diff --git a/doc/api/tracing.md b/doc/api/tracing.md index 890938eb9e1396..7766412ed0b75a 100644 --- a/doc/api/tracing.md +++ b/doc/api/tracing.md @@ -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. diff --git a/src/connection_wrap.cc b/src/connection_wrap.cc index 300f7d316b8309..29815eee4381a0 100644 --- a/src/connection_wrap.cc +++ b/src/connection_wrap.cc @@ -108,6 +108,12 @@ void ConnectionWrap::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); } diff --git a/src/pipe_wrap.cc b/src/pipe_wrap.cc index da52f5cee01062..85daf4a1e618ae 100644 --- a/src/pipe_wrap.cc +++ b/src/pipe_wrap.cc @@ -241,6 +241,12 @@ void PipeWrap::Connect(const FunctionCallbackInfo& 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. } diff --git a/src/tcp_wrap.cc b/src/tcp_wrap.cc index 1f81094d32b829..53ecfd2b7abffb 100644 --- a/src/tcp_wrap.cc +++ b/src/tcp_wrap.cc @@ -335,8 +335,18 @@ void TCPWrap::Connect(const FunctionCallbackInfo& args, &wrap->handle_, reinterpret_cast(&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); diff --git a/test/parallel/test-trace-events-net.js b/test/parallel/test-trace-events-net.js new file mode 100644 index 00000000000000..5c09ff9d89ce3f --- /dev/null +++ b/test/parallel/test-trace-events-net.js @@ -0,0 +1,55 @@ +'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 server = net.createServer((socket) => { + socket.destroy(); + server.close(); + }).listen('${common.PIPE}', () => { + net.connect('${common.PIPE}'); + }); + } + { + const server = net.createServer((socket) => { + socket.destroy(); + server.close(); + }).listen(0, () => { + net.connect(server.address().port); + }); + } + // Make sure the data is flushed to file + setTimeout(() => {}, 3000); +`; + +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); + })); +}));