Inspector
BlogTry for free
  • Concepts
    • Introduction
    • Metrics
    • Notification Channels
      • Email
      • Slack
      • Telegram
      • Microsoft Teams
      • Google Chat
      • Webhook
      • Discord
      • Pushover
      • PagerDuty
      • Twilio - SMS
    • Filtering Syntax
    • Alerts
    • Grouping Patterns
    • Custom Segments
    • Billing
    • AI Bug Fixer
  • Security and access
    • Access control
    • Two-factor authentication
    • Connected Devices
  • SDK
    • PHP
      • Installation & Set Up
      • Custom Segments
      • Exceptions Monitoring
      • Configuration
    • Laravel / Lumen
      • Upgrade Guide
      • Installation & Set-up
      • Http Requests Monitoring
      • Configuration
      • Exception Monitoring
      • Laravel Vapor
      • Laravel Octane
      • Laravel Nova Tool
      • Group by service name
    • Symfony
      • Installation
      • Configuration
      • Exception Monitoring
    • CodeIgniter
      • Installation
      • Configuration
      • Exception Monitoring
    • Drupal
    • Spring Boot
    • Slim
    • NodeJS
      • Configurations
      • Custom Segments
      • Exception monitoring
      • Autowiring
    • ExpressJs
    • Fastify
    • Python
    • Django
      • Installation & Set Up
      • Custom Segments
      • Error Monitoring
  • REST API
    • Authentication
    • Apps
    • Platforms
    • Transactions
    • Segments
    • Analytics
Powered by GitBook
On this page
  • Server requirements
  • Install
  • Register On Container
  • Attach the middleware
  • Test that everything works
  • Add Segments
  1. SDK

Slim

Code Execution Monitoring for Slim based applications.

PreviousSpring BootNextNodeJS

Last updated 3 months ago

Server requirements

  • PHP >= 7.2

  • Slim >= 4.x

Install

Install the latest package version by:

composer require inspector-apm/inspector-slim

Register On Container

First you have to register the Inspector instance inside the application container in order to make the monitoring agent available within the application.

Consider to use to store your project's INGESTION KEY:

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

If you are using a Slim 4 skeleton you can add a new container definition in app/dependencies.php file:

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);
        }
        
    ]);
}

Attach the middleware

To monitor all the incoming HTTP traffic you can attach the middleware globally:

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

Or in specific routes:

$app->get('/home', function () {
    
    // do something...
    
})->add(\Inspector\Slim\WebRequestMonitoring::class);

Test that everything works

$app->get('/test', function () {
    
    throw new \Exception('My First Exception.');
    
});

You should receive your first notification in a few seconds.

Add Segments

You can add segments to the transaction's timeline from route functions:

$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;
});

If your routes are organized using controllers you need to inject the container in the controller constructor in order to retrieve the inspector agent later during execution:

namespace App\Controllers;


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

class TestRouteController
{
    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)
    {
        // Use 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;
    }
}

You can get an INSPECTOR_INGESTION_KEY creating a new project in your account.

Create a test route and open it in the browser

Learn more about .

environment variables
Inspector
http://localhost:8080
custom segments