Usage
How to start monitoring your script.

Transactions

All start with a transaction. A Transaction represents an execution cycle and it can contains one or hundreds 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.
Typically the transaction should be started in the entry point of your application e.g. index.php
1
/*
2
* A transaction should start as soon as possible,
3
* typically in the "index.php" file of your application.
4
*/
5
$pathInfo = explode('?', $_SERVER["REQUEST_URI"]);
6
$path = array_shift($pathInfo);
7
8
$inspector->startTransaction($path);
9
10
11
// Continue with the script...
Copied!

Set transaction result

When your application return a response back to the client you can setup the result of the transaction like the HTTP code:
1
$inspector->currentTransaction()->setResult(200);
Copied!

Add Context

Each transaction can be enriched with additional information to have a more complete picture of the process execution context.
You can use the addCotext method to attach new tabs to the current transaction:
1
$inspector->currentTransaction()->addContext('label', ['foo' => 'bar']);
Copied!
Contextual information will be reported as additional tabs in the transaction view. This code example creates a new tab "Label" in the transaction details as showed in the image below:
The new "Label" tab

Make Inspector available in your application globally

To be free to add new segments, or interact with the current transaction you need a way to make the Inspector instance available everywhere in your application.
Use an IoC container like PHP-DI is a common way to distribute internal services inside an application. If your application has one, or implement other similar strategies, you could put the Inspector instance in the registry:
1
$container->set('inspector', $inspector);
Copied!
in order to use it later inside your application (e.g. add segments):
1
class UserController extends BaseController
2
{
3
/**
4
* Example of how to get the Inspector instance from a container.
5
*/
6
public function action()
7
{
8
// Get the instance from the container.
9
$inspector = $container->get('inspector');
10
11
// Add a new segment to monitor an action.
12
return $inspector->addSegment(function () {
13
14
return $this->someService->action();
15
16
}, 'someService', 'action');
17
}
18
}
Copied!

Register callbacks

Using the beforeFlush() static method you can register a list of callbacks that will be executed just before data are sent out of your application.
This method provides a simple hook to interact with your monitoring data at the end of the transaction execution.
1
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
2
// Do something
3
});
Copied!
You can also register multiple callbacks that will be executed in the same order:
1
// First
2
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
3
// Do something
4
});
5
6
// Second
7
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
8
// Do something
9
});
10
11
// Last
12
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
13
// Do something
14
});
Copied!
By explicitly returning FALSE from a callback, the current transaction will not be sent to the remote platform, regardless of the value returned by other callbacks:
1
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
2
if ($inspector->currentTransaction()->name === 'GET /api') {
3
return false; // <- Do not send data to the remote platform.
4
}
5
});
Copied!