Usage

How to start monitoring your script.

Transactions

All start with a transaction. Transaction represent 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

/*
* A transaction should start as soon as possible,
* typically in the "index.php" file of your application.
*/
$pathInfo = explode('?', $_SERVER["REQUEST_URI"]);
$path = array_shift($pathInfo);
$inspector->startTransaction($path);
// Continue with the script...

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:

$inspector->currentTransaction()->setResult(200);

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:

$inspector->currentTransaction()->addContext('label', ['foo' => 'bar']);

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:

$container->set('inspector', $inspector);

in order to use it later inside your application to add segments:

class UserController extends BaseController
{
/**
* @var \Inspector\Inspector
*/
protected $inspector;
/**
* UserController constructor.
*/
public function __construct()
{
// Get Inspector instance from the container.
$this->inspector = $container->get('inspector');
}
/**
* Do something.
*/
public function action()
{
// Monitor a piece of code and return the result back to the client.
return $this->inspector->addSegment(function () {
return $this->someService->doSomething();
}, 'someService', 'doSomething');
}
}