Skip to content
Related Articles

Related Articles

Node.js Tracing Objects
  • Last Updated : 09 Feb, 2021

The Tracing Objects (Added in v10.0.0) are used for a set of categories to enable and disable the tracing. When tracing events are created then tracing objects is disabled by calling tracing.enable() method and then categories are added to set of enabled trace and can be accessed by calling tracing.categories.

Some of the Tracing objects are mentioned below:

  1. tracing.categories (Added in v10.0.0): object is an inbuilt application programming interface of the ‘trace_events’ module which returns trace event categories which is a comma-separated list that is covered by this Tracing object.  

    Syntax:

    tracing.categories

    Parameters: This object does not accept any parameters as mentioned above.



    Return Value <string>: It returns the trace event categories list separated by a comma.

  2. tracing.disable() (Added in v10.0.0): object is an inbuilt application programming interface of the ‘trace_events’ module which disables the Tracing object which was enabled before by tracing.enable() method.

    Syntax:

    tracing.disable()

    Parameters: This object does not accept any parameters.

    Return Value: It does not return any value instead disables the trace events.

  3. tracing.enable() (Added in v10.0.0): object is an inbuilt application programming interface of the ‘trace_events’ module which enables the Tracing object for the set of categories which was enabled before by tracing.enable() method.

    Syntax:

    tracing.enable()

    Parameters: This object does not accept any parameters.



    Return Value: It does not return any value instead enables trace events.

  4. tracing.enabled (Added in v10.0.0): This object traces whether trace events are enabled or disabled and returns back the boolean value accordingly. 

    Syntax:

    tracing.enabled

    Parameters: This object does not accept any parameters.

    Return Value <boolean>: It returns true only if the Tracing object has been enabled.

Below programs illustrate the Tracing Objects in Node.js:

Install trace_events module:

npm install trace_events

Example 1: Filename: index.js




// Node.js program to demonstrate the 
// Tracing methods 
  
// Using require to access trace_events module 
const trace_events = require('trace_events'); 
  
const newTraceEvent1 = trace_events.createTracing({ 
categories: ['node.perf.usertiming', 'v8'] });
  
const { createTracing } = require('trace_events');
  
const newTraceEvent2 = createTracing({ 
categories: ['node', 'node.promises.rejections'] });
  
newTraceEvent1.enable();
newTraceEvent2.enable();
  
// Prints newTraceEvent1 trace event
console.log(newTraceEvent1);
  
// Prints newTraceEvent2 trace event
console.log(newTraceEvent2);
  
// Prints all enabled categories
console.log(trace_events.getEnabledCategories());
  
// Disable 'node, node.promises.rejections' category
newTraceEvent2.disable(); 
  
// Prints 'node.perf.usertiming, v8'
console.log(trace_events.getEnabledCategories());
  
// Disable 'node.perf.usertiming' category
newTraceEvent1.disable();

Run index.js file using the following command:

node index.js

Output:

Tracing { enabled: true, categories: ‘node.perf.usertiming, v8’ }
Tracing { enabled: true, categories: ‘node, node.promises.rejections’ }
node, node.perf.usertiming, node.promises.rejections, v8
node.perf.usertiming, v8

Example 2: Filename: index.js




// Node.js program to demonstrate the 
// Trace methods 
  
// Using require to access trace_events module 
const trace_events = require("trace_events");
  
// Tracing categories
const categories = [ 'myapp.category', 'v8', 'node'
'node.async_hooks', 'node.promises.rejections', 'node.vm.script'
'node.perf.usertiming', 'node.perf.timerify'];
  
// Now create tracing for custom trace category.
const newTracing = trace_events.createTracing({ categories });
  
// Printing tracing event
console.log(newTracing);
  
// Printing tracing categories
console.log(newTracing.categories);
  
// Checking wheather trace is enabled or not
console.log(newTracing.enabled);
  
// Enabling newTracing
newTracing.enable();
  
// Printing tracing categories
console.log(newTracing.categories);
  
// Checking wheather trace is enabled or not
console.log(newTracing.enabled);
  
// Do some stuff
const perf_hooks = require("perf_hooks");
perf_hooks.performance.mark("Alfa");
() => {
  perf_hooks.performance.mark("Beta");
  perf_hooks.performance.measure(
      "Alfa to Beta", "Alfa", "Beta");
};
  
// Prints performance stuff
console.log(perf_hooks.performance);
  
// Disables newTracing
newTracing.disable();

Run index.js file using the following command:

node index.js
For custom tracing
node --trace-event-categories v8, node, node.async_hooks index.js

Output:

>>Tracing { enabled: false, categories: ‘myapp.category, ………., node.perf.timerify’}
>>myapp.category, v8, ……….., node.perf.timerify
>>false
>>myapp.category, v8, ………, node.perf.timerify
>>true
>>{ nodeTiming: {   name: ‘node’, …loopExit: -1 },
 timeOrigin: 1596967720328.603}

Tracing in Browser: In Google Chrome enter URL chrome://tracing. Now, Click the Load button, and load the file for tracing.

Reference: https://nodejs.org/api/tracing.html#tracing_tracing_object

My Personal Notes arrow_drop_up
Recommended Articles
Page :