PHP code example of palzin-apm / palzin-codeigniter

1. Go to this page and download the library: Download palzin-apm/palzin-codeigniter 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/ */

    

palzin-apm / palzin-codeigniter example snippets




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

$palzinInstance = service('palzin');

/* gets JSON payload of $limit users */
public function getUsers(int $limit)
{
  return $palzinInstance->addSegment(function() {
    $userModel = new UserModel();
    $users = $userModel->findAll($limit);
    $this->response->setStatusCode(200, 'OK')->setJSON($users, true)->send();
  }, 'getUsers', 'Get Users');
}

/* validate the user has the proper age set */
public function isActiveAttribute(): bool
{
  try {
    if($this->monitor->active === true) {
      throw new \Exception('Status is active so it means danger.');
    }
  } catch (\Exception $e) {
    $palzinInstance->reportException($e);
    /* Your exception handling code... */
  }
}

helper('palzin');

/* get an instance of palzin */
$palzinInstance = palzin();

/* add a segment through the helper */
palzin(function () {
  /* run your code here... */
  $asyncData = $this->checkWebsite('https://doesthissiteworkforyou.test');
  return $this->checkWebsiteSyncJob($asyncData);
}, 'data-load', 'Website Check Flow');

/* add a segment through the instance */
$palzinInstance->addSegment(function ($segment) use ($config) {
            usleep(10 * 1000);

            $segment->addContext('example payload', ['key' => $config->get('palzin.key')]);
        }, 'test', 'Check Palzin Monitor (APM) Ingestion key');

/* set an exception */
$palzinInstance->reportException(new \Exception('First Exception detected using Palzin Monitor (APM)'));