Nodejs Exceptions Monitoring

Custom Segments

Start monitoring your custom code blocks in your Node.js application.

With Palzin Monitor (APM), you have the ability to include any code block you want in the timeline of transactions, providing real-time feedback on the execution of specific code segments within your application.

By default, Palzin Monitor (APM) captures various tasks based on the application's dependencies. However, you can enhance the monitoring by wrapping parts of your code that you consider important, thereby gaining a comprehensive understanding of the statements executed during an execution cycle and their performance.

Here are some ideas on how to use custom segments:

  • Making HTTP calls to external services (webhooks, integrations, etc.)
  • Handling file-related operations (PDF, Excel, images)
  • Performing data manipulation processes (import/export, data aggregation, etc.)

Short Form

Only a few lines of code are needed:

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

This will create a new segment in the timeline:

Codeblock Timeline

A new segment is added to the timeline using the palzin.addSegment() function, which returns a Promise. Refer to the Working with Promise section below for more details.

As shown in the example above, a segment is created with three input parameters:

Parameter Required Description
callback YES The code block that you want to monitor in real-time.
type YES The master category of your segments.
label NO A human-readable label or specific task name that will be shown in the timeline on your Palzin Monitor (APM) dashboard. If not provided, the type is used as the label.

Consider how database queries are reported. The master category is something like mysql, but each query has its own custom label, which is simply an extract of the SQL code executed, enabling better timeline analysis.

  • mysql: Master type
  • SELECT * FROM table...: Label

Using this approach, you can group statements related to a specific task by using something like csv-export as the type parameter and using the filename as the label for each statement of this type.

Return Value

The value you return from your callback function will be returned by the addSegment() method.

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

Working with Promises

The palzin.addSegment() method returns a Promise. If you want to wait for the callback execution, you can use the async/await construct or then/catch.

// Using async/await
await palzin.addSegment(...);

// Using then/catch
palzin.addSegment(...).then().catch();

Adding Contextual Information

If you need to provide additional contextual information, the new segment can be accessed through the callback parameter:

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

FileName);
Last updated: 1 year ago

Want to get started with Palzin Monitor? We offer a no-strings-attached
15 days trial. No credit card required.

It takes less than a minutes to setup your first monitoring.