Open In App

Node.js trace_events.getEnabledCategories() Method

Last Updated : 19 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The trace_events.getEnabledCategories() (Added in v10.0.0) method is an inbuilt application programming interface of the ‘trace_events’ module which is used in showing the current set of enabled trace event categories that is determined by the union of all currently-enabled Tracing objects. By default, it’s status is disabled by v8 runtime and all enabled categories are fetch using the –trace-event-categories flag. 

Syntax:

trace_events.getEnabledCategories()

In order to use this method, we need to create trace events using (trace_events.createTracing()) method, and we need to import the ‘trace_events’ module.

const trace_events = require('trace_events');  

Parameters: This method does not accept any parameters.

Return Value <string>: It returns a list of all currently-enabled trace event categories which are separated by commas.

Example 1: Filename: index.js




// Node.js program to demonstrate the 
// trace_events.getEnabledCategories() method 
  
// Using require to access trace_events module 
const trace_events = require('trace_events');
  
// Creating trace events
const trace_event1 = trace_events
    .createTracing({ categories: ['node'] });
const trace_event2 = trace_events.createTracing({
    categories: ['node.promises.rejections']
});
const { createTracing } = require('trace_events');
  
// Another way to create Tracing events
const trace_event3 = createTracing(
        { categories: ['node.vm.script'] });
  
// Enabling the trace_event1
trace_event1.enable();
  
// Enabling the trace_event2
trace_event2.enable();
  
// Printing the enabled categories
console.log("Enabled categories: "
    trace_events.getEnabledCategories());
  
// Disabling the trace_event1 
trace_event1.disable();
  
// Disabling the trace_event2 
trace_event2.disable();


Output:

Enabled categories node,node.promises.rejections

Example 2: Filename: index.js




// Node.js program to demonstrate the 
// trace_events.getEnabledCategories() method 
  
// 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 tracing = trace_events.createTracing({ categories });
  
// Enabling Tracing event
tracing.enable();
  
// Printing tracing event
console.log(">> ", tracing);
  
// Returns false if any tracing event is enabled 
console.log(">> ", !trace_events.getEnabledCategories());
  
if (trace_events.getEnabledCategories()) {
    console.log(">> Following tracing events are enabled",
        trace_events.getEnabledCategories());
} else {
    console.log(">> None of the Tracing events are enabled");
}
  
// Disabling tracing event
tracing.disable();
  
if (trace_events.getEnabledCategories()) {
    console.log(">> Following tracing events are enabled",
        trace_events.getEnabledCategories());
} else {
    console.log(">> None of the Tracing events are enabled");
}


Run index.js file using the following command:

node index.js

Output:

>>  Tracing { enabled: true,

 categories:’myapp.category,v8,node,node.async_hooks,node.promises.rejections,node.vm.script,node.perf.usertiming,node.perf.timerify’}

>>  false

>> Following tracing events are enabled : myapp.category,node,node.async_hooks,node.perf.timerify,node.perf.usertiming,node.promises.rejections,node.vm.script,v8

>> None of the Tracing events are enabled

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads