1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v18.20.1"> 7 <title>Trace events | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/tracing.html"> 12 <script async defer src="assets/api.js" type="text/javascript"></script> 13 14</head> 15<body class="alt apidoc" id="api-section-tracing"> 16 <div id="content" class="clearfix"> 17 <div id="column2" class="interior"> 18 <div id="intro" class="interior"> 19 <a href="/" title="Go back to the home page"> 20 Node.js 21 </a> 22 </div> 23 <ul> 24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 26</ul> 27<hr class="line"> 28<ul> 29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 33<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 38<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 39<li><a href="console.html" class="nav-console">Console</a></li> 40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 45<li><a href="dns.html" class="nav-dns">DNS</a></li> 46<li><a href="domain.html" class="nav-domain">Domain</a></li> 47<li><a href="errors.html" class="nav-errors">Errors</a></li> 48<li><a href="events.html" class="nav-events">Events</a></li> 49<li><a href="fs.html" class="nav-fs">File system</a></li> 50<li><a href="globals.html" class="nav-globals">Globals</a></li> 51<li><a href="http.html" class="nav-http">HTTP</a></li> 52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 53<li><a href="https.html" class="nav-https">HTTPS</a></li> 54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 55<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 60<li><a href="net.html" class="nav-net">Net</a></li> 61<li><a href="os.html" class="nav-os">OS</a></li> 62<li><a href="path.html" class="nav-path">Path</a></li> 63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 65<li><a href="process.html" class="nav-process">Process</a></li> 66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 68<li><a href="readline.html" class="nav-readline">Readline</a></li> 69<li><a href="repl.html" class="nav-repl">REPL</a></li> 70<li><a href="report.html" class="nav-report">Report</a></li> 71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 72<li><a href="stream.html" class="nav-stream">Stream</a></li> 73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 74<li><a href="test.html" class="nav-test">Test runner</a></li> 75<li><a href="timers.html" class="nav-timers">Timers</a></li> 76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 77<li><a href="tracing.html" class="nav-tracing active">Trace events</a></li> 78<li><a href="tty.html" class="nav-tty">TTY</a></li> 79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 80<li><a href="url.html" class="nav-url">URL</a></li> 81<li><a href="util.html" class="nav-util">Utilities</a></li> 82<li><a href="v8.html" class="nav-v8">V8</a></li> 83<li><a href="vm.html" class="nav-vm">VM</a></li> 84<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 89</ul> 90<hr class="line"> 91<ul> 92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 93</ul> 94 </div> 95 96 <div id="column1" data-id="tracing" class="interior"> 97 <header class="header"> 98 <div class="header-container"> 99 <h1>Node.js v18.20.1 documentation</h1> 100 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 101 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 102 <path fill="none" d="M0 0h24v24H0z" /> 103 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 104 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 105 </svg> 106 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 107 <path d="M0 0h24v24H0z" fill="none" /> 108 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 109 </svg> 110 </button> 111 </div> 112 <div id="gtoc"> 113 <ul> 114 <li class="pinned-header">Node.js v18.20.1</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 119 Table of contents 120 </a> 121 122 <div class="picker"><div class="toc"><ul> 123<li><span class="stability_1"><a href="#trace-events">Trace events</a></span> 124<ul> 125<li><a href="#the-nodetrace_events-module">The <code>node:trace_events</code> module</a> 126<ul> 127<li><a href="#tracing-object"><code>Tracing</code> object</a> 128<ul> 129<li><a href="#tracingcategories"><code>tracing.categories</code></a></li> 130<li><a href="#tracingdisable"><code>tracing.disable()</code></a></li> 131<li><a href="#tracingenable"><code>tracing.enable()</code></a></li> 132<li><a href="#tracingenabled"><code>tracing.enabled</code></a></li> 133</ul> 134</li> 135<li><a href="#trace_eventscreatetracingoptions"><code>trace_events.createTracing(options)</code></a></li> 136<li><a href="#trace_eventsgetenabledcategories"><code>trace_events.getEnabledCategories()</code></a></li> 137</ul> 138</li> 139<li><a href="#examples">Examples</a> 140<ul> 141<li><a href="#collect-trace-events-data-by-inspector">Collect trace events data by inspector</a></li> 142</ul> 143</li> 144</ul> 145</li> 146</ul></div></div> 147 </li> 148 149 150 <li class="picker-header"> 151 <a href="#"> 152 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 153 Index 154 </a> 155 156 <div class="picker"><ul> 157<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 158<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 159 160 <li> 161 <a href="index.html">Index</a> 162 </li> 163 </ul> 164 165<hr class="line"> 166<ul> 167<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 168<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 169<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 170<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 171<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 172<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 173<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 174<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 175<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 176<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 177<li><a href="console.html" class="nav-console">Console</a></li> 178<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 179<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 180<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 181<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 182<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 183<li><a href="dns.html" class="nav-dns">DNS</a></li> 184<li><a href="domain.html" class="nav-domain">Domain</a></li> 185<li><a href="errors.html" class="nav-errors">Errors</a></li> 186<li><a href="events.html" class="nav-events">Events</a></li> 187<li><a href="fs.html" class="nav-fs">File system</a></li> 188<li><a href="globals.html" class="nav-globals">Globals</a></li> 189<li><a href="http.html" class="nav-http">HTTP</a></li> 190<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 191<li><a href="https.html" class="nav-https">HTTPS</a></li> 192<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 193<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 194<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 195<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 196<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 197<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 198<li><a href="net.html" class="nav-net">Net</a></li> 199<li><a href="os.html" class="nav-os">OS</a></li> 200<li><a href="path.html" class="nav-path">Path</a></li> 201<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 202<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 203<li><a href="process.html" class="nav-process">Process</a></li> 204<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 205<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 206<li><a href="readline.html" class="nav-readline">Readline</a></li> 207<li><a href="repl.html" class="nav-repl">REPL</a></li> 208<li><a href="report.html" class="nav-report">Report</a></li> 209<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 210<li><a href="stream.html" class="nav-stream">Stream</a></li> 211<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 212<li><a href="test.html" class="nav-test">Test runner</a></li> 213<li><a href="timers.html" class="nav-timers">Timers</a></li> 214<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 215<li><a href="tracing.html" class="nav-tracing active">Trace events</a></li> 216<li><a href="tty.html" class="nav-tty">TTY</a></li> 217<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 218<li><a href="url.html" class="nav-url">URL</a></li> 219<li><a href="util.html" class="nav-util">Utilities</a></li> 220<li><a href="v8.html" class="nav-v8">V8</a></li> 221<li><a href="vm.html" class="nav-vm">VM</a></li> 222<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 223<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 224<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 225<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 226<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 227</ul> 228<hr class="line"> 229<ul> 230<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 231</ul></div> 232 </li> 233 234 235 <li class="picker-header"> 236 <a href="#"> 237 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 238 Other versions 239 </a> 240 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/tracing.html">21.x</a></li> 241<li><a href="https://nodejs.org/docs/latest-v20.x/api/tracing.html">20.x <b>LTS</b></a></li> 242<li><a href="https://nodejs.org/docs/latest-v19.x/api/tracing.html">19.x</a></li> 243<li><a href="https://nodejs.org/docs/latest-v18.x/api/tracing.html">18.x <b>LTS</b></a></li> 244<li><a href="https://nodejs.org/docs/latest-v17.x/api/tracing.html">17.x</a></li> 245<li><a href="https://nodejs.org/docs/latest-v16.x/api/tracing.html">16.x</a></li> 246<li><a href="https://nodejs.org/docs/latest-v15.x/api/tracing.html">15.x</a></li> 247<li><a href="https://nodejs.org/docs/latest-v14.x/api/tracing.html">14.x</a></li> 248<li><a href="https://nodejs.org/docs/latest-v13.x/api/tracing.html">13.x</a></li> 249<li><a href="https://nodejs.org/docs/latest-v12.x/api/tracing.html">12.x</a></li> 250<li><a href="https://nodejs.org/docs/latest-v11.x/api/tracing.html">11.x</a></li> 251<li><a href="https://nodejs.org/docs/latest-v10.x/api/tracing.html">10.x</a></li> 252<li><a href="https://nodejs.org/docs/latest-v9.x/api/tracing.html">9.x</a></li> 253<li><a href="https://nodejs.org/docs/latest-v8.x/api/tracing.html">8.x</a></li> 254<li><a href="https://nodejs.org/docs/latest-v7.x/api/tracing.html">7.x</a></li></ol></div> 255 </li> 256 257 <li class="picker-header"> 258 <a href="#"> 259 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 260 Options 261 </a> 262 263 <div class="picker"> 264 <ul> 265 <li> 266 <a href="all.html">View on single page</a> 267 </li> 268 <li> 269 <a href="tracing.json">View as JSON</a> 270 </li> 271 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/tracing.md">Edit on GitHub</a></li> 272 </ul> 273 </div> 274 </li> 275 </ul> 276 </div> 277 <hr> 278 </header> 279 280 <details id="toc" open><summary>Table of contents</summary><ul> 281<li><span class="stability_1"><a href="#trace-events">Trace events</a></span> 282<ul> 283<li><a href="#the-nodetrace_events-module">The <code>node:trace_events</code> module</a> 284<ul> 285<li><a href="#tracing-object"><code>Tracing</code> object</a> 286<ul> 287<li><a href="#tracingcategories"><code>tracing.categories</code></a></li> 288<li><a href="#tracingdisable"><code>tracing.disable()</code></a></li> 289<li><a href="#tracingenable"><code>tracing.enable()</code></a></li> 290<li><a href="#tracingenabled"><code>tracing.enabled</code></a></li> 291</ul> 292</li> 293<li><a href="#trace_eventscreatetracingoptions"><code>trace_events.createTracing(options)</code></a></li> 294<li><a href="#trace_eventsgetenabledcategories"><code>trace_events.getEnabledCategories()</code></a></li> 295</ul> 296</li> 297<li><a href="#examples">Examples</a> 298<ul> 299<li><a href="#collect-trace-events-data-by-inspector">Collect trace events data by inspector</a></li> 300</ul> 301</li> 302</ul> 303</li> 304</ul></details> 305 306 <div id="apicontent"> 307 <h2>Trace events<span><a class="mark" href="#trace-events" id="trace-events">#</a></span><a aria-hidden="true" class="legacy" id="tracing_trace_events"></a></h2> 308 309<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 310<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/trace_events.js">lib/trace_events.js</a></p> 311<p>The <code>node:trace_events</code> module provides a mechanism to centralize tracing 312information generated by V8, Node.js core, and userspace code.</p> 313<p>Tracing can be enabled with the <code>--trace-event-categories</code> command-line flag 314or by using the <code>node:trace_events</code> module. The <code>--trace-event-categories</code> flag 315accepts a list of comma-separated category names.</p> 316<p>The available categories are:</p> 317<ul> 318<li><code>node</code>: An empty placeholder.</li> 319<li><code>node.async_hooks</code>: Enables capture of detailed <a href="async_hooks.html"><code>async_hooks</code></a> trace data. 320The <a href="async_hooks.html"><code>async_hooks</code></a> events have a unique <code>asyncId</code> and a special <code>triggerId</code> 321<code>triggerAsyncId</code> property.</li> 322<li><code>node.bootstrap</code>: Enables capture of Node.js bootstrap milestones.</li> 323<li><code>node.console</code>: Enables capture of <code>console.time()</code> and <code>console.count()</code> 324output.</li> 325<li><code>node.threadpoolwork.sync</code>: Enables capture of trace data for threadpool 326synchronous operations, such as <code>blob</code>, <code>zlib</code>, <code>crypto</code> and <code>node_api</code>.</li> 327<li><code>node.threadpoolwork.async</code>: Enables capture of trace data for threadpool 328asynchronous operations, such as <code>blob</code>, <code>zlib</code>, <code>crypto</code> and <code>node_api</code>.</li> 329<li><code>node.dns.native</code>: Enables capture of trace data for DNS queries.</li> 330<li><code>node.net.native</code>: Enables capture of trace data for network.</li> 331<li><code>node.environment</code>: Enables capture of Node.js Environment milestones.</li> 332<li><code>node.fs.sync</code>: Enables capture of trace data for file system sync methods.</li> 333<li><code>node.fs_dir.sync</code>: Enables capture of trace data for file system sync 334directory methods.</li> 335<li><code>node.fs.async</code>: Enables capture of trace data for file system async methods.</li> 336<li><code>node.fs_dir.async</code>: Enables capture of trace data for file system async 337directory methods.</li> 338<li><code>node.perf</code>: Enables capture of <a href="perf_hooks.html">Performance API</a> measurements. 339<ul> 340<li><code>node.perf.usertiming</code>: Enables capture of only Performance API User Timing 341measures and marks.</li> 342<li><code>node.perf.timerify</code>: Enables capture of only Performance API timerify 343measurements.</li> 344</ul> 345</li> 346<li><code>node.promises.rejections</code>: Enables capture of trace data tracking the number 347of unhandled Promise rejections and handled-after-rejections.</li> 348<li><code>node.vm.script</code>: Enables capture of trace data for the <code>node:vm</code> module's 349<code>runInNewContext()</code>, <code>runInContext()</code>, and <code>runInThisContext()</code> methods.</li> 350<li><code>v8</code>: The <a href="v8.html">V8</a> events are GC, compiling, and execution related.</li> 351<li><code>node.http</code>: Enables capture of trace data for http request / response.</li> 352</ul> 353<p>By default the <code>node</code>, <code>node.async_hooks</code>, and <code>v8</code> categories are enabled.</p> 354<pre><code class="language-bash">node --trace-event-categories v8,node,node.async_hooks server.js</code> <button class="copy-button">copy</button></pre> 355<p>Prior versions of Node.js required the use of the <code>--trace-events-enabled</code> 356flag to enable trace events. This requirement has been removed. However, the 357<code>--trace-events-enabled</code> flag <em>may</em> still be used and will enable the 358<code>node</code>, <code>node.async_hooks</code>, and <code>v8</code> trace event categories by default.</p> 359<pre><code class="language-bash">node --trace-events-enabled 360 361<span class="hljs-comment"># is equivalent to</span> 362 363node --trace-event-categories v8,node,node.async_hooks</code> <button class="copy-button">copy</button></pre> 364<p>Alternatively, trace events may be enabled using the <code>node:trace_events</code> module:</p> 365<pre><code class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:trace_events'</span>); 366<span class="hljs-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">createTracing</span>({ <span class="hljs-attr">categories</span>: [<span class="hljs-string">'node.perf'</span>] }); 367tracing.<span class="hljs-title function_">enable</span>(); <span class="hljs-comment">// Enable trace event capture for the 'node.perf' category</span> 368 369<span class="hljs-comment">// do work</span> 370 371tracing.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Disable trace event capture for the 'node.perf' category</span></code> <button class="copy-button">copy</button></pre> 372<p>Running Node.js with tracing enabled will produce log files that can be opened 373in the <a href="https://www.chromium.org/developers/how-tos/trace-event-profiling-tool"><code>chrome://tracing</code></a> 374tab of Chrome.</p> 375<p>The logging file is by default called <code>node_trace.${rotation}.log</code>, where 376<code>${rotation}</code> is an incrementing log-rotation id. The filepath pattern can 377be specified with <code>--trace-event-file-pattern</code> that accepts a template 378string that supports <code>${rotation}</code> and <code>${pid}</code>:</p> 379<pre><code class="language-bash">node --trace-event-categories v8 --trace-event-file-pattern <span class="hljs-string">'${pid}-${rotation}.log'</span> server.js</code> <button class="copy-button">copy</button></pre> 380<p>To guarantee that the log file is properly generated after signal events like 381<code>SIGINT</code>, <code>SIGTERM</code>, or <code>SIGBREAK</code>, make sure to have the appropriate handlers 382in your code, such as:</p> 383<pre><code class="language-js">process.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <span class="hljs-keyword">function</span> <span class="hljs-title function_">onSigint</span>(<span class="hljs-params"></span>) { 384 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">info</span>(<span class="hljs-string">'Received SIGINT.'</span>); 385 process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">130</span>); <span class="hljs-comment">// Or applicable exit code depending on OS and signal</span> 386});</code> <button class="copy-button">copy</button></pre> 387<p>The tracing system uses the same time source 388as the one used by <code>process.hrtime()</code>. 389However the trace-event timestamps are expressed in microseconds, 390unlike <code>process.hrtime()</code> which returns nanoseconds.</p> 391<p>The features from this module are not available in <a href="worker_threads.html#class-worker"><code>Worker</code></a> threads.</p> 392<section><h3>The <code>node:trace_events</code> module<span><a class="mark" href="#the-nodetrace_events-module" id="the-nodetrace_events-module">#</a></span><a aria-hidden="true" class="legacy" id="tracing_the_node_trace_events_module"></a></h3> 393<div class="api_metadata"> 394<span>Added in: v10.0.0</span> 395</div> 396<h4><code>Tracing</code> object<span><a class="mark" href="#tracing-object" id="tracing-object">#</a></span><a aria-hidden="true" class="legacy" id="tracing_tracing_object"></a></h4> 397<div class="api_metadata"> 398<span>Added in: v10.0.0</span> 399</div> 400<p>The <code>Tracing</code> object is used to enable or disable tracing for sets of 401categories. Instances are created using the <code>trace_events.createTracing()</code> 402method.</p> 403<p>When created, the <code>Tracing</code> object is disabled. Calling the 404<code>tracing.enable()</code> method adds the categories to the set of enabled trace event 405categories. Calling <code>tracing.disable()</code> will remove the categories from the 406set of enabled trace event categories.</p> 407<h5><code>tracing.categories</code><span><a class="mark" href="#tracingcategories" id="tracingcategories">#</a></span><a aria-hidden="true" class="legacy" id="tracing_tracing_categories"></a></h5> 408<div class="api_metadata"> 409<span>Added in: v10.0.0</span> 410</div> 411<ul> 412<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 413</ul> 414<p>A comma-separated list of the trace event categories covered by this 415<code>Tracing</code> object.</p> 416<h5><code>tracing.disable()</code><span><a class="mark" href="#tracingdisable" id="tracingdisable">#</a></span><a aria-hidden="true" class="legacy" id="tracing_tracing_disable"></a></h5> 417<div class="api_metadata"> 418<span>Added in: v10.0.0</span> 419</div> 420<p>Disables this <code>Tracing</code> object.</p> 421<p>Only trace event categories <em>not</em> covered by other enabled <code>Tracing</code> objects 422and <em>not</em> specified by the <code>--trace-event-categories</code> flag will be disabled.</p> 423<pre><code class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:trace_events'</span>); 424<span class="hljs-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({ <span class="hljs-attr">categories</span>: [<span class="hljs-string">'node'</span>, <span class="hljs-string">'v8'</span>] }); 425<span class="hljs-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({ <span class="hljs-attr">categories</span>: [<span class="hljs-string">'node.perf'</span>, <span class="hljs-string">'node'</span>] }); 426t1.<span class="hljs-title function_">enable</span>(); 427t2.<span class="hljs-title function_">enable</span>(); 428 429<span class="hljs-comment">// Prints 'node,node.perf,v8'</span> 430<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(trace_events.<span class="hljs-title function_">getEnabledCategories</span>()); 431 432t2.<span class="hljs-title function_">disable</span>(); <span class="hljs-comment">// Will only disable emission of the 'node.perf' category</span> 433 434<span class="hljs-comment">// Prints 'node,v8'</span> 435<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(trace_events.<span class="hljs-title function_">getEnabledCategories</span>());</code> <button class="copy-button">copy</button></pre> 436<h5><code>tracing.enable()</code><span><a class="mark" href="#tracingenable" id="tracingenable">#</a></span><a aria-hidden="true" class="legacy" id="tracing_tracing_enable"></a></h5> 437<div class="api_metadata"> 438<span>Added in: v10.0.0</span> 439</div> 440<p>Enables this <code>Tracing</code> object for the set of categories covered by the 441<code>Tracing</code> object.</p> 442<h5><code>tracing.enabled</code><span><a class="mark" href="#tracingenabled" id="tracingenabled">#</a></span><a aria-hidden="true" class="legacy" id="tracing_tracing_enabled"></a></h5> 443<div class="api_metadata"> 444<span>Added in: v10.0.0</span> 445</div> 446<ul> 447<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> only if the <code>Tracing</code> object has been enabled.</li> 448</ul> 449<h4><code>trace_events.createTracing(options)</code><span><a class="mark" href="#trace_eventscreatetracingoptions" id="trace_eventscreatetracingoptions">#</a></span><a aria-hidden="true" class="legacy" id="tracing_trace_events_createtracing_options"></a></h4> 450<div class="api_metadata"> 451<span>Added in: v10.0.0</span> 452</div> 453<ul> 454<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 455<ul> 456<li><code>categories</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> An array of trace category names. Values included 457in the array are coerced to a string when possible. An error will be 458thrown if the value cannot be coerced.</li> 459</ul> 460</li> 461<li>Returns: <a href="tracing.html#tracing-object" class="type"><Tracing></a>.</li> 462</ul> 463<p>Creates and returns a <code>Tracing</code> object for the given set of <code>categories</code>.</p> 464<pre><code class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:trace_events'</span>); 465<span class="hljs-keyword">const</span> categories = [<span class="hljs-string">'node.perf'</span>, <span class="hljs-string">'node.async_hooks'</span>]; 466<span class="hljs-keyword">const</span> tracing = trace_events.<span class="hljs-title function_">createTracing</span>({ categories }); 467tracing.<span class="hljs-title function_">enable</span>(); 468<span class="hljs-comment">// do stuff</span> 469tracing.<span class="hljs-title function_">disable</span>();</code> <button class="copy-button">copy</button></pre> 470<h4><code>trace_events.getEnabledCategories()</code><span><a class="mark" href="#trace_eventsgetenabledcategories" id="trace_eventsgetenabledcategories">#</a></span><a aria-hidden="true" class="legacy" id="tracing_trace_events_getenabledcategories"></a></h4> 471<div class="api_metadata"> 472<span>Added in: v10.0.0</span> 473</div> 474<ul> 475<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 476</ul> 477<p>Returns a comma-separated list of all currently-enabled trace event 478categories. The current set of enabled trace event categories is determined 479by the <em>union</em> of all currently-enabled <code>Tracing</code> objects and any categories 480enabled using the <code>--trace-event-categories</code> flag.</p> 481<p>Given the file <code>test.js</code> below, the command 482<code>node --trace-event-categories node.perf test.js</code> will print 483<code>'node.async_hooks,node.perf'</code> to the console.</p> 484<pre><code class="language-js"><span class="hljs-keyword">const</span> trace_events = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:trace_events'</span>); 485<span class="hljs-keyword">const</span> t1 = trace_events.<span class="hljs-title function_">createTracing</span>({ <span class="hljs-attr">categories</span>: [<span class="hljs-string">'node.async_hooks'</span>] }); 486<span class="hljs-keyword">const</span> t2 = trace_events.<span class="hljs-title function_">createTracing</span>({ <span class="hljs-attr">categories</span>: [<span class="hljs-string">'node.perf'</span>] }); 487<span class="hljs-keyword">const</span> t3 = trace_events.<span class="hljs-title function_">createTracing</span>({ <span class="hljs-attr">categories</span>: [<span class="hljs-string">'v8'</span>] }); 488 489t1.<span class="hljs-title function_">enable</span>(); 490t2.<span class="hljs-title function_">enable</span>(); 491 492<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(trace_events.<span class="hljs-title function_">getEnabledCategories</span>());</code> <button class="copy-button">copy</button></pre> 493</section><section><h3>Examples<span><a class="mark" href="#examples" id="examples">#</a></span><a aria-hidden="true" class="legacy" id="tracing_examples"></a></h3> 494<h4>Collect trace events data by inspector<span><a class="mark" href="#collect-trace-events-data-by-inspector" id="collect-trace-events-data-by-inspector">#</a></span><a aria-hidden="true" class="legacy" id="tracing_collect_trace_events_data_by_inspector"></a></h4> 495<pre><code class="language-js"><span class="hljs-meta">'use strict'</span>; 496 497<span class="hljs-keyword">const</span> { <span class="hljs-title class_">Session</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'inspector'</span>); 498<span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Session</span>(); 499session.<span class="hljs-title function_">connect</span>(); 500 501<span class="hljs-keyword">function</span> <span class="hljs-title function_">post</span>(<span class="hljs-params">message, data</span>) { 502 <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> { 503 session.<span class="hljs-title function_">post</span>(message, data, <span class="hljs-function">(<span class="hljs-params">err, result</span>) =></span> { 504 <span class="hljs-keyword">if</span> (err) 505 <span class="hljs-title function_">reject</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Error</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(err))); 506 <span class="hljs-keyword">else</span> 507 <span class="hljs-title function_">resolve</span>(result); 508 }); 509 }); 510} 511 512<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">collect</span>(<span class="hljs-params"></span>) { 513 <span class="hljs-keyword">const</span> data = []; 514 session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'NodeTracing.dataCollected'</span>, <span class="hljs-function">(<span class="hljs-params">chunk</span>) =></span> data.<span class="hljs-title function_">push</span>(chunk)); 515 session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'NodeTracing.tracingComplete'</span>, <span class="hljs-function">() =></span> { 516 <span class="hljs-comment">// done</span> 517 }); 518 <span class="hljs-keyword">const</span> traceConfig = { <span class="hljs-attr">includedCategories</span>: [<span class="hljs-string">'v8'</span>] }; 519 <span class="hljs-keyword">await</span> <span class="hljs-title function_">post</span>(<span class="hljs-string">'NodeTracing.start'</span>, { traceConfig }); 520 <span class="hljs-comment">// do something</span> 521 <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> { 522 <span class="hljs-title function_">post</span>(<span class="hljs-string">'NodeTracing.stop'</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">() =></span> { 523 session.<span class="hljs-title function_">disconnect</span>(); 524 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data); 525 }); 526 }, <span class="hljs-number">1000</span>); 527} 528 529<span class="hljs-title function_">collect</span>();</code> <button class="copy-button">copy</button></pre></section> 530 <!-- API END --> 531 </div> 532 </div> 533 </div> 534</body> 535</html> 536