11cb0ef41Sopenharmony_ci# Trace events 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v7.7.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/trace_events.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `node:trace_events` module provides a mechanism to centralize tracing 101cb0ef41Sopenharmony_ciinformation generated by V8, Node.js core, and userspace code. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciTracing can be enabled with the `--trace-event-categories` command-line flag 131cb0ef41Sopenharmony_cior by using the `node:trace_events` module. The `--trace-event-categories` flag 141cb0ef41Sopenharmony_ciaccepts a list of comma-separated category names. 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciThe available categories are: 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci* `node`: An empty placeholder. 191cb0ef41Sopenharmony_ci* `node.async_hooks`: Enables capture of detailed [`async_hooks`][] trace data. 201cb0ef41Sopenharmony_ci The [`async_hooks`][] events have a unique `asyncId` and a special `triggerId` 211cb0ef41Sopenharmony_ci `triggerAsyncId` property. 221cb0ef41Sopenharmony_ci* `node.bootstrap`: Enables capture of Node.js bootstrap milestones. 231cb0ef41Sopenharmony_ci* `node.console`: Enables capture of `console.time()` and `console.count()` 241cb0ef41Sopenharmony_ci output. 251cb0ef41Sopenharmony_ci* `node.threadpoolwork.sync`: Enables capture of trace data for threadpool 261cb0ef41Sopenharmony_ci synchronous operations, such as `blob`, `zlib`, `crypto` and `node_api`. 271cb0ef41Sopenharmony_ci* `node.threadpoolwork.async`: Enables capture of trace data for threadpool 281cb0ef41Sopenharmony_ci asynchronous operations, such as `blob`, `zlib`, `crypto` and `node_api`. 291cb0ef41Sopenharmony_ci* `node.dns.native`: Enables capture of trace data for DNS queries. 301cb0ef41Sopenharmony_ci* `node.net.native`: Enables capture of trace data for network. 311cb0ef41Sopenharmony_ci* `node.environment`: Enables capture of Node.js Environment milestones. 321cb0ef41Sopenharmony_ci* `node.fs.sync`: Enables capture of trace data for file system sync methods. 331cb0ef41Sopenharmony_ci* `node.fs_dir.sync`: Enables capture of trace data for file system sync 341cb0ef41Sopenharmony_ci directory methods. 351cb0ef41Sopenharmony_ci* `node.fs.async`: Enables capture of trace data for file system async methods. 361cb0ef41Sopenharmony_ci* `node.fs_dir.async`: Enables capture of trace data for file system async 371cb0ef41Sopenharmony_ci directory methods. 381cb0ef41Sopenharmony_ci* `node.perf`: Enables capture of [Performance API][] measurements. 391cb0ef41Sopenharmony_ci * `node.perf.usertiming`: Enables capture of only Performance API User Timing 401cb0ef41Sopenharmony_ci measures and marks. 411cb0ef41Sopenharmony_ci * `node.perf.timerify`: Enables capture of only Performance API timerify 421cb0ef41Sopenharmony_ci measurements. 431cb0ef41Sopenharmony_ci* `node.promises.rejections`: Enables capture of trace data tracking the number 441cb0ef41Sopenharmony_ci of unhandled Promise rejections and handled-after-rejections. 451cb0ef41Sopenharmony_ci* `node.vm.script`: Enables capture of trace data for the `node:vm` module's 461cb0ef41Sopenharmony_ci `runInNewContext()`, `runInContext()`, and `runInThisContext()` methods. 471cb0ef41Sopenharmony_ci* `v8`: The [V8][] events are GC, compiling, and execution related. 481cb0ef41Sopenharmony_ci* `node.http`: Enables capture of trace data for http request / response. 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciBy default the `node`, `node.async_hooks`, and `v8` categories are enabled. 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci```bash 531cb0ef41Sopenharmony_cinode --trace-event-categories v8,node,node.async_hooks server.js 541cb0ef41Sopenharmony_ci``` 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ciPrior versions of Node.js required the use of the `--trace-events-enabled` 571cb0ef41Sopenharmony_ciflag to enable trace events. This requirement has been removed. However, the 581cb0ef41Sopenharmony_ci`--trace-events-enabled` flag _may_ still be used and will enable the 591cb0ef41Sopenharmony_ci`node`, `node.async_hooks`, and `v8` trace event categories by default. 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci```bash 621cb0ef41Sopenharmony_cinode --trace-events-enabled 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci# is equivalent to 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_cinode --trace-event-categories v8,node,node.async_hooks 671cb0ef41Sopenharmony_ci``` 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciAlternatively, trace events may be enabled using the `node:trace_events` module: 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci```js 721cb0ef41Sopenharmony_ciconst trace_events = require('node:trace_events'); 731cb0ef41Sopenharmony_ciconst tracing = trace_events.createTracing({ categories: ['node.perf'] }); 741cb0ef41Sopenharmony_citracing.enable(); // Enable trace event capture for the 'node.perf' category 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci// do work 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_citracing.disable(); // Disable trace event capture for the 'node.perf' category 791cb0ef41Sopenharmony_ci``` 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciRunning Node.js with tracing enabled will produce log files that can be opened 821cb0ef41Sopenharmony_ciin the [`chrome://tracing`](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool) 831cb0ef41Sopenharmony_citab of Chrome. 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ciThe logging file is by default called `node_trace.${rotation}.log`, where 861cb0ef41Sopenharmony_ci`${rotation}` is an incrementing log-rotation id. The filepath pattern can 871cb0ef41Sopenharmony_cibe specified with `--trace-event-file-pattern` that accepts a template 881cb0ef41Sopenharmony_cistring that supports `${rotation}` and `${pid}`: 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci```bash 911cb0ef41Sopenharmony_cinode --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js 921cb0ef41Sopenharmony_ci``` 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciTo guarantee that the log file is properly generated after signal events like 951cb0ef41Sopenharmony_ci`SIGINT`, `SIGTERM`, or `SIGBREAK`, make sure to have the appropriate handlers 961cb0ef41Sopenharmony_ciin your code, such as: 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci```js 991cb0ef41Sopenharmony_ciprocess.on('SIGINT', function onSigint() { 1001cb0ef41Sopenharmony_ci console.info('Received SIGINT.'); 1011cb0ef41Sopenharmony_ci process.exit(130); // Or applicable exit code depending on OS and signal 1021cb0ef41Sopenharmony_ci}); 1031cb0ef41Sopenharmony_ci``` 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ciThe tracing system uses the same time source 1061cb0ef41Sopenharmony_cias the one used by `process.hrtime()`. 1071cb0ef41Sopenharmony_ciHowever the trace-event timestamps are expressed in microseconds, 1081cb0ef41Sopenharmony_ciunlike `process.hrtime()` which returns nanoseconds. 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ciThe features from this module are not available in [`Worker`][] threads. 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci## The `node:trace_events` module 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci<!-- YAML 1151cb0ef41Sopenharmony_ciadded: v10.0.0 1161cb0ef41Sopenharmony_ci--> 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci### `Tracing` object 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci<!-- YAML 1211cb0ef41Sopenharmony_ciadded: v10.0.0 1221cb0ef41Sopenharmony_ci--> 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ciThe `Tracing` object is used to enable or disable tracing for sets of 1251cb0ef41Sopenharmony_cicategories. Instances are created using the `trace_events.createTracing()` 1261cb0ef41Sopenharmony_cimethod. 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ciWhen created, the `Tracing` object is disabled. Calling the 1291cb0ef41Sopenharmony_ci`tracing.enable()` method adds the categories to the set of enabled trace event 1301cb0ef41Sopenharmony_cicategories. Calling `tracing.disable()` will remove the categories from the 1311cb0ef41Sopenharmony_ciset of enabled trace event categories. 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci#### `tracing.categories` 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci<!-- YAML 1361cb0ef41Sopenharmony_ciadded: v10.0.0 1371cb0ef41Sopenharmony_ci--> 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci* {string} 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ciA comma-separated list of the trace event categories covered by this 1421cb0ef41Sopenharmony_ci`Tracing` object. 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci#### `tracing.disable()` 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci<!-- YAML 1471cb0ef41Sopenharmony_ciadded: v10.0.0 1481cb0ef41Sopenharmony_ci--> 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ciDisables this `Tracing` object. 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ciOnly trace event categories _not_ covered by other enabled `Tracing` objects 1531cb0ef41Sopenharmony_ciand _not_ specified by the `--trace-event-categories` flag will be disabled. 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci```js 1561cb0ef41Sopenharmony_ciconst trace_events = require('node:trace_events'); 1571cb0ef41Sopenharmony_ciconst t1 = trace_events.createTracing({ categories: ['node', 'v8'] }); 1581cb0ef41Sopenharmony_ciconst t2 = trace_events.createTracing({ categories: ['node.perf', 'node'] }); 1591cb0ef41Sopenharmony_cit1.enable(); 1601cb0ef41Sopenharmony_cit2.enable(); 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci// Prints 'node,node.perf,v8' 1631cb0ef41Sopenharmony_ciconsole.log(trace_events.getEnabledCategories()); 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_cit2.disable(); // Will only disable emission of the 'node.perf' category 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci// Prints 'node,v8' 1681cb0ef41Sopenharmony_ciconsole.log(trace_events.getEnabledCategories()); 1691cb0ef41Sopenharmony_ci``` 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci#### `tracing.enable()` 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci<!-- YAML 1741cb0ef41Sopenharmony_ciadded: v10.0.0 1751cb0ef41Sopenharmony_ci--> 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ciEnables this `Tracing` object for the set of categories covered by the 1781cb0ef41Sopenharmony_ci`Tracing` object. 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci#### `tracing.enabled` 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ci<!-- YAML 1831cb0ef41Sopenharmony_ciadded: v10.0.0 1841cb0ef41Sopenharmony_ci--> 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci* {boolean} `true` only if the `Tracing` object has been enabled. 1871cb0ef41Sopenharmony_ci 1881cb0ef41Sopenharmony_ci### `trace_events.createTracing(options)` 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci<!-- YAML 1911cb0ef41Sopenharmony_ciadded: v10.0.0 1921cb0ef41Sopenharmony_ci--> 1931cb0ef41Sopenharmony_ci 1941cb0ef41Sopenharmony_ci* `options` {Object} 1951cb0ef41Sopenharmony_ci * `categories` {string\[]} An array of trace category names. Values included 1961cb0ef41Sopenharmony_ci in the array are coerced to a string when possible. An error will be 1971cb0ef41Sopenharmony_ci thrown if the value cannot be coerced. 1981cb0ef41Sopenharmony_ci* Returns: {Tracing}. 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ciCreates and returns a `Tracing` object for the given set of `categories`. 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci```js 2031cb0ef41Sopenharmony_ciconst trace_events = require('node:trace_events'); 2041cb0ef41Sopenharmony_ciconst categories = ['node.perf', 'node.async_hooks']; 2051cb0ef41Sopenharmony_ciconst tracing = trace_events.createTracing({ categories }); 2061cb0ef41Sopenharmony_citracing.enable(); 2071cb0ef41Sopenharmony_ci// do stuff 2081cb0ef41Sopenharmony_citracing.disable(); 2091cb0ef41Sopenharmony_ci``` 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci### `trace_events.getEnabledCategories()` 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci<!-- YAML 2141cb0ef41Sopenharmony_ciadded: v10.0.0 2151cb0ef41Sopenharmony_ci--> 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci* Returns: {string} 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ciReturns a comma-separated list of all currently-enabled trace event 2201cb0ef41Sopenharmony_cicategories. The current set of enabled trace event categories is determined 2211cb0ef41Sopenharmony_ciby the _union_ of all currently-enabled `Tracing` objects and any categories 2221cb0ef41Sopenharmony_cienabled using the `--trace-event-categories` flag. 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ciGiven the file `test.js` below, the command 2251cb0ef41Sopenharmony_ci`node --trace-event-categories node.perf test.js` will print 2261cb0ef41Sopenharmony_ci`'node.async_hooks,node.perf'` to the console. 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ci```js 2291cb0ef41Sopenharmony_ciconst trace_events = require('node:trace_events'); 2301cb0ef41Sopenharmony_ciconst t1 = trace_events.createTracing({ categories: ['node.async_hooks'] }); 2311cb0ef41Sopenharmony_ciconst t2 = trace_events.createTracing({ categories: ['node.perf'] }); 2321cb0ef41Sopenharmony_ciconst t3 = trace_events.createTracing({ categories: ['v8'] }); 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_cit1.enable(); 2351cb0ef41Sopenharmony_cit2.enable(); 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ciconsole.log(trace_events.getEnabledCategories()); 2381cb0ef41Sopenharmony_ci``` 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci## Examples 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci### Collect trace events data by inspector 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci```js 2451cb0ef41Sopenharmony_ci'use strict'; 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ciconst { Session } = require('inspector'); 2481cb0ef41Sopenharmony_ciconst session = new Session(); 2491cb0ef41Sopenharmony_cisession.connect(); 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_cifunction post(message, data) { 2521cb0ef41Sopenharmony_ci return new Promise((resolve, reject) => { 2531cb0ef41Sopenharmony_ci session.post(message, data, (err, result) => { 2541cb0ef41Sopenharmony_ci if (err) 2551cb0ef41Sopenharmony_ci reject(new Error(JSON.stringify(err))); 2561cb0ef41Sopenharmony_ci else 2571cb0ef41Sopenharmony_ci resolve(result); 2581cb0ef41Sopenharmony_ci }); 2591cb0ef41Sopenharmony_ci }); 2601cb0ef41Sopenharmony_ci} 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ciasync function collect() { 2631cb0ef41Sopenharmony_ci const data = []; 2641cb0ef41Sopenharmony_ci session.on('NodeTracing.dataCollected', (chunk) => data.push(chunk)); 2651cb0ef41Sopenharmony_ci session.on('NodeTracing.tracingComplete', () => { 2661cb0ef41Sopenharmony_ci // done 2671cb0ef41Sopenharmony_ci }); 2681cb0ef41Sopenharmony_ci const traceConfig = { includedCategories: ['v8'] }; 2691cb0ef41Sopenharmony_ci await post('NodeTracing.start', { traceConfig }); 2701cb0ef41Sopenharmony_ci // do something 2711cb0ef41Sopenharmony_ci setTimeout(() => { 2721cb0ef41Sopenharmony_ci post('NodeTracing.stop').then(() => { 2731cb0ef41Sopenharmony_ci session.disconnect(); 2741cb0ef41Sopenharmony_ci console.log(data); 2751cb0ef41Sopenharmony_ci }); 2761cb0ef41Sopenharmony_ci }, 1000); 2771cb0ef41Sopenharmony_ci} 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_cicollect(); 2801cb0ef41Sopenharmony_ci``` 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ci[Performance API]: perf_hooks.md 2831cb0ef41Sopenharmony_ci[V8]: v8.md 2841cb0ef41Sopenharmony_ci[`Worker`]: worker_threads.md#class-worker 2851cb0ef41Sopenharmony_ci[`async_hooks`]: async_hooks.md 286