Custom Segments
Monitor custom code blocks inside your application.

What are segments?

Segments represents the tasks performed during the current execution cycle.
Thanks to Inspector you are able to put everything you want in your transaction's timeline getting a real-time feedback about the execution of a custom code block inside your app:
  • Http call to external services (webhooks, integration, etc.)
  • Functions that deal with files (pdf, excel, images)
  • Data manipulation processes (import/export, data aggregation, etc.)
and anything else you think is relevant to monitor how your application is behaving.

Add Segments (short form)

This is the most easy way to wrap a code block into a segment:
1
$inspector->addSegment(function () {
2
3
// Code block that you want add to the transaction timeline.
4
// DB queries, http calls, etc.
5
6
}, 'type', 'label');
Copied!
The code block inside the callback will be transparently executed, and your script will continue its execution normally. Inspector will works under the hood to profile execution performance.
Here is an example of how the segment will appear in the timeline:
A new segment in the transaction's timeline
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's name that will be used as label inside the timeline. Otherwise type is used.
Think about how database queries is reported. There is one master category like mysql but each query has its own custom label that simply is an extract of the sql code.
  • mysql: master type
  • select * from table: task name
In this way you can mark toghether the statements related to the same topic or service, using something like mysql as type parameter and use the specific query string or task name as label for each statements of this type.

Add Segments (extended code)

You can use startSegment() method on the Inspector instance to manually control the monitoring of a code block:
1
/*
2
* Create a new "segment" that will be automatically attached
3
* to the current transaction.
4
*/
5
$segment = $inspector->startSegment('type', 'label');
6
7
try {
8
9
/*
10
* Here is your code
11
*/
12
13
} catch(UnauthorizedException $exception) {
14
15
/*
16
* Report the exception to Inspector for diagnosis purpose.
17
*/
18
$inspector->reportException($exception);
19
20
} finally {
21
/*
22
* Close the code block with end() method to stop monitoring.
23
*/
24
$segment->end();
25
}
Copied!
As you can see in the example above we use try/catch/finally to be sure that your code block is monitored in a consistent way.
Or you can use addSegment() that implements this strategy for you as seen above:
1
$inspector->addSegment(function () {
2
3
/*
4
* Write here your code block
5
*/
6
7
}, 'type', 'label');
Copied!

Segments tips and tricks

It may happen that you aren't able to wrap directly the statement to trace performance, so it might be useful to add a segment after the task has been executed.
In this case you can create the segment manually passing the start time in the start() method, and the duration in the end() method, as shown in the example below:
1
/*
2
* Http call to an external url could be perfromed with following result.
3
*/
4
$url = 'https://www.inspector.dev';
5
$duration_ms = 10; // Duration in milliseconds
6
7
/**
8
* -----------------------------------------
9
* Create the segment manually setting timing information.
10
* -----------------------------------------
11
*/
12
13
// convert duration in seconds
14
$timespamp_start = microtime(true) - ($duration_ms/1000)
15
16
$inspector->startSegment('api', $url)
17
->start($timespamp_start)
18
->end($duration_ms);
Copied!

Access the Inspector instance

If you are using a framework specific library (Laravel, Symfony) you have several options to access the current Inspector instance to call addSegment() method. Check it out on their specific documetation:
Last modified 1mo ago