diff --git a/doc/api/tracing.md b/doc/api/tracing.md index e4ab6f16c50221..a0bd4060ca4d51 100644 --- a/doc/api/tracing.md +++ b/doc/api/tracing.md @@ -228,6 +228,48 @@ t2.enable(); console.log(trace_events.getEnabledCategories()); ``` +## Examples + +### Collect trace events data by inspector + +```js +'use strict'; + +const { Session } = require('inspector'); +const session = new Session(); +session.connect(); + +function post(message, data) { + return new Promise((resolve, reject) => { + session.post(message, data, (err, result) => { + if (err) + reject(new Error(JSON.stringify(err))); + else + resolve(result); + }); + }); +} + +async function collect() { + const data = []; + session.on('NodeTracing.dataCollected', (chunk) => data.push(chunk)); + session.on('NodeTracing.tracingComplete', () => { + // done + }); + const traceConfig = { includedCategories: ['v8'] }; + await post('NodeTracing.start', { traceConfig }); + // do something + setTimeout(() => { + post('NodeTracing.stop').then(() => { + session.disconnect(); + console.log(data); + }); + }, 1000); +} + +collect(); +``` + [Performance API]: perf_hooks.md [V8]: v8.md [`Worker`]: worker_threads.md#class-worker