How to Use Palzin Monitor with Slim Framework

Introduction

The Slim Framework package for Palzin Monitor provides application performance monitoring capabilities for Slim-based applications. It seamlessly integrates with your Slim application, automatically instruments the code, and records performance metrics related to HTTP requests, database queries, and other operations. By utilizing this package, you can easily monitor and analyze the performance of your Slim application.

Server Requirements

Before getting started, make sure your server meets the following 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:

<?php

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

Replace 'PALZIN_APM_INGESTION_KEY' with your actual Palzin Monitor (APM) ingestion key and 'PALZIN_APM_URL' with the URL provided by Palzin.

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

Ensure that you replace 'PALZIN_APM_INGESTION_KEY' and 'PALZIN_APM_URL' with your actual Palzin Monitor (APM) ingestion key and URL respectively.

This guide provides you with the necessary steps to integrate Palzin Monitor with your Slim Framework application. By following these steps, you can easily monitor and analyze the performance of your Slim application using Palzin Monitor.

Last updated: 1 second 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.