11cb0ef41Sopenharmony_ci{ 21cb0ef41Sopenharmony_ci "type": "module", 31cb0ef41Sopenharmony_ci "source": "doc/api/tracing.md", 41cb0ef41Sopenharmony_ci "modules": [ 51cb0ef41Sopenharmony_ci { 61cb0ef41Sopenharmony_ci "textRaw": "Trace events", 71cb0ef41Sopenharmony_ci "name": "trace_events", 81cb0ef41Sopenharmony_ci "introduced_in": "v7.7.0", 91cb0ef41Sopenharmony_ci "stability": 1, 101cb0ef41Sopenharmony_ci "stabilityText": "Experimental", 111cb0ef41Sopenharmony_ci "desc": "<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>\n<p>The <code>node:trace_events</code> module provides a mechanism to centralize tracing\ninformation generated by V8, Node.js core, and userspace code.</p>\n<p>Tracing can be enabled with the <code>--trace-event-categories</code> command-line flag\nor by using the <code>node:trace_events</code> module. The <code>--trace-event-categories</code> flag\naccepts a list of comma-separated category names.</p>\n<p>The available categories are:</p>\n<ul>\n<li><code>node</code>: An empty placeholder.</li>\n<li><code>node.async_hooks</code>: Enables capture of detailed <a href=\"async_hooks.html\"><code>async_hooks</code></a> trace data.\nThe <a href=\"async_hooks.html\"><code>async_hooks</code></a> events have a unique <code>asyncId</code> and a special <code>triggerId</code>\n<code>triggerAsyncId</code> property.</li>\n<li><code>node.bootstrap</code>: Enables capture of Node.js bootstrap milestones.</li>\n<li><code>node.console</code>: Enables capture of <code>console.time()</code> and <code>console.count()</code>\noutput.</li>\n<li><code>node.threadpoolwork.sync</code>: Enables capture of trace data for threadpool\nsynchronous operations, such as <code>blob</code>, <code>zlib</code>, <code>crypto</code> and <code>node_api</code>.</li>\n<li><code>node.threadpoolwork.async</code>: Enables capture of trace data for threadpool\nasynchronous operations, such as <code>blob</code>, <code>zlib</code>, <code>crypto</code> and <code>node_api</code>.</li>\n<li><code>node.dns.native</code>: Enables capture of trace data for DNS queries.</li>\n<li><code>node.net.native</code>: Enables capture of trace data for network.</li>\n<li><code>node.environment</code>: Enables capture of Node.js Environment milestones.</li>\n<li><code>node.fs.sync</code>: Enables capture of trace data for file system sync methods.</li>\n<li><code>node.fs_dir.sync</code>: Enables capture of trace data for file system sync\ndirectory methods.</li>\n<li><code>node.fs.async</code>: Enables capture of trace data for file system async methods.</li>\n<li><code>node.fs_dir.async</code>: Enables capture of trace data for file system async\ndirectory methods.</li>\n<li><code>node.perf</code>: Enables capture of <a href=\"perf_hooks.html\">Performance API</a> measurements.\n<ul>\n<li><code>node.perf.usertiming</code>: Enables capture of only Performance API User Timing\nmeasures and marks.</li>\n<li><code>node.perf.timerify</code>: Enables capture of only Performance API timerify\nmeasurements.</li>\n</ul>\n</li>\n<li><code>node.promises.rejections</code>: Enables capture of trace data tracking the number\nof unhandled Promise rejections and handled-after-rejections.</li>\n<li><code>node.vm.script</code>: Enables capture of trace data for the <code>node:vm</code> module's\n<code>runInNewContext()</code>, <code>runInContext()</code>, and <code>runInThisContext()</code> methods.</li>\n<li><code>v8</code>: The <a href=\"v8.html\">V8</a> events are GC, compiling, and execution related.</li>\n<li><code>node.http</code>: Enables capture of trace data for http request / response.</li>\n</ul>\n<p>By default the <code>node</code>, <code>node.async_hooks</code>, and <code>v8</code> categories are enabled.</p>\n<pre><code class=\"language-bash\">node --trace-event-categories v8,node,node.async_hooks server.js\n</code></pre>\n<p>Prior versions of Node.js required the use of the <code>--trace-events-enabled</code>\nflag to enable trace events. This requirement has been removed. However, the\n<code>--trace-events-enabled</code> flag <em>may</em> still be used and will enable the\n<code>node</code>, <code>node.async_hooks</code>, and <code>v8</code> trace event categories by default.</p>\n<pre><code class=\"language-bash\">node --trace-events-enabled\n\n# is equivalent to\n\nnode --trace-event-categories v8,node,node.async_hooks\n</code></pre>\n<p>Alternatively, trace events may be enabled using the <code>node:trace_events</code> module:</p>\n<pre><code class=\"language-js\">const trace_events = require('node:trace_events');\nconst tracing = trace_events.createTracing({ categories: ['node.perf'] });\ntracing.enable(); // Enable trace event capture for the 'node.perf' category\n\n// do work\n\ntracing.disable(); // Disable trace event capture for the 'node.perf' category\n</code></pre>\n<p>Running Node.js with tracing enabled will produce log files that can be opened\nin the <a href=\"https://www.chromium.org/developers/how-tos/trace-event-profiling-tool\"><code>chrome://tracing</code></a>\ntab of Chrome.</p>\n<p>The logging file is by default called <code>node_trace.${rotation}.log</code>, where\n<code>${rotation}</code> is an incrementing log-rotation id. The filepath pattern can\nbe specified with <code>--trace-event-file-pattern</code> that accepts a template\nstring that supports <code>${rotation}</code> and <code>${pid}</code>:</p>\n<pre><code class=\"language-bash\">node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js\n</code></pre>\n<p>To guarantee that the log file is properly generated after signal events like\n<code>SIGINT</code>, <code>SIGTERM</code>, or <code>SIGBREAK</code>, make sure to have the appropriate handlers\nin your code, such as:</p>\n<pre><code class=\"language-js\">process.on('SIGINT', function onSigint() {\n console.info('Received SIGINT.');\n process.exit(130); // Or applicable exit code depending on OS and signal\n});\n</code></pre>\n<p>The tracing system uses the same time source\nas the one used by <code>process.hrtime()</code>.\nHowever the trace-event timestamps are expressed in microseconds,\nunlike <code>process.hrtime()</code> which returns nanoseconds.</p>\n<p>The features from this module are not available in <a href=\"worker_threads.html#class-worker\"><code>Worker</code></a> threads.</p>", 121cb0ef41Sopenharmony_ci "modules": [ 131cb0ef41Sopenharmony_ci { 141cb0ef41Sopenharmony_ci "textRaw": "The `node:trace_events` module", 151cb0ef41Sopenharmony_ci "name": "the_`node:trace_events`_module", 161cb0ef41Sopenharmony_ci "meta": { 171cb0ef41Sopenharmony_ci "added": [ 181cb0ef41Sopenharmony_ci "v10.0.0" 191cb0ef41Sopenharmony_ci ], 201cb0ef41Sopenharmony_ci "changes": [] 211cb0ef41Sopenharmony_ci }, 221cb0ef41Sopenharmony_ci "modules": [ 231cb0ef41Sopenharmony_ci { 241cb0ef41Sopenharmony_ci "textRaw": "`Tracing` object", 251cb0ef41Sopenharmony_ci "name": "`tracing`_object", 261cb0ef41Sopenharmony_ci "meta": { 271cb0ef41Sopenharmony_ci "added": [ 281cb0ef41Sopenharmony_ci "v10.0.0" 291cb0ef41Sopenharmony_ci ], 301cb0ef41Sopenharmony_ci "changes": [] 311cb0ef41Sopenharmony_ci }, 321cb0ef41Sopenharmony_ci "desc": "<p>The <code>Tracing</code> object is used to enable or disable tracing for sets of\ncategories. Instances are created using the <code>trace_events.createTracing()</code>\nmethod.</p>\n<p>When created, the <code>Tracing</code> object is disabled. Calling the\n<code>tracing.enable()</code> method adds the categories to the set of enabled trace event\ncategories. Calling <code>tracing.disable()</code> will remove the categories from the\nset of enabled trace event categories.</p>", 331cb0ef41Sopenharmony_ci "properties": [ 341cb0ef41Sopenharmony_ci { 351cb0ef41Sopenharmony_ci "textRaw": "`categories` {string}", 361cb0ef41Sopenharmony_ci "type": "string", 371cb0ef41Sopenharmony_ci "name": "categories", 381cb0ef41Sopenharmony_ci "meta": { 391cb0ef41Sopenharmony_ci "added": [ 401cb0ef41Sopenharmony_ci "v10.0.0" 411cb0ef41Sopenharmony_ci ], 421cb0ef41Sopenharmony_ci "changes": [] 431cb0ef41Sopenharmony_ci }, 441cb0ef41Sopenharmony_ci "desc": "<p>A comma-separated list of the trace event categories covered by this\n<code>Tracing</code> object.</p>" 451cb0ef41Sopenharmony_ci }, 461cb0ef41Sopenharmony_ci { 471cb0ef41Sopenharmony_ci "textRaw": "`enabled` {boolean} `true` only if the `Tracing` object has been enabled.", 481cb0ef41Sopenharmony_ci "type": "boolean", 491cb0ef41Sopenharmony_ci "name": "enabled", 501cb0ef41Sopenharmony_ci "meta": { 511cb0ef41Sopenharmony_ci "added": [ 521cb0ef41Sopenharmony_ci "v10.0.0" 531cb0ef41Sopenharmony_ci ], 541cb0ef41Sopenharmony_ci "changes": [] 551cb0ef41Sopenharmony_ci }, 561cb0ef41Sopenharmony_ci "desc": "`true` only if the `Tracing` object has been enabled." 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci ], 591cb0ef41Sopenharmony_ci "methods": [ 601cb0ef41Sopenharmony_ci { 611cb0ef41Sopenharmony_ci "textRaw": "`tracing.disable()`", 621cb0ef41Sopenharmony_ci "type": "method", 631cb0ef41Sopenharmony_ci "name": "disable", 641cb0ef41Sopenharmony_ci "meta": { 651cb0ef41Sopenharmony_ci "added": [ 661cb0ef41Sopenharmony_ci "v10.0.0" 671cb0ef41Sopenharmony_ci ], 681cb0ef41Sopenharmony_ci "changes": [] 691cb0ef41Sopenharmony_ci }, 701cb0ef41Sopenharmony_ci "signatures": [ 711cb0ef41Sopenharmony_ci { 721cb0ef41Sopenharmony_ci "params": [] 731cb0ef41Sopenharmony_ci } 741cb0ef41Sopenharmony_ci ], 751cb0ef41Sopenharmony_ci "desc": "<p>Disables this <code>Tracing</code> object.</p>\n<p>Only trace event categories <em>not</em> covered by other enabled <code>Tracing</code> objects\nand <em>not</em> specified by the <code>--trace-event-categories</code> flag will be disabled.</p>\n<pre><code class=\"language-js\">const trace_events = require('node:trace_events');\nconst t1 = trace_events.createTracing({ categories: ['node', 'v8'] });\nconst t2 = trace_events.createTracing({ categories: ['node.perf', 'node'] });\nt1.enable();\nt2.enable();\n\n// Prints 'node,node.perf,v8'\nconsole.log(trace_events.getEnabledCategories());\n\nt2.disable(); // Will only disable emission of the 'node.perf' category\n\n// Prints 'node,v8'\nconsole.log(trace_events.getEnabledCategories());\n</code></pre>" 761cb0ef41Sopenharmony_ci }, 771cb0ef41Sopenharmony_ci { 781cb0ef41Sopenharmony_ci "textRaw": "`tracing.enable()`", 791cb0ef41Sopenharmony_ci "type": "method", 801cb0ef41Sopenharmony_ci "name": "enable", 811cb0ef41Sopenharmony_ci "meta": { 821cb0ef41Sopenharmony_ci "added": [ 831cb0ef41Sopenharmony_ci "v10.0.0" 841cb0ef41Sopenharmony_ci ], 851cb0ef41Sopenharmony_ci "changes": [] 861cb0ef41Sopenharmony_ci }, 871cb0ef41Sopenharmony_ci "signatures": [ 881cb0ef41Sopenharmony_ci { 891cb0ef41Sopenharmony_ci "params": [] 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci ], 921cb0ef41Sopenharmony_ci "desc": "<p>Enables this <code>Tracing</code> object for the set of categories covered by the\n<code>Tracing</code> object.</p>" 931cb0ef41Sopenharmony_ci } 941cb0ef41Sopenharmony_ci ], 951cb0ef41Sopenharmony_ci "type": "module", 961cb0ef41Sopenharmony_ci "displayName": "`Tracing` object" 971cb0ef41Sopenharmony_ci }, 981cb0ef41Sopenharmony_ci { 991cb0ef41Sopenharmony_ci "textRaw": "Collect trace events data by inspector", 1001cb0ef41Sopenharmony_ci "name": "collect_trace_events_data_by_inspector", 1011cb0ef41Sopenharmony_ci "desc": "<pre><code class=\"language-js\">'use strict';\n\nconst { Session } = require('inspector');\nconst session = new Session();\nsession.connect();\n\nfunction post(message, data) {\n return new Promise((resolve, reject) => {\n session.post(message, data, (err, result) => {\n if (err)\n reject(new Error(JSON.stringify(err)));\n else\n resolve(result);\n });\n });\n}\n\nasync function collect() {\n const data = [];\n session.on('NodeTracing.dataCollected', (chunk) => data.push(chunk));\n session.on('NodeTracing.tracingComplete', () => {\n // done\n });\n const traceConfig = { includedCategories: ['v8'] };\n await post('NodeTracing.start', { traceConfig });\n // do something\n setTimeout(() => {\n post('NodeTracing.stop').then(() => {\n session.disconnect();\n console.log(data);\n });\n }, 1000);\n}\n\ncollect();\n</code></pre>", 1021cb0ef41Sopenharmony_ci "type": "module", 1031cb0ef41Sopenharmony_ci "displayName": "Collect trace events data by inspector" 1041cb0ef41Sopenharmony_ci } 1051cb0ef41Sopenharmony_ci ], 1061cb0ef41Sopenharmony_ci "methods": [ 1071cb0ef41Sopenharmony_ci { 1081cb0ef41Sopenharmony_ci "textRaw": "`trace_events.createTracing(options)`", 1091cb0ef41Sopenharmony_ci "type": "method", 1101cb0ef41Sopenharmony_ci "name": "createTracing", 1111cb0ef41Sopenharmony_ci "meta": { 1121cb0ef41Sopenharmony_ci "added": [ 1131cb0ef41Sopenharmony_ci "v10.0.0" 1141cb0ef41Sopenharmony_ci ], 1151cb0ef41Sopenharmony_ci "changes": [] 1161cb0ef41Sopenharmony_ci }, 1171cb0ef41Sopenharmony_ci "signatures": [ 1181cb0ef41Sopenharmony_ci { 1191cb0ef41Sopenharmony_ci "return": { 1201cb0ef41Sopenharmony_ci "textRaw": "Returns: {Tracing}.", 1211cb0ef41Sopenharmony_ci "name": "return", 1221cb0ef41Sopenharmony_ci "type": "Tracing", 1231cb0ef41Sopenharmony_ci "desc": "." 1241cb0ef41Sopenharmony_ci }, 1251cb0ef41Sopenharmony_ci "params": [ 1261cb0ef41Sopenharmony_ci { 1271cb0ef41Sopenharmony_ci "textRaw": "`options` {Object}", 1281cb0ef41Sopenharmony_ci "name": "options", 1291cb0ef41Sopenharmony_ci "type": "Object", 1301cb0ef41Sopenharmony_ci "options": [ 1311cb0ef41Sopenharmony_ci { 1321cb0ef41Sopenharmony_ci "textRaw": "`categories` {string\\[]} An array of trace category names. Values included in the array are coerced to a string when possible. An error will be thrown if the value cannot be coerced.", 1331cb0ef41Sopenharmony_ci "name": "categories", 1341cb0ef41Sopenharmony_ci "type": "string\\[]", 1351cb0ef41Sopenharmony_ci "desc": "An array of trace category names. Values included in the array are coerced to a string when possible. An error will be thrown if the value cannot be coerced." 1361cb0ef41Sopenharmony_ci } 1371cb0ef41Sopenharmony_ci ] 1381cb0ef41Sopenharmony_ci } 1391cb0ef41Sopenharmony_ci ] 1401cb0ef41Sopenharmony_ci } 1411cb0ef41Sopenharmony_ci ], 1421cb0ef41Sopenharmony_ci "desc": "<p>Creates and returns a <code>Tracing</code> object for the given set of <code>categories</code>.</p>\n<pre><code class=\"language-js\">const trace_events = require('node:trace_events');\nconst categories = ['node.perf', 'node.async_hooks'];\nconst tracing = trace_events.createTracing({ categories });\ntracing.enable();\n// do stuff\ntracing.disable();\n</code></pre>" 1431cb0ef41Sopenharmony_ci }, 1441cb0ef41Sopenharmony_ci { 1451cb0ef41Sopenharmony_ci "textRaw": "`trace_events.getEnabledCategories()`", 1461cb0ef41Sopenharmony_ci "type": "method", 1471cb0ef41Sopenharmony_ci "name": "getEnabledCategories", 1481cb0ef41Sopenharmony_ci "meta": { 1491cb0ef41Sopenharmony_ci "added": [ 1501cb0ef41Sopenharmony_ci "v10.0.0" 1511cb0ef41Sopenharmony_ci ], 1521cb0ef41Sopenharmony_ci "changes": [] 1531cb0ef41Sopenharmony_ci }, 1541cb0ef41Sopenharmony_ci "signatures": [ 1551cb0ef41Sopenharmony_ci { 1561cb0ef41Sopenharmony_ci "return": { 1571cb0ef41Sopenharmony_ci "textRaw": "Returns: {string}", 1581cb0ef41Sopenharmony_ci "name": "return", 1591cb0ef41Sopenharmony_ci "type": "string" 1601cb0ef41Sopenharmony_ci }, 1611cb0ef41Sopenharmony_ci "params": [] 1621cb0ef41Sopenharmony_ci } 1631cb0ef41Sopenharmony_ci ], 1641cb0ef41Sopenharmony_ci "desc": "<p>Returns a comma-separated list of all currently-enabled trace event\ncategories. The current set of enabled trace event categories is determined\nby the <em>union</em> of all currently-enabled <code>Tracing</code> objects and any categories\nenabled using the <code>--trace-event-categories</code> flag.</p>\n<p>Given the file <code>test.js</code> below, the command\n<code>node --trace-event-categories node.perf test.js</code> will print\n<code>'node.async_hooks,node.perf'</code> to the console.</p>\n<pre><code class=\"language-js\">const trace_events = require('node:trace_events');\nconst t1 = trace_events.createTracing({ categories: ['node.async_hooks'] });\nconst t2 = trace_events.createTracing({ categories: ['node.perf'] });\nconst t3 = trace_events.createTracing({ categories: ['v8'] });\n\nt1.enable();\nt2.enable();\n\nconsole.log(trace_events.getEnabledCategories());\n</code></pre>\n<h2>Examples</h2>" 1651cb0ef41Sopenharmony_ci } 1661cb0ef41Sopenharmony_ci ], 1671cb0ef41Sopenharmony_ci "type": "module", 1681cb0ef41Sopenharmony_ci "displayName": "The `node:trace_events` module" 1691cb0ef41Sopenharmony_ci } 1701cb0ef41Sopenharmony_ci ], 1711cb0ef41Sopenharmony_ci "type": "module", 1721cb0ef41Sopenharmony_ci "displayName": "Trace events" 1731cb0ef41Sopenharmony_ci } 1741cb0ef41Sopenharmony_ci ] 1751cb0ef41Sopenharmony_ci}