Slim Usage

Real-Time Monitoring Package using Palzin Monitor for Slim-based Applications

Server Requirements

  • PHP version 7.2 or higher
  • Slim version 4.x or higher

Installation

To install the latest version of the package, use the following composer command:

composer require palzin-apm/palzin-slim

Register On Container

To make the Palzin Monitor (APM) agent available within your Slim application, you need to register the Palzin Monitor instance inside the application container. It is recommended to use environment variables to store your project's ingestion key. Here's an example of how to register the Palzin Monitor instance:

$container->set('palzin', function () {
    $configuration = new \Palzin\Slim\Configuration('PALZIN_APM_INGESTION_KEY');
    $configuration->setUrl('PALZIN_APM_URL');
    return new \Palzin\Palzin($configuration);
});

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

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

return function (ContainerBuilder $containerBuilder) {
    $containerBuilder->addDefinitions([
        // Other service definitions...
        'palzin' => function (ContainerInterface $container) {
            $configuration = new \Palzin\Slim\Configuration('PALZIN_APM_INGESTION_KEY');
            $configuration->setUrl('PALZIN_APM_URL');
            return new \Palzin\Palzin($configuration);
        }
    ]);
}

You can obtain the PALZIN_APM_INGESTION_KEY and PALZIN_APM_URL by creating a new project in your Palzin account.

Attach the Middleware

To monitor all incoming HTTP traffic, you can attach the Palzin Monitor middleware globally or to specific routes. Here are examples of both:

Attach globally:

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

Attach to specific routes:

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

Test that Everything Works

Create a test route and open it in the browser at http://localhost:8080.

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

You should receive your first notification within a few seconds.

Add Segments

You can add segments to the transaction's timeline from route functions. Here's an example:

$app->get('/', function (Request $request, Response $response) {
    $this->get('palzin')->addSegment(function () {
        // Your code here...
        sleep(1);
    }, 'sleep');
    return $response;
});

If your routes are organized using controllers, you need to inject the container into the controller constructor to retrieve the Palzin Monitor agent during execution. Here's an example:

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 Palzin Monitor instance later.
     */
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

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

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

        return $response;
    }
}

Custom Segments

Learn more about custom segments.

Last updated: 1 year ago

Want to get started with Palzin Monitor? We offer a no-strings-attached
15 days trial. No credit card required.

It takes less than a minutes to setup your first monitoring.