How to Use Palzin Monitor with CodeIgniter 4

Palzin Monitor is a powerful application performance monitoring tool that helps you identify and resolve performance issues in your PHP applications. This guide will walk you through the steps to install, configure, and use Palzin Monitor in your CodeIgniter application

Setting up a CodeIgniter 4 Project

Before using Palzin Monitor with CodeIgniter, you need to set up a CodeIgniter 4 project. Follow these steps to get started:

  1. Install Composer: If you don't have Composer installed, visit the Composer website and follow the instructions to install it on your system.

  2. Create a new CodeIgniter 4 project: Open your terminal or command prompt, navigate to the directory where you want to create your project, and run the following command:

    composer create-project codeigniter4/appstarter project-name
    

    Replace project-name with the desired name for your project.

  3. Navigate to the project directory: After the project is created, navigate to the project directory using the following command:

    cd project-name
    
  4. Configure the database: Open the .env file located in the project root directory and update the database settings according to your environment. Provide the necessary details such as database name, username, password, and host.

  5. Run the development server: To start the development server and run your CodeIgniter 4 project locally, run the following command:

    php spark serve
    

    This will start the server, and you can access your project at http://localhost:8080 in your web browser.

Now that you have set up your CodeIgniter 4 project, you can proceed with the steps to use Palzin Monitor.

Usage

To use the Palzin Monitor CodeIgniter package, follow the steps below:

  1. Install the Palzin Monitor CodeIgniter package: Run the following command in your project's root directory:

    composer require palzin-apm/palzin-codeigniter
    

    This will install the Palzin Monitor package and its dependencies into your project.

  2. Create the Palzin configuration file: In your terminal, run the following command to create a configuration file:

    php spark make:config Palzin
    

    This will create a Palzin.php configuration file inside the app/Config directory.

  3. Open the app/Config/Palzin.php file in your CodeIgniter 4 project. This file contains the configuration options for the Palzin Monitor package.

    Look for the Palzin class definition within the Palzin.php file. It should look like this:

    <?php
    
    namespace Config;
    
    use CodeIgniter\Config\BaseConfig;
    
    class Palzin extends BaseConfig
    {
        /**
         * Set the value to true if you want all your controller methods to be automatically inspected.
         * Set the value to false if you prefer to define your own inspection points, which offers greater flexibility.
         *
         * @var bool
         */
        public $AutoInspect = true;
    
        /**
         * To enable sending unhandled exceptions to the Palzin dashboard,
         * set this option to true. By default, it is set to false for backward compatibility.
         *
         * @var bool
         */
        public $LogUnhandledExceptions = false;
    
        /**
         * Palzin Monitor (APM) ingestion key, you can find this on your Palzin dashboard
         *
         * @var string
         */
        public $PalzinMonitorAPMIngestionKey = 'YOUR_INGESTION_KEY';
    
        /**
         * @var bool
         */
        public $Enable = true;
    
        /**
         * Remote endpoint to send data.
         *
         * @var string
         */
        public $URL = 'https://demo.palzin.app';
    
        /**
         * @var string
         */
        public $Transport = 'async';
    
        /**
         * Transport options.
         *
         * @var array
         */
        public $Options = [];
    
        /**
         * Max numbers of items to collect in a single session.
         *
         * @var int
         */
        public $MaxItems = 100;
    }
    ?>
    

    Locate the $PalzinMonitorAPMIngestionKey option within the class. It is set to 'YOUR_INGESTION_KEY' by default.

    Replace 'YOUR_INGESTION_KEY' with your actual Palzin Monitor (APM) ingestion key. You can find this key on your Palzin dashboard. Log in to your Palzin account, navigate to the dashboard, and look for the APM ingestion key specific to your project.

    Save the Palzin.php file after making the necessary changes.

    By updating the Palzin.php configuration file with your Palzin Monitor (APM) ingestion key, the Palzin Monitor package will be configured to send performance data to your Palzin dashboard for analysis. Make sure to save the file after making the changes.

  4. Need to add code in controller file Home.php

    <?php
    
    namespace App\Controllers;
    
    use CodeIgniter\CLI\CLI;
    use Palzin\CodeIgniter\Palzin;
    use Palzin\Models\Segment;
    
    class Home extends BaseController
    {
        public function index()
        {
            $config = config('Palzin');
            $palzinInstance = Palzin::getInstance($config);
    
            $palzinInstance->addSegment(function (Segment $segment) {
    
    
                $config = config('Palzin');
                $palzinKey = $config->palzin['IngestionKey'];
    
                echo $palzinKey;
    
                if (!empty($palzinKey)) {
                    echo "✅ Palzin Monitor (APM) ingestion key installed. \n\n";
                } else {
                    echo "❌ Palzin Monitor (APM) ingestion key not specified. Make sure you specify the PALZIN_APM_INGESTION_KEY in your .env file.";
                }
    
                $segment->addContext('example payload', ['IngestionKey' => $palzinKey]);
            }, 'test', 'Check Palzin Monitor (APM) Ingestion key');
    
            // Check Palzin is enabled
            $palzinInstance->addSegment(function (Segment $segment) {
                usleep(10 * 1000);
    
                $config = config('Palzin');
                $palzinEnable = $config->palzin['Enable'];
    
                if ($palzinEnable) {
                    CLI::write("✅ Palzin Monitor (APM) is enabled.");
                } else {
                    CLI::write("❌ Palzin Monitor (APM) is actually disabled, turn to true the `enable` field of the `palzin-apm` config file.");
                }
    
                $segment->addContext('another payload', ['enable' => $palzinEnable]);
            }, 'test', 'Check if Palzin Monitor (APM) is enabled');
    
    
            // Report Exception
            $palzinInstance->reportException(new Exception('First Exception detected using Palzin Monitor (APM)'));
    
            // End the transaction
            $palzinInstance->currentTransaction()
                ->setResult('success')
                ->end();
    
            return view('welcome_message');
        }
    }
    
  5. Start using Palzin Monitor:

    • Automatic Inspection: With the AutoInspect option set to true in the configuration, your application's controller methods will be automatically inspected. This means Palzin Monitor will track and record the performance metrics for these methods.

    • Manual Inspection: If you prefer to define your own inspection points, you can use the Palzin Monitor service. Access the palzin service by adding the following line to your code:

      $palzinInstance = service('palzin');
      

      You can then use the $palzinInstance variable to add segments and report exceptions as needed. Refer to the Palzin Monitor (APM) documentation for more methods and features.

That's it! You have successfully set up Palzin Monitor with your CodeIgniter 4 project. Palzin Monitor will now monitor and record performance metrics related to your application's HTTP requests, database queries, and more. You can view and analyze these metrics on your Palzin dashboard.

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.