inspector-apm / inspector-codeigniter
CodeIgniter应用简单的代码执行监控。
Requires
- php: >=7.4
- inspector-apm/inspector-php: ^3.7
Requires (Dev)
- codeigniter/coding-standard: ^1.1
- codeigniter4/codeigniter4: ^4.2
- nexusphp/cs-config: ^3.4
- phpunit/phpunit: ^9.0
README
将CodeIgniter应用程序连接到Inspector监控系统。
官方维护者
此存储库由 Prysis 维护 - sales@prysis.co.za
快速入门
- 使用Composer安装:
> composer require inspector-apm/inspector-codeigniter
- 创建一个新的配置类,并将以下提供的类复制到其中。
Inspector CodeIgniter
为您提供围绕inspector PHP监控库的简单包装,您可以使用它来为您的CodeIgniter4应用程序进行监控。它易于使用,并且可以配置为“自动监控”您的代码,无需您额外编写代码。
它也非常灵活,允许您覆盖自动监控,以便您对检查点有更多的控制权。您可以在控制器、模型、事件、库和自定义类中使用它。任何可以访问CI4服务的代码都可以使用此库。
安装
通过Composer轻松安装以利用CodeIgniter4的自动加载功能。
> composer require inspector-apm/inspector-codeigniter
或者,通过下载源文件并将目录添加到 app/Config/Autoload.php
中来手动安装。
配置
要开始使用集成库,您需要为它创建一个配置类。 > ./spark make:config Inspector
<?php namespace Config; use CodeIgniter\Config\BaseConfig; class Inspector extends BaseConfig { /** * set to true if you want all your controller methods to be 'auto-inspected' * set to false to set your own inspection points - provides more flexibility * * @var bool */ public $AutoInspect = true; /** * set this option to true if you want your application to send unhandled exceptions * to the inspector dashboard. Default is false for backward compatibility. * * @var bool */ public $LogUnhandledExceptions = false; /** * set this option to true if you want your application to send long running queries * and query errors to the inspector dashboard. Default is false for backward compatibility. * * @var bool */ public $LogQueries = false; /** * application ingestion key, you can find this on your inspector dashboard * * @var string */ public $IngestionKey = 'YOUR_INGESTION_KEY'; /** * @var bool */ public $Enable = true; /** * Remote endpoint to send data. * * @var string */ public $URL = 'https://ingest.inspector.dev'; /** * @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; }
用法
要使用inspector库集成,请使用 inspector
服务。
$inspectorInstance = service('inspector');
当AutoInspect设置为true时,您无需执行其他操作,您的应用程序将自动开始被监控,这是通过使用CI4事件功能在 post_controller_constructor
中实现的,代码将开始一个段落,将控制器作为标题,将方法名称作为标签。然后在 post_system
中结束段落,这意味着从接收到的请求开始到结果交付,您的代码路径将被“跟踪”,并将结果提交到inspector.dev。就这样。
但是,您可能需要对代码点有更细粒度的控制,并可能需要访问其他更强大的inspector功能,这正是服务发挥作用的地方。在这里,我们仅展示了几个有用的方法,有关更多方法和功能,请参阅inspector文档。
您可以从代码中的任何地方添加一个段落(假设这是您的控制器方法getUsers)
/* gets JSON payload of $limit users */ public function getUsers(int $limit) { return $inspectorInstance->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 validateUserAge(): bool { try { if($this->UserAge < 13) { throw new \UserException\AgeNotAppropriate('Cannot register user, minimum age requirement not met.'); } } catch (\UserException\AgeNotAppropriate $e) { $inspectorInstance->reportException($e); /* Your exception handling code... */ } }
使用助手函数
助手函数提供了使用服务的快捷方式。它必须首先使用 helper()
方法加载,或者告诉您的BaseController始终加载它。
helper('inspector'); /* get an instance of inspector */ $inspectorInstance = inspector(); /* add a segment through the helper */ inspector(function () { /* run your code here... */ $asyncData = $this->getAsyncData('https://data.mysite.io'); return $this->startDataSyncJob($asyncData); }, 'data-load', 'Data Load Flow'); /* add a segment through the instance */ $inspectorInstance->addSegment(function () { /* run your code here... */ try { $asyncData = $this->getAsyncData('https://data.mysite.io'); return $this->startDataSyncJob($asyncData); } catch(\DataException\DataLoadException $e) { $inspectorInstance->reportException($e); } }, 'data-load', 'Data Load Flow'); /* set an exception */ $inspectorInstance->reportException(new \Exception('Model Setup Error'));
注意:由于助手函数的简写特性,它只能添加一个段落或返回一个服务实例。
官方文档
贡献
我们鼓励您为Inspector做出贡献!请查看有关如何进行的贡献指南。加入我们吧!
许可证
此软件包受MIT许可证的许可。