1{
2  "type": "module",
3  "source": "doc/api/tracing.md",
4  "modules": [
5    {
6      "textRaw": "Trace Events",
7      "name": "trace_events",
8      "introduced_in": "v7.7.0",
9      "stability": 1,
10      "stabilityText": "Experimental",
11      "desc": "<p>Trace Event provides a mechanism to centralize tracing information generated by\nV8, 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>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.fs.sync</code> - Enables capture of trace data for file system sync methods.</li>\n<li>\n<p><code>node.perf</code> - Enables capture of <a href=\"perf_hooks.html\">Performance API</a> measurements.</p>\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>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</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-txt\">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-txt\">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>trace_events</code> module:</p>\n<pre><code class=\"language-js\">const trace_events = require('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-txt\">node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js\n</code></pre>\n<p>Starting with Node.js 10.0.0, 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>",
12      "modules": [
13        {
14          "textRaw": "The `trace_events` module",
15          "name": "the_`trace_events`_module",
16          "meta": {
17            "added": [
18              "v10.0.0"
19            ],
20            "changes": []
21          },
22          "modules": [
23            {
24              "textRaw": "`Tracing` object",
25              "name": "`tracing`_object",
26              "meta": {
27                "added": [
28                  "v10.0.0"
29                ],
30                "changes": []
31              },
32              "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>",
33              "modules": [
34                {
35                  "textRaw": "`tracing.categories`",
36                  "name": "`tracing.categories`",
37                  "meta": {
38                    "added": [
39                      "v10.0.0"
40                    ],
41                    "changes": []
42                  },
43                  "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\">&lt;string&gt;</a></li>\n</ul>\n<p>A comma-separated list of the trace event categories covered by this\n<code>Tracing</code> object.</p>",
44                  "type": "module",
45                  "displayName": "`tracing.categories`"
46                },
47                {
48                  "textRaw": "`tracing.disable()`",
49                  "name": "`tracing.disable()`",
50                  "meta": {
51                    "added": [
52                      "v10.0.0"
53                    ],
54                    "changes": []
55                  },
56                  "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('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>",
57                  "type": "module",
58                  "displayName": "`tracing.disable()`"
59                },
60                {
61                  "textRaw": "`tracing.enable()`",
62                  "name": "`tracing.enable()`",
63                  "meta": {
64                    "added": [
65                      "v10.0.0"
66                    ],
67                    "changes": []
68                  },
69                  "desc": "<p>Enables this <code>Tracing</code> object for the set of categories covered by the\n<code>Tracing</code> object.</p>",
70                  "type": "module",
71                  "displayName": "`tracing.enable()`"
72                },
73                {
74                  "textRaw": "`tracing.enabled`",
75                  "name": "`tracing.enabled`",
76                  "meta": {
77                    "added": [
78                      "v10.0.0"
79                    ],
80                    "changes": []
81                  },
82                  "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type\" class=\"type\">&lt;boolean&gt;</a> <code>true</code> only if the <code>Tracing</code> object has been enabled.</li>\n</ul>",
83                  "type": "module",
84                  "displayName": "`tracing.enabled`"
85                }
86              ],
87              "type": "module",
88              "displayName": "`Tracing` object"
89            },
90            {
91              "textRaw": "`trace_events.createTracing(options)`",
92              "name": "`trace_events.createtracing(options)`",
93              "meta": {
94                "added": [
95                  "v10.0.0"
96                ],
97                "changes": []
98              },
99              "desc": "<ul>\n<li>\n<p><code>options</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object\" class=\"type\">&lt;Object&gt;</a></p>\n<ul>\n<li><code>categories</code> <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\">&lt;string[]&gt;</a> An array of trace category names. Values included\nin the array are coerced to a string when possible. An error will be\nthrown if the value cannot be coerced.</li>\n</ul>\n</li>\n<li>Returns: <a href=\"tracing.html#tracing_tracing_object\" class=\"type\">&lt;Tracing&gt;</a>.</li>\n</ul>\n<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('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>",
100              "type": "module",
101              "displayName": "`trace_events.createTracing(options)`"
102            },
103            {
104              "textRaw": "`trace_events.getEnabledCategories()`",
105              "name": "`trace_events.getenabledcategories()`",
106              "meta": {
107                "added": [
108                  "v10.0.0"
109                ],
110                "changes": []
111              },
112              "desc": "<ul>\n<li>Returns: <a href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type\" class=\"type\">&lt;string&gt;</a></li>\n</ul>\n<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('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>",
113              "type": "module",
114              "displayName": "`trace_events.getEnabledCategories()`"
115            }
116          ],
117          "type": "module",
118          "displayName": "The `trace_events` module"
119        }
120      ],
121      "type": "module",
122      "displayName": "Trace Events"
123    }
124  ]
125}