Related Articles

Related Articles

Node.js trace_events.createTracing() Method
  • Last Updated : 19 Aug, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();

chevron_right


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




My Personal Notes arrow_drop_up
Recommended Articles
Page :