Custom Segments

Monitor custom code blocks in your nodejs application.

Add a Segment (short form)

Just a few lines of code are needed:

inspector.addSegment(() => {
    
    // Your statements here...
    
}, 'type', 'label');

This will produce a new segment in the timeline:

inspector.addSegment() return a Promise. See the Working with Promise section below to learn more.

As showed in the example above a new segment is built with three input parameters:

Parameter

Required

Description

callback

YES

The code block you want keep under real-time monitoring

type

YES

This is the master category of your segments

label

NO

Human readable label or specific task name that will be showed inside the timeline in your Inspector dashboard. If it isn't provided type is used as label.

Think about how database queries is reported. That's one master category like mysql but each query has its own custom label that simply is an extract of the sql code executed for a better timeline consultation.

  • mysql: master type

  • select * from table...: label

In this way you can mark toghether the statements related to a specific task using something like csv-export as type parameter and use the filename as label for each statement of this type.

Get return

What you return from your callback will be returned back by addSegment() method.

/**
 * "result " will contains the string "Hello".
 * 
 * Note the use of "await".
 */
let result = await inspector.addSegment(() => {
    
    // Your statements here...
    
    return 'Hello';
    
}, 'csv-export', csvFileName);

Working with Promise

inspector.addSegment() return a Promise. If you want wait the execution of the callback you need to use async/await construct or then/catch.

// Use await
await inspector.addSegment(...);

// Use then/catch
inspector.addSegment(...)
    .then()
    .catch();

Add contextual informations

If you need to report some contextual information, the new segment will be injected as callback parameter:

/**
 * "result " will contains the string "Hello"
 */
let result = await inspector.addSegment(segment => {
    
    // Your statements here...
    
    // Attach interesting data
    segment.addContext('payload', {
        foo: 'bar'
    });
    
    // return a result if needed
    return 'Hello';
    
}, 'csv-export', csvFileName);

Last updated