The trace_events.createTracing(options) (Added in v10.0.0) method is an inbuilt application programming interface of the ‘trace_events’ module which creates the Tracing object and returns the same for the given set of categories. The trace_events module contains Tracing objects which is used to enable or disable tracing for sets of categories.
Syntax:
trace_events.createTracing(options)
In order to use this (trace_events.createTracing()) method, we need to import ‘trace_events’ module.
const trace_events = require('trace_events');
Parameters: This function accepts objects as parameters as mentioned above and described below:
-
options <Object>: It accepts an object which contains some categorized string values that are required while tracing.
- categories <string[]> It accepts a string array containing trace category names. If possible then the values are included in the array and persuaded to a string. If the value cannot be persuaded, then an error will be returned.
Return Value<Tracing>: Returns an object which contains categories <string[]> and enabled <boolean> values.
Below programs illustrate the trace_events.createTracing() Method in Node.js:
Example 1: Filename: index.js
// Node.js program to demonstrate the // trace_events.createTracing() methods // Using require to access trace_events module const tracing_events = require( "trace_events" );
// Different types of tracing categories const categories = [ 'myapp.category' ,
'v8' , 'node' , 'node.async_hooks' ,
'node.promises.rejections' ,
'node.vm.script' , 'node.perf.usertiming' ,
'node.perf.timerify'
]; // Now creating tracing for custom // trace category. const newTracing = tracing_events.createTracing( { categories });
// Printing tracing event console.log(newTracing); |
Output:
>> Tracing {
enabled: false,
categories: ‘myapp.category, v8, node, node.async_hooks,
node.promises.rejections, node.vm.script, node.perf.usertiming, node.perf.timerify’
}
Example 2: Filename: index.js
// Node.js program to demonstrate the // trace_events.createTracing() methods // Using require to access trace_events module const { createTracing } = require( 'trace_events' );
// Now create tracing for custom trace category. const tracing = createTracing({ categories: [ 'perf_hooks' ,
'node.promises.rejections' ] });
console.log( "Tracing Created..." );
// Printing tracing event console.log(tracing); // Enabling Tracing event tracing.enable(); // Do some steff here const perf_hooks = require( "perf_hooks" );
perf_hooks.performance.mark( "A" );
() => { perf_hooks.performance.mark( "B" );
perf_hooks.performance.measure( "A to B" , "C" , "D" );
}; // Disabling tracing event tracing.disable(); |
Run index.js file using the following command:
node index.js
Output:
Tracing Created…
Tracing { enabled: false, categories: ‘perf_hooks, node.promises.rejections’ }
Reference: https://nodejs.org/api/tracing.html#tracing_trace_events_createtracing_options