palzin-apm / palzin-codeigniter
Palzin Monitor(APM)实时监控Codeigniter包。
Requires
- php: >=7.2
- palzin-apm/palzin-php: ^23.03.08
Requires (Dev)
- codeigniter/coding-standard: ^1.1
- codeigniter4/codeigniter4: ^4.2
- nexusphp/cs-config: ^3.4
- phpunit/phpunit: ^9.0
README
Palzin Monitor提供实时性能监控功能,可让您有效地监控和分析应用程序的性能。使用Palzin Monitor,您可以捕获和跟踪所有请求,而无需进行任何代码修改。此功能使您能够深入了解您的函数、数据库语句和外部请求对整体用户体验的影响。
入门指南
要快速入门Codeigniter中的Palzin Monitor(APM),请按照以下步骤操作
- 使用以下命令通过Composer安装包:
composer require palzin-apm/palzin-codeigniter
。 - 在您的CodeIgniter项目中创建一个新的配置类。
- 将下面的类复制并粘贴到您刚创建的配置类中。
Palzin CodeIgniter
是Palzin Monitor(APM)PHP监控库的便利包装,专为CodeIgniter4应用程序设计。它通过提供自动检查功能,简化了监控代码的过程,而无需您提供额外的代码。
此库非常灵活,允许您根据需求自定义自动检查功能。您有权定义自己的检查点,从而在监控过程中拥有更大的控制权。Palzin CodeIgniter可用于应用程序的各个组件,包括控制器、模型、事件、库和自定义类。只要代码可以访问CodeIgniter4的服务,您就可以利用此库的功能。
安装
Palzin CodeIgniter可以通过Composer轻松安装,利用CodeIgniter4的自动加载功能。只需运行以下命令
composer require palzin-apm/palzin-codeigniter
或者,您可以通过下载源文件并将其目录添加到app/Config/Autoload.php
文件中来手动安装库。
设置
为了开始使用集成库,您需要为它创建一个配置类。> ./spark make:config Palzin
<?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; }
用法
要使用Palzin Monitor库集成,请使用palzin
服务。
$palzinInstance = service('palzin');
将AutoInspect设置为true时,您无需做任何事情,应用程序将自动开始检查。这是通过在post_controller_constructor
中使用CI4事件功能实现的,代码将开始一个段,控制器作为标题,方法名称作为标签。然后在post_system
中结束段,这意味着从接收到的请求开始到结果交付,您的代码路径将被“跟踪”,并将结果提交到palzin.app。就这样。
然而,您可能需要更精细地控制代码点,并可能需要访问其他更强大的Palzin Monitor(APM)功能,这就是服务的作用所在。这里我们仅展示一些有用的方法,请查看Palzin Monitor(APM)文档以获取更多方法和功能。
您可以从代码中的任何位置添加段(假设这是您的控制器方法getUsers)
/* 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()方法加载它。您可以在代码中通过调用helper()方法并传递助手名称作为参数来实现这一点。另一种选择是配置您的BaseController以始终加载助手。
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)'));
注意:由于助手函数的简写性质,它只能添加一个段或返回一个服务实例。
官方文档
许可证
本软件包采用MIT许可证。