PHP code example of inspector-apm / inspector-slim

1. Go to this page and download the library: Download inspector-apm/inspector-slim library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inspector-apm / inspector-slim example snippets


$container->set('inspector', function () {
    $configuration = new \Inspector\Slim\Configuration('INSPECTOR_INGESTION_KEY');
    
    return new Inspector($configuration);
});

use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;

return function (ContainerBuilder $containerBuilder) {
    $containerBuilder->addDefinitions([
        // Other services definitions...
    
        'inspector' => function (ContainerInterface $container) {
            $configuration = new \Inspector\Slim\Configuration('INSPECTOR_INGESTION_KEY');
            return new Inspector\Inspector($configuration);
        }
        
    ]);
}

$app->add(\Inspector\Slim\WebRequestMonitoring::class);

$app->get('/', function () {
    
    // your code here...
    
})->add(\Inspector\Slim\WebRequestMonitoring::class);

$app->get('/test', function () {
    
    throw new \Exception('My First Exception.');
    
})->add(\Inspector\Slim\WebRequestMonitoring::class);

$app->get('/', function (Request $request, Response $response) {
    /*
     * Retrieve the inspector instance from the container.
     */
    $this->get('inspector')->addSegment(function () {
    
        // Your code here...
        sleep(1);
        
    }, 'sleep');
        
    return $response;
});

namespace App\Controllers;


use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class TestController
{
    protected $container;

    /**
     * Inject the container to retrieve the inspector instance later.
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function __invoke(Request $request, Response $response)
    {
        // Retrieve the inspector instance from the container.
        $this->container->get('inspector')->addSegment(function () {
        
            // Your code here...
            sleep(1);
            
        }, 'sleep');

        $response->getBody()->write('Test route.');

        return $response;
    }
}