Skip to content

Commit 0c7fd73

Browse files
committed
trace_events: adds a new trace_events api
Removes the requirement to use `--trace-events-enabled` to enable trace events. Tracing is enabled automatically if there are any enabled categories. Adds a new `trace_events` module with an API for enabling/disabling trace events at runtime without a command line flag. ```js const trace_events = require('trace_events'); const categories = [ 'node.perf', 'node.async_hooks' ]; const tracing = trace_events.createTracing({ categories }); tracing.enable(); // do stuff tracing.disable(); ``` Multiple `Tracing` objects may exist and be enabled at any point in time. The enabled trace event categories is the union of all enabled `Tracing` objects and the `--trace-event-categories` flag.
1 parent 887f4c5 commit 0c7fd73

25 files changed

+677
-83
lines changed

doc/api/_toc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* [String Decoder](string_decoder.html)
4747
* [Timers](timers.html)
4848
* [TLS/SSL](tls.html)
49-
* [Tracing](tracing.html)
49+
* [Trace Events](tracing.html)
5050
* [TTY](tty.html)
5151
* [UDP/Datagram](dgram.html)
5252
* [URL](url.html)

doc/api/errors.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,18 @@ socket, which is only valid from a client.
15311531

15321532
An attempt was made to renegotiate TLS on a socket instance with TLS disabled.
15331533

1534+
<a id="ERR_TRACE_EVENTS_CATEGORY_REQUIRED"></a>
1535+
### ERR_TRACE_EVENTS_CATEGORY_REQUIRED
1536+
1537+
The `trace_events.createTracing()` method requires at least one trace event
1538+
category.
1539+
1540+
<a id="ERR_TRACE_EVENTS_UNAVAILABLE"></a>
1541+
### ERR_TRACE_EVENTS_UNAVAILABLE
1542+
1543+
The `trace_events` module could not be loaded because Node.js was compiled with
1544+
the `--without-v8-platform` flag.
1545+
15341546
<a id="ERR_TRANSFORM_ALREADY_TRANSFORMING"></a>
15351547
### ERR_TRANSFORM_ALREADY_TRANSFORMING
15361548

doc/api/tracing.md

Lines changed: 159 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# Tracing
1+
# Trace Events
22

33
<!--introduced_in=v7.7.0-->
44

5+
> Stability: 1 - Experimental
6+
57
Trace Event provides a mechanism to centralize tracing information generated by
68
V8, 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

1113
The 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:
2729
By 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

3360
Running 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
4067
string 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

4673
Starting with Node.js 10.0.0, the tracing system uses the same time source
4774
as the one used by `process.hrtime()`
4875
however the trace-event timestamps are expressed in microseconds,
4976
unlike `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

lib/internal/errors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,9 @@ E('ERR_TLS_REQUIRED_SERVER_NAME',
982982
E('ERR_TLS_SESSION_ATTACK', 'TLS session renegotiation attack detected', Error);
983983
E('ERR_TLS_SNI_FROM_SERVER',
984984
'Cannot issue SNI from a TLS server-side socket', Error);
985+
E('ERR_TRACE_EVENTS_CATEGORY_REQUIRED',
986+
'At least one category is required', TypeError);
987+
E('ERR_TRACE_EVENTS_UNAVAILABLE', 'Trace events are unavailable', Error);
985988
E('ERR_TRANSFORM_ALREADY_TRANSFORMING',
986989
'Calling transform done when still transforming', Error);
987990

lib/internal/modules/cjs/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ const builtinLibs = [
101101
'assert', 'async_hooks', 'buffer', 'child_process', 'cluster', 'crypto',
102102
'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'http2', 'https', 'net',
103103
'os', 'path', 'perf_hooks', 'punycode', 'querystring', 'readline', 'repl',
104-
'stream', 'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'
104+
'stream', 'string_decoder', 'tls', 'trace_events', 'tty', 'url', 'util',
105+
'v8', 'vm', 'zlib'
105106
];
106107

107108
if (typeof process.binding('inspector').open === 'function') {

lib/trace_events.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
const { hasTracing } = process.binding('config');
4+
const kHandle = Symbol('handle');
5+
const kEnabled = Symbol('enabled');
6+
const kCategories = Symbol('categories');
7+
8+
const {
9+
ERR_TRACE_EVENTS_CATEGORY_REQUIRED,
10+
ERR_TRACE_EVENTS_UNAVAILABLE,
11+
ERR_INVALID_ARG_TYPE
12+
} = require('internal/errors').codes;
13+
14+
if (!hasTracing)
15+
throw new ERR_TRACE_EVENTS_UNAVAILABLE();
16+
17+
const { CategorySet, getEnabledCategories } = process.binding('trace_events');
18+
const { customInspectSymbol } = require('internal/util');
19+
const { format } = require('util');
20+
21+
const enabledTracingObjects = new Set();
22+
23+
class Tracing {
24+
constructor(categories) {
25+
this[kHandle] = new CategorySet(categories);
26+
this[kCategories] = categories;
27+
this[kEnabled] = false;
28+
}
29+
30+
enable() {
31+
if (!this[kEnabled]) {
32+
this[kEnabled] = true;
33+
this[kHandle].enable();
34+
enabledTracingObjects.add(this);
35+
}
36+
}
37+
38+
disable() {
39+
if (this[kEnabled]) {
40+
this[kEnabled] = false;
41+
this[kHandle].disable();
42+
enabledTracingObjects.delete(this);
43+
}
44+
}
45+
46+
get enabled() {
47+
return this[kEnabled];
48+
}
49+
50+
get categories() {
51+
return this[kCategories].join(',');
52+
}
53+
54+
[customInspectSymbol](depth, opts) {
55+
const obj = {
56+
enabled: this.enabled,
57+
categories: this.categories
58+
};
59+
return `Tracing ${format(obj)}`;
60+
}
61+
}
62+
63+
function createTracing(options) {
64+
if (typeof options !== 'object' || options == null)
65+
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
66+
67+
if (!Array.isArray(options.categories)) {
68+
throw new ERR_INVALID_ARG_TYPE('options.categories', 'string[]',
69+
options.categories);
70+
}
71+
72+
if (options.categories.length <= 0)
73+
throw new ERR_TRACE_EVENTS_CATEGORY_REQUIRED();
74+
75+
return new Tracing(options.categories);
76+
}
77+
78+
function getEnabledTracingObjects() {
79+
return Array.from(enabledTracingObjects);
80+
}
81+
82+
module.exports = {
83+
createTracing,
84+
getEnabledCategories,
85+
getEnabledTracingObjects
86+
};

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
'lib/tls.js',
7474
'lib/_tls_common.js',
7575
'lib/_tls_wrap.js',
76+
'lib/trace_events.js',
7677
'lib/tty.js',
7778
'lib/url.js',
7879
'lib/util.js',

src/env-inl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "v8.h"
3333
#include "node_perf_common.h"
3434
#include "node_context_data.h"
35+
#include "tracing/agent.h"
3536

3637
#include <stddef.h>
3738
#include <stdint.h>
@@ -325,6 +326,10 @@ inline v8::Isolate* Environment::isolate() const {
325326
return isolate_;
326327
}
327328

329+
inline tracing::Agent* Environment::tracing_agent() const {
330+
return tracing_agent_;
331+
}
332+
328333
inline Environment* Environment::from_immediate_check_handle(
329334
uv_check_t* handle) {
330335
return ContainerOf(&Environment::immediate_check_handle_, handle);

src/env.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "node_buffer.h"
44
#include "node_platform.h"
55
#include "node_file.h"
6+
#include "tracing/agent.h"
67

78
#include <stdio.h>
89
#include <algorithm>
@@ -87,9 +88,11 @@ void InitThreadLocalOnce() {
8788
}
8889

8990
Environment::Environment(IsolateData* isolate_data,
90-
Local<Context> context)
91+
Local<Context> context,
92+
tracing::Agent* tracing_agent)
9193
: isolate_(context->GetIsolate()),
9294
isolate_data_(isolate_data),
95+
tracing_agent_(tracing_agent),
9396
immediate_info_(context->GetIsolate()),
9497
tick_info_(context->GetIsolate()),
9598
timer_base_(uv_now(isolate_data->event_loop())),

0 commit comments

Comments
 (0)