Installation & Set Up

Connect your application with Inspector.

Server requirements

  • PHP >= 7.2

Install

You can install Inspector using composer:

composer require inspector-apm/inspector-php

Create an Inspector instance

Be sure to include the composer autoload in your application. The first step is to create an instance of the Configuration class using the Ingestion Key.

<?php
require __DIR__ . '/../vendor/autoload.php';

use Inspector\Inspector;
use Inspector\Configuration;

// Create a configuration instance.
$configuration = new Configuration('YOUR_INGESTION_KEY');

// Pass the configuration to the Inspector constructor.
$inspector = new Inspector($configuration);

Get a new Ingestion Key by signing up for Inspector (https://app.inspector.dev/register) and creating a new application.

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

$pathInfo = explode('?', $_SERVER["REQUEST_URI"]);
$path = array_shift($pathInfo);


/*
 * A transaction should start as soon as possible, 
 * typically in the "index.php" file of your application.
 */
$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->transaction()->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->transaction()->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 context is designed to help developers easily collect custom data during an execution cycle.

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 (e.g. add segments):

class UserController extends BaseController
{
    /**
     * Example of how to get the Inspector instance from a container.
     */
    public function action()
    {
        // Get the instance from the container.
        $inspector = $container->get('inspector');
    
        // Add a new segment to monitor an action.
        return $inspector->addSegment(function () {
            
            return $this->someService->action();
        
        }, 'someService', 'action');
    }
}

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.

$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
    // Do something
});

You can also register multiple callbacks that will be executed in the same order:

// First
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
    // Do something
});

// Second
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
    // Do something
});

// Last
$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
    // Do something
});

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:

$inspector::beforeFlush(function (\Inspector\Inspector $inspector) {
    if ($inspector->transaction()->name === 'GET /api') {
        return false; // <- Do not send data to the remote platform.
    }
});

Last updated