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+ Tracing can be enabled with the ` --trace-event-categories ` command-line flag
11+ or by using the ` trace_events ` module .
1012
1113The set of categories for which traces are recorded can be specified using the
1214` --trace-event-categories ` flag followed by a list of comma separated category
@@ -27,7 +29,32 @@ The available categories are:
2729By default the ` node ` , ` node.async_hooks ` , and ` v8 ` categories are enabled.
2830
2931``` txt
30- node --trace-events-enabled --trace-event-categories v8,node,node.async_hooks server.js
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 `
36+ flag to enable trace events. This requirement has been removed. However, the
37+ ` --trace-events-enabled ` flag * may* still be used and will enable the
38+ ` node ` , ` node.async_hooks ` , and ` v8 ` trace event categories by default.
39+
40+ ``` txt
41+ node --trace-events-enabled
42+
43+ // is equivalent to
44+
45+ node --trace-event-categories v8,node,node.async_hooks
46+ ```
47+
48+ Alternatively, trace events may be enabled using the ` trace_events ` module:
49+
50+ ``` js
51+ const trace_events = require (' trace_events' );
52+ const tracing = trace_events .createTracing ({ categories: [' node.perf' ] });
53+ tracing .enable (); // Enable trace event capture for the 'node.perf' category
54+
55+ // do work
56+
57+ tracing .disable (); // Disable trace event capture for the 'node.perf' category
3158```
3259
3360Running Node.js with tracing enabled will produce log files that can be opened
@@ -40,12 +67,139 @@ be specified with `--trace-event-file-pattern` that accepts a template
4067string that supports ` ${rotation} ` and ` ${pid} ` . For example:
4168
4269``` txt
43- node --trace-events-enabled --trace-event-file-pattern '${pid}-${rotation}.log' server.js
70+ node --trace-event-categories v8 --trace-event-file-pattern '${pid}-${rotation}.log' server.js
4471```
4572
4673Starting with Node.js 10.0.0, the tracing system uses the same time source
4774as the one used by ` process.hrtime() `
4875however the trace-event timestamps are expressed in microseconds,
4976unlike ` process.hrtime() ` which returns nanoseconds.
5077
78+ ## The ` trace_events ` module
79+ <!-- YAML
80+ added: REPLACEME
81+ -->
82+
83+ ### ` Tracing ` object
84+ <!-- YAML
85+ added: REPLACEME
86+ -->
87+
88+ The ` Tracing ` object is used to enable or disable tracing for sets of
89+ categories. Instances are created using the ` trace_events.createTracing() `
90+ method.
91+
92+ When created, the ` Tracing ` object is disabled. Calling the
93+ ` tracing.enable() ` method adds the categories to the set of enabled trace event
94+ categories. Calling ` tracing.disable() ` will remove the categories from the
95+ set of enabled trace event categories.
96+
97+ #### ` tracing.categories `
98+ <!-- YAML
99+ added: REPLACEME
100+ -->
101+
102+ * {string}
103+
104+ A comma-separated list of the trace event categories covered by this
105+ ` Tracing ` object.
106+
107+ #### ` tracing.disable() `
108+ <!-- YAML
109+ added: REPLACEME
110+ -->
111+
112+ Disables this ` Tracing ` object.
113+
114+ Only trace event categories * not* covered by other enabled ` Tracing ` objects
115+ and * not* specified by the ` --trace-event-categories ` flag will be disabled.
116+
117+ ``` js
118+ const trace_events = require (' trace_events' );
119+ const t1 = trace_events .createTracing ({ categories: [' node' , ' v8' ] });
120+ const t2 = trace_events .createTracing ({ categories: [' node.perf' , ' node' ] });
121+ t1 .enable ();
122+ t2 .enable ();
123+
124+ // Prints 'node,node.perf,v8'
125+ console .log (trace_events .getEnabledCategories ());
126+
127+ t2 .disable (); // will only disable emission of the 'node.perf' category
128+
129+ // Prints 'node,v8'
130+ console .log (trace_events .getEnabledCategories ());
131+ ```
132+
133+ #### ` tracing.enable() `
134+ <!-- YAML
135+ added: REPLACEME
136+ -->
137+
138+ Enables this ` Tracing ` object for the set of categories covered by the
139+ ` Tracing ` object.
140+
141+ #### ` tracing.enabled `
142+ <!-- YAML
143+ added: REPLACEME
144+ -->
145+
146+ * {boolean} ` true ` only if the ` Tracing ` object has been enabled.
147+
148+ ### ` trace_events.createTracing(options) `
149+ <!-- YAML
150+ added: REPLACEME
151+ -->
152+
153+ * ` options ` {Object}
154+ * ` categories ` {string[ ] } An array of trace category names
155+ * Returns: {Tracing}.
156+
157+ Creates and returns a ` Tracing ` object for the given set of ` categories ` .
158+
159+ ``` js
160+ const trace_events = require (' trace_events' );
161+ const categories = [' node.perf' , ' node.async_hooks' ];
162+ const tracing = trace_events .createTracing ({ categories });
163+ tracing .enable ();
164+ // do stuff
165+ tracing .disable ();
166+ ```
167+
168+ ### ` trace_events.getEnabledCategories() `
169+ <!-- YAML
170+ added: REPLACEME
171+ -->
172+
173+ * Returns: {string}
174+
175+ Returns a comma-separated list of all currently-enabled trace event
176+ categories. The current set of enabled trace event categories is determined
177+ by the * union* of all currently-enabled ` Tracing ` objects and any categories
178+ enabled using the ` --trace-event-categories ` flag.
179+
180+ Given the file ` test.js ` below, the command
181+ ` node --trace-event-categories node.perf test.js ` will print
182+ ` 'node.async_hooks,node.perf' ` to the console.
183+
184+ ``` js
185+ const trace_events = require (' trace_events' );
186+ const t1 = trace_events .createTracing ({ categories: [' node.async_hooks' ] });
187+ const t2 = trace_events .createTracing ({ categories: [' node.perf' ] });
188+ const t3 = trace_events .createTracing ({ categories: [' v8' ] });
189+
190+ t1 .enable ();
191+ t2 .enable ();
192+
193+ console .log (trace_events .getEnabledCategories ());
194+ ```
195+
196+ ### ` trace_events.getEnabledTracingObjects() `
197+ <!-- YAML
198+ added: REPLACEME
199+ -->
200+
201+ * Returns: {Tracing[ ] }
202+
203+ Returns an array of currently enabled ` Tracing ` objects.
204+
51205[ Performance API ] : perf_hooks.html
0 commit comments