Usage

How to start monitoring your script.

Transactions

All start with a transaction. Transaction represent an execution cycle and it can contains one or hundred of segments.

It should be started as soon as possible in your application to better represent the time your script takes to fulfill the request.

$transaction = $inspector->startTransaction($_SERVER['PATH_INFO']);
/*
* A transaction should start in your "index" file
* and closed when the execution is completed.
*/
$transaction->end();

Segments

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 code block inside your app:

$segment = $inspector->startSegment('type', 'label');
// Code block that you want to monitor in real-time.
$segment->end();

This will produce a new segment 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 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 the same service or task using something like mysql as type parameter and use the specific query string or task name as label for each statements of this type.

Segment code snippet

You can use startSegment method on Inspector instance and manage the code block monitoring by yourself:

/*
* Create a new "segment" that will be automatically attached
* to the current transaction.
*/
$segment = $inspector->startSegment('type', 'label');
try {
/*
* Write here your code block
*/
} catch(UnauthorizedException $exception) {
/*
* Report the exception to Inspector for diagnosis purpose.
*/
$inspector->reportException($exception);
} finally {
/*
* Close the code block with end() method to stop segment monitoring.
*/
$segment->end();
}

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 performs this strategy for you and accept a collback to execute your code block:

$segment = $inspector->addSegment(function () {
/*
* Write here your code block
*/
}, 'type', 'label');