1- # Tracing
1+ # Trace Events
22
33<!-- introduced_in=v7.7.0-->
44
5+ > Stability: 1 - Experimental
6+
57Trace Event provides a mechanism to centralize tracing information generated by
68V8, Node.js core, and userspace code.
79
8- Tracing can be enabled by passing the ` --trace-events-enabled ` flag when
9- starting a Node.js application.
10-
11- The set of categories for which traces are recorded can be specified using the
12- ` --trace-event-categories ` flag followed by a list of comma separated category
13- names.
10+ Tracing can be enabled with the ` --trace-event-categories ` command-line flag
11+ or by using the trace_events module. The ` --trace-event-categories ` flag accepts
12+ a list of comma-separated category names.
1413
1514The available categories are:
1615
@@ -27,7 +26,32 @@ The available categories are:
2726By default the ` node ` , ` node.async_hooks ` , and ` v8 ` categories are enabled.
2827
2928``` txt
30- node --trace-events-enabled --trace-event-categories v8,node,node.async_hooks server.js
29+ node --trace-event-categories v8,node,node.async_hooks server.js
30+ ```
31+
32+ Prior versions of Node.js required the use of the ` --trace-events-enabled `
33+ flag to enable trace events. This requirement has been removed. However, the
34+ ` --trace-events-enabled ` flag * may* still be used and will enable the
35+ ` node ` , ` node.async_hooks ` , and ` v8 ` trace event categories by default.
36+
37+ ``` txt
38+ node --trace-events-enabled
39+
40+ // is equivalent to
41+
42+ node --trace-event-categories v8,node,node.async_hooks
43+ ```
44+
45+ Alternatively, trace events may be enabled using the ` trace_events ` module:
46+
47+ ``` js
48+ const trace_events = require (' trace_events' );
49+ const tracing = trace_events .createTracing ({ categories: [' node.perf' ] });
50+ tracing .enable (); // Enable trace event capture for the 'node.perf' category
51+
52+ // do work
53+
54+ tracing .disable (); // Disable trace event capture for the 'node.perf' category
3155```
3256
3357Running Node.js with tracing enabled will produce log files that can be opened
@@ -40,12 +64,132 @@ be specified with `--trace-event-file-pattern` that accepts a template
4064string that supports ` ${rotation} ` and ` ${pid} ` . For example:
4165
4266``` txt
43- node --trace-events-enabled --trace-event-file-pattern '${pid}-${rotation}.log' server.js
67+ node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js
4468```
4569
4670Starting with Node.js 10.0.0, the tracing system uses the same time source
4771as the one used by ` process.hrtime() `
4872however the trace-event timestamps are expressed in microseconds,
4973unlike ` process.hrtime() ` which returns nanoseconds.
5074
75+ ## The ` trace_events ` module
76+ <!-- YAML
77+ added: REPLACEME
78+ -->
79+
80+ ### ` Tracing ` object
81+ <!-- YAML
82+ added: REPLACEME
83+ -->
84+
85+ The ` Tracing ` object is used to enable or disable tracing for sets of
86+ categories. Instances are created using the ` trace_events.createTracing() `
87+ method.
88+
89+ When created, the ` Tracing ` object is disabled. Calling the
90+ ` tracing.enable() ` method adds the categories to the set of enabled trace event
91+ categories. Calling ` tracing.disable() ` will remove the categories from the
92+ set of enabled trace event categories.
93+
94+ #### ` tracing.categories `
95+ <!-- YAML
96+ added: REPLACEME
97+ -->
98+
99+ * {string}
100+
101+ A comma-separated list of the trace event categories covered by this
102+ ` Tracing ` object.
103+
104+ #### ` tracing.disable() `
105+ <!-- YAML
106+ added: REPLACEME
107+ -->
108+
109+ Disables this ` Tracing ` object.
110+
111+ Only trace event categories * not* covered by other enabled ` Tracing ` objects
112+ and * not* specified by the ` --trace-event-categories ` flag will be disabled.
113+
114+ ``` js
115+ const trace_events = require (' trace_events' );
116+ const t1 = trace_events .createTracing ({ categories: [' node' , ' v8' ] });
117+ const t2 = trace_events .createTracing ({ categories: [' node.perf' , ' node' ] });
118+ t1 .enable ();
119+ t2 .enable ();
120+
121+ // Prints 'node,node.perf,v8'
122+ console .log (trace_events .getEnabledCategories ());
123+
124+ t2 .disable (); // will only disable emission of the 'node.perf' category
125+
126+ // Prints 'node,v8'
127+ console .log (trace_events .getEnabledCategories ());
128+ ```
129+
130+ #### ` tracing.enable() `
131+ <!-- YAML
132+ added: REPLACEME
133+ -->
134+
135+ Enables this ` Tracing ` object for the set of categories covered by the
136+ ` Tracing ` object.
137+
138+ #### ` tracing.enabled `
139+ <!-- YAML
140+ added: REPLACEME
141+ -->
142+
143+ * {boolean} ` true ` only if the ` Tracing ` object has been enabled.
144+
145+ ### ` trace_events.createTracing(options) `
146+ <!-- YAML
147+ added: REPLACEME
148+ -->
149+
150+ * ` options ` {Object}
151+ * ` categories ` {string[ ] } An array of trace category names. Values included
152+ in the array are coerced to a string when possible. An error will be
153+ thrown if the value cannot be coerced.
154+ * Returns: {Tracing}.
155+
156+ Creates and returns a ` Tracing ` object for the given set of ` categories ` .
157+
158+ ``` js
159+ const trace_events = require (' trace_events' );
160+ const categories = [' node.perf' , ' node.async_hooks' ];
161+ const tracing = trace_events .createTracing ({ categories });
162+ tracing .enable ();
163+ // do stuff
164+ tracing .disable ();
165+ ```
166+
167+ ### ` trace_events.getEnabledCategories() `
168+ <!-- YAML
169+ added: REPLACEME
170+ -->
171+
172+ * Returns: {string}
173+
174+ Returns a comma-separated list of all currently-enabled trace event
175+ categories. The current set of enabled trace event categories is determined
176+ by the * union* of all currently-enabled ` Tracing ` objects and any categories
177+ enabled using the ` --trace-event-categories ` flag.
178+
179+ Given the file ` test.js ` below, the command
180+ ` node --trace-event-categories node.perf test.js ` will print
181+ ` 'node.async_hooks,node.perf' ` to the console.
182+
183+ ``` js
184+ const trace_events = require (' trace_events' );
185+ const t1 = trace_events .createTracing ({ categories: [' node.async_hooks' ] });
186+ const t2 = trace_events .createTracing ({ categories: [' node.perf' ] });
187+ const t3 = trace_events .createTracing ({ categories: [' v8' ] });
188+
189+ t1 .enable ();
190+ t2 .enable ();
191+
192+ console .log (trace_events .getEnabledCategories ());
193+ ```
194+
51195[ Performance API ] : perf_hooks.html
0 commit comments