Custom Segments

Monitor code blocks inside your application.

Thanks to Inspector you are able to put everything you want in your transactions timeline getting a real-time feedback about the execution of a code block inside your application.

$segment = Inspector::startSegment('type', 'label');
// Your statement here...
$segment->end();

This will produce a new segment in the timeline:

A new segment in the transaction's timeline

By default Inspector will report database queries, jobs, emails sent and so on, 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 application transaction.

How to create a segment

To create a new segment you can use Inspector facade or inspector helper function:

// using Inspector Facade
$segment = \Inspector::startSegment('type', 'label');
// or using the helper function
$segment = inspector()->startSegment('type', 'label');

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

Parameter

Required

Description

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 databse 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.

  • mysql: master type

  • select * from table: task name

In this way you can mark toghether the statements related to an external service using something like activecampaign-api as type parameter and use the specific url or task name as label for each statements of this type.

$segment = \Inspector::startSegment('activecampaign-api', 'Add Tag');

Terminate a Segment

Each segment must be completed calling the end() method.

To allows Inspector to do its job it's mandatory to call the end() method on the segment instance. To be sure to accomplish this task you can use the finally statement in a try - catch block:

try {
$segment = Inspector::startSegment('activecampaign-api', 'Add Tag');
// Your statememnt here...
} catch(GuzzleException $exception) {
Inspector::reportException($exception);
} finally {
$segment->end();
}

This is the safest way to monitor a code block in your application.

Via Callback

Inspector provides you the addSegment method that implement this strategy for you. You just need to pass a callback that will be autmatically monitored also if your code will fires an exception. If you need to add some context information, the new segment will be injected as callback parameter:

inspector()->addSegment(function ($segment) {
// Your code block here...
}, 'activecampaign-api', 'Add Tag');