-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
trace_events: enable trace events implicitly, add module #19233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
54d42e0
e3108b4
422580f
d366cc6
b3cf556
03dcd24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,9 @@ | |
Trace Event provides a mechanism to centralize tracing information generated by | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't have to be in this PR, but we really need to rename this file to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, definitely :-) ... I can mark that as a todo for another PR. |
||
V8, Node.js core, and userspace code. | ||
|
||
Tracing can be enabled by passing the `--trace-events-enabled` flag when | ||
starting a Node.js application. | ||
|
||
The set of categories for which traces are recorded can be specified using the | ||
`--trace-event-categories` flag followed by a list of comma separated category | ||
names. | ||
Tracing can be enabled either by using the `--trace-event-categories` flag | ||
followed by a list of comma separated category names, or by using the | ||
`trace_events.setTracingCategories()` method. | ||
|
||
The available categories are: | ||
|
||
|
@@ -23,28 +20,91 @@ The available categories are: | |
measurements. | ||
* `v8` | ||
|
||
By default the `node`, `node.async_hooks`, and `v8` categories are enabled. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
|
||
```txt | ||
node --trace-events-enabled --trace-event-categories v8,node,node.async_hooks server.js | ||
node --trace-event-categories v8,node,node.async_hooks server.js | ||
``` | ||
|
||
```js | ||
const trace_events = require('trace_events'); | ||
trace_events.enableTracingCategories('v8', 'node,node.async_hooks'); | ||
``` | ||
|
||
Running Node.js with tracing enabled will produce log files that can be opened | ||
in the [`chrome://tracing`](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool) | ||
tab of Chrome. | ||
in the [`chrome://tracing`][] tab of Chrome. | ||
|
||
The logging file is by default called `node_trace.${rotation}.log`, where | ||
`${rotation}` is an incrementing log-rotation id. The filepath pattern can | ||
be specified with `--trace-event-file-pattern` that accepts a template | ||
string that supports `${rotation}` and `${pid}`. For example: | ||
|
||
```txt | ||
node --trace-events-enabled --trace-event-file-pattern '${pid}-${rotation}.log' server.js | ||
node --trace-event-file-pattern '${pid}-${rotation}.log' server.js | ||
``` | ||
|
||
Starting with Node.js 10.0.0, the tracing system uses the same time source | ||
as the one used by `process.hrtime()` | ||
however the trace-event timestamps are expressed in microseconds, | ||
unlike `process.hrtime()` which returns nanoseconds. | ||
|
||
Previous versions of Node.js required use of the `--trace-events-enabled` | ||
command line flag to enable trace event capture. This flag is no longer | ||
required to enable trace events. If it is used, however, trace events are | ||
enabled using the default categories: `v8`, `node`, and `node.async_hooks`. | ||
|
||
## The `trace_events` module | ||
|
||
The `trace_events` module may be used to turn trace event reporting on and off. | ||
|
||
It is accessible using `require('trace_events')`. | ||
|
||
### trace_events.disableTracingCategories(...categories) | ||
|
||
* `...categories` {string} One or more category names. | ||
|
||
Disables trace event capture for the given set of category names. | ||
|
||
An error will be thrown if trace event support is not enabled in the Node.js | ||
binary. | ||
|
||
```js | ||
const trace_events = require('trace_events'); | ||
trace_events.disableTracingCategories('v8', 'node.async_hooks'); | ||
``` | ||
|
||
To disable all currently enabled trace event categories, the | ||
`trace_events.getTracingCategories()` method may be used together with | ||
`trace_events.disableTracingCategories()`: | ||
|
||
```js | ||
const trace_events = require('trace_events'); | ||
trace_events.disableTracingCategories( | ||
...trace_events.getTracingCategories()); | ||
``` | ||
|
||
### trace_events.enableTracingCategories(...categories) | ||
|
||
* `...categories` {string} One or more category names. | ||
|
||
Enables trace event capture for the given set of category names. | ||
|
||
An error will be thrown if trace event support is not enabled in the Node.js | ||
binary. | ||
|
||
```js | ||
const trace_events = require('trace_events'); | ||
trace_events.enableTracingCategories('v8', 'node.async_hooks'); | ||
``` | ||
|
||
### trace_events.getTracingCategories() | ||
|
||
* Returns: {string[]} | ||
|
||
Returns an Array of the currently enabled trace event categories. | ||
|
||
```js | ||
const trace_events = require('trace_events'); | ||
console.log(trace_events.getTracingCategories()); | ||
``` | ||
|
||
[`chrome://tracing`]: https://www.chromium.org/developers/how-tos/trace-event-profiling-tool | ||
[Performance API]: perf_hooks.html |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const { | ||
enableTracingCategories: _enableTracingCategories, | ||
disableTracingCategories: _disableTracingCategories, | ||
getTracingCategories | ||
} = process.binding('trace_events'); | ||
|
||
const { trace_events_enabled } = process.binding('config'); | ||
|
||
const { | ||
codes: { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_TRACE_EVENTS_UNAVAILABLE | ||
} | ||
} = require('internal/errors'); | ||
|
||
function enableTracingCategories(...categories) { | ||
if (!trace_events_enabled) | ||
throw new ERR_TRACE_EVENTS_UNAVAILABLE(); | ||
|
||
for (var n = 0; n < categories.length; n++) { | ||
if (typeof categories[n] !== 'string') | ||
throw new ERR_INVALID_ARG_TYPE('category', 'string'); | ||
} | ||
|
||
_enableTracingCategories(categories); | ||
} | ||
|
||
function disableTracingCategories(...categories) { | ||
if (!trace_events_enabled) | ||
throw new ERR_TRACE_EVENTS_UNAVAILABLE(); | ||
|
||
for (var n = 0; n < categories.length; n++) { | ||
if (typeof categories[n] !== 'string') | ||
throw new ERR_INVALID_ARG_TYPE('category', 'string'); | ||
} | ||
|
||
_disableTracingCategories(categories); | ||
} | ||
|
||
module.exports = { | ||
enableTracingCategories, | ||
disableTracingCategories, | ||
getTracingCategories | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding: