Custom Segments
Monitor custom code blocks in your nodejs application.
Thanks to Inspector you can put everything you want in the transactions timeline getting a real-time feedback about the execution of a code block inside your application.
By default Inspector will report many different tasks based on the application's dependencies, but you are free to "wrap" some parts of your code that you consider relevant to create a more complete picture of what statements are executed during an execution cycle and their performance.
Some ideas on how to use custom segments:
- Http call to external services (webhooks, integrations, etc.);
- Functions that deal with files (pdf, excel, images);
- Data manipulation processes (import/export, data aggregation, etc).
Just a few lines of code are needed:
inspector.addSegment(() => {
// Your statements here...
}, 'type', 'label');
This will produce a new segment in the timeline:

A new segment in the timeline
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.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);
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();
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 modified 7mo ago