Source Code: lib/trace_events.js
The node:trace_events module provides a mechanism to centralize tracing\ninformation generated by V8, Node.js core, and userspace code.
node:trace_events
Tracing can be enabled with the --trace-event-categories command-line flag\nor by using the node:trace_events module. The --trace-event-categories flag\naccepts a list of comma-separated category names.
--trace-event-categories
The available categories are:
node
node.async_hooks
async_hooks
asyncId
triggerId
triggerAsyncId
node.bootstrap
node.console
console.time()
console.count()
node.dns.native
node.net.native
node.environment
node.fs.sync
node.fs_dir.sync
node.fs.async
node.fs_dir.async
node.perf
node.perf.usertiming
node.perf.timerify
node.promises.rejections
node.vm.script
node:vm
runInNewContext()
runInContext()
runInThisContext()
v8
node.http
By default the node, node.async_hooks, and v8 categories are enabled.
node --trace-event-categories v8,node,node.async_hooks server.js\n
Prior versions of Node.js required the use of the --trace-events-enabled\nflag to enable trace events. This requirement has been removed. However, the\n--trace-events-enabled flag may still be used and will enable the\nnode, node.async_hooks, and v8 trace event categories by default.
--trace-events-enabled
node --trace-events-enabled\n\n# is equivalent to\n\nnode --trace-event-categories v8,node,node.async_hooks\n
Alternatively, trace events may be enabled using the node:trace_events module:
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
Running Node.js with tracing enabled will produce log files that can be opened\nin the chrome://tracing\ntab of Chrome.
chrome://tracing
The logging file is by default called node_trace.${rotation}.log, where\n${rotation} is an incrementing log-rotation id. The filepath pattern can\nbe specified with --trace-event-file-pattern that accepts a template\nstring that supports ${rotation} and ${pid}:
node_trace.${rotation}.log
${rotation}
--trace-event-file-pattern
${pid}
node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js\n
To guarantee that the log file is properly generated after signal events like\nSIGINT, SIGTERM, or SIGBREAK, make sure to have the appropriate handlers\nin your code, such as:
SIGINT
SIGTERM
SIGBREAK
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
The tracing system uses the same time source\nas the one used by process.hrtime().\nHowever the trace-event timestamps are expressed in microseconds,\nunlike process.hrtime() which returns nanoseconds.
process.hrtime()
The features from this module are not available in Worker threads.
Worker
The Tracing object is used to enable or disable tracing for sets of\ncategories. Instances are created using the trace_events.createTracing()\nmethod.
Tracing
trace_events.createTracing()
When created, the Tracing object is disabled. Calling the\ntracing.enable() method adds the categories to the set of enabled trace event\ncategories. Calling tracing.disable() will remove the categories from the\nset of enabled trace event categories.
tracing.enable()
tracing.disable()
A comma-separated list of the trace event categories covered by this\nTracing object.
Disables this Tracing object.
Only trace event categories not covered by other enabled Tracing objects\nand not specified by the --trace-event-categories flag will be disabled.
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
Enables this Tracing object for the set of categories covered by the\nTracing object.
'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
Creates and returns a Tracing object for the given set of categories.
categories
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
Returns a comma-separated list of all currently-enabled trace event\ncategories. The current set of enabled trace event categories is determined\nby the union of all currently-enabled Tracing objects and any categories\nenabled using the --trace-event-categories flag.
Given the file test.js below, the command\nnode --trace-event-categories node.perf test.js will print\n'node.async_hooks,node.perf' to the console.
test.js
node --trace-event-categories node.perf test.js
'node.async_hooks,node.perf'
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