UNPKG

6.78 kBTypeScriptView Raw
1/**
2 * The `trace_events` module provides a mechanism to centralize tracing information
3 * generated by V8, Node.js core, and userspace code.
4 *
5 * Tracing can be enabled with the `--trace-event-categories` command-line flag
6 * or by using the `trace_events` module. The `--trace-event-categories` flag
7 * accepts a list of comma-separated category names.
8 *
9 * The available categories are:
10 *
11 * * `node`: An empty placeholder.
12 * * `node.async_hooks`: Enables capture of detailed `async_hooks` trace data.
13 * The `async_hooks` events have a unique `asyncId` and a special `triggerId` `triggerAsyncId` property.
14 * * `node.bootstrap`: Enables capture of Node.js bootstrap milestones.
15 * * `node.console`: Enables capture of `console.time()` and `console.count()`output.
16 * * `node.dns.native`: Enables capture of trace data for DNS queries.
17 * * `node.environment`: Enables capture of Node.js Environment milestones.
18 * * `node.fs.sync`: Enables capture of trace data for file system sync methods.
19 * * `node.perf`: Enables capture of `Performance API` measurements.
20 * * `node.perf.usertiming`: Enables capture of only Performance API User Timing
21 * measures and marks.
22 * * `node.perf.timerify`: Enables capture of only Performance API timerify
23 * measurements.
24 * * `node.promises.rejections`: Enables capture of trace data tracking the number
25 * of unhandled Promise rejections and handled-after-rejections.
26 * * `node.vm.script`: Enables capture of trace data for the `vm` module's`runInNewContext()`, `runInContext()`, and `runInThisContext()` methods.
27 * * `v8`: The `V8` events are GC, compiling, and execution related.
28 *
29 * By default the `node`, `node.async_hooks`, and `v8` categories are enabled.
30 *
31 * ```bash
32 * node --trace-event-categories v8,node,node.async_hooks server.js
33 * ```
34 *
35 * Prior versions of Node.js required the use of the `--trace-events-enabled`flag to enable trace events. This requirement has been removed. However, the`--trace-events-enabled` flag _may_ still be
36 * used and will enable the`node`, `node.async_hooks`, and `v8` trace event categories by default.
37 *
38 * ```bash
39 * node --trace-events-enabled
40 *
41 * # is equivalent to
42 *
43 * node --trace-event-categories v8,node,node.async_hooks
44 * ```
45 *
46 * Alternatively, trace events may be enabled using the `trace_events` module:
47 *
48 * ```js
49 * const trace_events = require('trace_events');
50 * const tracing = trace_events.createTracing({ categories: ['node.perf'] });
51 * tracing.enable(); // Enable trace event capture for the 'node.perf' category
52 *
53 * // do work
54 *
55 * tracing.disable(); // Disable trace event capture for the 'node.perf' category
56 * ```
57 *
58 * Running Node.js with tracing enabled will produce log files that can be opened
59 * in the [`chrome://tracing`](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool) tab of Chrome.
60 *
61 * The logging file is by default called `node_trace.${rotation}.log`, where`${rotation}` is an incrementing log-rotation id. The filepath pattern can
62 * be specified with `--trace-event-file-pattern` that accepts a template
63 * string that supports `${rotation}` and `${pid}`:
64 *
65 * ```bash
66 * node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js
67 * ```
68 *
69 * The tracing system uses the same time source
70 * as the one used by `process.hrtime()`.
71 * However the trace-event timestamps are expressed in microseconds,
72 * unlike `process.hrtime()` which returns nanoseconds.
73 *
74 * The features from this module are not available in `Worker` threads.
75 * @experimental
76 * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/trace_events.js)
77 */
78declare module 'trace_events' {
79 /**
80 * The `Tracing` object is used to enable or disable tracing for sets of
81 * categories. Instances are created using the
82 * `trace_events.createTracing()` method.
83 *
84 * When created, the `Tracing` object is disabled. Calling the
85 * `tracing.enable()` method adds the categories to the set of enabled trace
86 * event categories. Calling `tracing.disable()` will remove the categories
87 * from the set of enabled trace event categories.
88 */
89 interface Tracing {
90 /**
91 * A comma-separated list of the trace event categories covered by this
92 * `Tracing` object.
93 */
94 readonly categories: string;
95 /**
96 * Disables this `Tracing` object.
97 *
98 * Only trace event categories _not_ covered by other enabled `Tracing`
99 * objects and _not_ specified by the `--trace-event-categories` flag
100 * will be disabled.
101 */
102 disable(): void;
103 /**
104 * Enables this `Tracing` object for the set of categories covered by
105 * the `Tracing` object.
106 */
107 enable(): void;
108 /**
109 * `true` only if the `Tracing` object has been enabled.
110 */
111 readonly enabled: boolean;
112 }
113 interface CreateTracingOptions {
114 /**
115 * An array of trace category names. Values included in the array are
116 * coerced to a string when possible. An error will be thrown if the
117 * value cannot be coerced.
118 */
119 categories: string[];
120 }
121 /**
122 * Creates and returns a `Tracing` object for the given set of `categories`.
123 *
124 * ```js
125 * const trace_events = require('trace_events');
126 * const categories = ['node.perf', 'node.async_hooks'];
127 * const tracing = trace_events.createTracing({ categories });
128 * tracing.enable();
129 * // do stuff
130 * tracing.disable();
131 * ```
132 * @since v10.0.0
133 * @return .
134 */
135 function createTracing(options: CreateTracingOptions): Tracing;
136 /**
137 * Returns a comma-separated list of all currently-enabled trace event
138 * categories. The current set of enabled trace event categories is determined
139 * by the _union_ of all currently-enabled `Tracing` objects and any categories
140 * enabled using the `--trace-event-categories` flag.
141 *
142 * Given the file `test.js` below, the command`node --trace-event-categories node.perf test.js` will print`'node.async_hooks,node.perf'` to the console.
143 *
144 * ```js
145 * const trace_events = require('trace_events');
146 * const t1 = trace_events.createTracing({ categories: ['node.async_hooks'] });
147 * const t2 = trace_events.createTracing({ categories: ['node.perf'] });
148 * const t3 = trace_events.createTracing({ categories: ['v8'] });
149 *
150 * t1.enable();
151 * t2.enable();
152 *
153 * console.log(trace_events.getEnabledCategories());
154 * ```
155 * @since v10.0.0
156 */
157 function getEnabledCategories(): string | undefined;
158}
159declare module 'node:trace_events' {
160 export * from 'trace_events';
161}