5d18a8287b4c3efc63c6c2854552d05d04ac97f494cbfb639eabfd5487e60e5129354d32b4333582f079424649910af0485850184e870228b08d8c2d26325e 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * The `node: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`](https://nodejs.org/docs/latest-v22.x/api/async_hooks.html) trace data.
  13. * The [`async_hooks`](https://nodejs.org/docs/latest-v22.x/api/async_hooks.html) 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.threadpoolwork.sync`: Enables capture of trace data for threadpool synchronous operations, such as `blob`, `zlib`, `crypto` and `node_api`.
  17. * * `node.threadpoolwork.async`: Enables capture of trace data for threadpool asynchronous operations, such as `blob`, `zlib`, `crypto` and `node_api`.
  18. * * `node.dns.native`: Enables capture of trace data for DNS queries.
  19. * * `node.net.native`: Enables capture of trace data for network.
  20. * * `node.environment`: Enables capture of Node.js Environment milestones.
  21. * * `node.fs.sync`: Enables capture of trace data for file system sync methods.
  22. * * `node.fs_dir.sync`: Enables capture of trace data for file system sync directory methods.
  23. * * `node.fs.async`: Enables capture of trace data for file system async methods.
  24. * * `node.fs_dir.async`: Enables capture of trace data for file system async directory methods.
  25. * * `node.perf`: Enables capture of [Performance API](https://nodejs.org/docs/latest-v22.x/api/perf_hooks.html) measurements.
  26. * * `node.perf.usertiming`: Enables capture of only Performance API User Timing
  27. * measures and marks.
  28. * * `node.perf.timerify`: Enables capture of only Performance API timerify
  29. * measurements.
  30. * * `node.promises.rejections`: Enables capture of trace data tracking the number
  31. * of unhandled Promise rejections and handled-after-rejections.
  32. * * `node.vm.script`: Enables capture of trace data for the `node:vm` module's `runInNewContext()`, `runInContext()`, and `runInThisContext()` methods.
  33. * * `v8`: The [V8](https://nodejs.org/docs/latest-v22.x/api/v8.html) events are GC, compiling, and execution related.
  34. * * `node.http`: Enables capture of trace data for http request / response.
  35. *
  36. * By default the `node`, `node.async_hooks`, and `v8` categories are enabled.
  37. *
  38. * ```bash
  39. * node --trace-event-categories v8,node,node.async_hooks server.js
  40. * ```
  41. *
  42. * 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
  43. * used and will enable the `node`, `node.async_hooks`, and `v8` trace event categories by default.
  44. *
  45. * ```bash
  46. * node --trace-events-enabled
  47. *
  48. * # is equivalent to
  49. *
  50. * node --trace-event-categories v8,node,node.async_hooks
  51. * ```
  52. *
  53. * Alternatively, trace events may be enabled using the `node:trace_events` module:
  54. *
  55. * ```js
  56. * import trace_events from 'node:trace_events';
  57. * const tracing = trace_events.createTracing({ categories: ['node.perf'] });
  58. * tracing.enable(); // Enable trace event capture for the 'node.perf' category
  59. *
  60. * // do work
  61. *
  62. * tracing.disable(); // Disable trace event capture for the 'node.perf' category
  63. * ```
  64. *
  65. * Running Node.js with tracing enabled will produce log files that can be opened
  66. * in the [`chrome://tracing`](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool) tab of Chrome.
  67. *
  68. * The logging file is by default called `node_trace.${rotation}.log`, where `${rotation}` is an incrementing log-rotation id. The filepath pattern can
  69. * be specified with `--trace-event-file-pattern` that accepts a template
  70. * string that supports `${rotation}` and `${pid}`:
  71. *
  72. * ```bash
  73. * node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js
  74. * ```
  75. *
  76. * To guarantee that the log file is properly generated after signal events like `SIGINT`, `SIGTERM`, or `SIGBREAK`, make sure to have the appropriate handlers
  77. * in your code, such as:
  78. *
  79. * ```js
  80. * process.on('SIGINT', function onSigint() {
  81. * console.info('Received SIGINT.');
  82. * process.exit(130); // Or applicable exit code depending on OS and signal
  83. * });
  84. * ```
  85. *
  86. * The tracing system uses the same time source
  87. * as the one used by `process.hrtime()`.
  88. * However the trace-event timestamps are expressed in microseconds,
  89. * unlike `process.hrtime()` which returns nanoseconds.
  90. *
  91. * The features from this module are not available in [`Worker`](https://nodejs.org/docs/latest-v22.x/api/worker_threads.html#class-worker) threads.
  92. * @experimental
  93. * @see [source](https://github.com/nodejs/node/blob/v22.x/lib/trace_events.js)
  94. */
  95. declare module "trace_events" {
  96. /**
  97. * The `Tracing` object is used to enable or disable tracing for sets of
  98. * categories. Instances are created using the
  99. * `trace_events.createTracing()` method.
  100. *
  101. * When created, the `Tracing` object is disabled. Calling the
  102. * `tracing.enable()` method adds the categories to the set of enabled trace
  103. * event categories. Calling `tracing.disable()` will remove the categories
  104. * from the set of enabled trace event categories.
  105. */
  106. interface Tracing {
  107. /**
  108. * A comma-separated list of the trace event categories covered by this
  109. * `Tracing` object.
  110. * @since v10.0.0
  111. */
  112. readonly categories: string;
  113. /**
  114. * Disables this `Tracing` object.
  115. *
  116. * Only trace event categories _not_ covered by other enabled `Tracing`
  117. * objects and _not_ specified by the `--trace-event-categories` flag
  118. * will be disabled.
  119. *
  120. * ```js
  121. * import trace_events from 'node:trace_events';
  122. * const t1 = trace_events.createTracing({ categories: ['node', 'v8'] });
  123. * const t2 = trace_events.createTracing({ categories: ['node.perf', 'node'] });
  124. * t1.enable();
  125. * t2.enable();
  126. *
  127. * // Prints 'node,node.perf,v8'
  128. * console.log(trace_events.getEnabledCategories());
  129. *
  130. * t2.disable(); // Will only disable emission of the 'node.perf' category
  131. *
  132. * // Prints 'node,v8'
  133. * console.log(trace_events.getEnabledCategories());
  134. * ```
  135. * @since v10.0.0
  136. */
  137. disable(): void;
  138. /**
  139. * Enables this `Tracing` object for the set of categories covered by
  140. * the `Tracing` object.
  141. * @since v10.0.0
  142. */
  143. enable(): void;
  144. /**
  145. * `true` only if the `Tracing` object has been enabled.
  146. * @since v10.0.0
  147. */
  148. readonly enabled: boolean;
  149. }
  150. interface CreateTracingOptions {
  151. /**
  152. * An array of trace category names. Values included in the array are
  153. * coerced to a string when possible. An error will be thrown if the
  154. * value cannot be coerced.
  155. */
  156. categories: string[];
  157. }
  158. /**
  159. * Creates and returns a `Tracing` object for the given set of `categories`.
  160. *
  161. * ```js
  162. * import trace_events from 'node:trace_events';
  163. * const categories = ['node.perf', 'node.async_hooks'];
  164. * const tracing = trace_events.createTracing({ categories });
  165. * tracing.enable();
  166. * // do stuff
  167. * tracing.disable();
  168. * ```
  169. * @since v10.0.0
  170. */
  171. function createTracing(options: CreateTracingOptions): Tracing;
  172. /**
  173. * Returns a comma-separated list of all currently-enabled trace event
  174. * categories. The current set of enabled trace event categories is determined
  175. * by the _union_ of all currently-enabled `Tracing` objects and any categories
  176. * enabled using the `--trace-event-categories` flag.
  177. *
  178. * 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.
  179. *
  180. * ```js
  181. * import trace_events from 'node:trace_events';
  182. * const t1 = trace_events.createTracing({ categories: ['node.async_hooks'] });
  183. * const t2 = trace_events.createTracing({ categories: ['node.perf'] });
  184. * const t3 = trace_events.createTracing({ categories: ['v8'] });
  185. *
  186. * t1.enable();
  187. * t2.enable();
  188. *
  189. * console.log(trace_events.getEnabledCategories());
  190. * ```
  191. * @since v10.0.0
  192. */
  193. function getEnabledCategories(): string | undefined;
  194. }
  195. declare module "node:trace_events" {
  196. export * from "trace_events";
  197. }