staabm/sysmonitor

1.0.5 2024-03-05 09:20 UTC

This package is auto-updated.

Last update: 2024-09-05 10:18:08 UTC


README

监控 PHP 应用程序并在某些错误/异常/资源耗尽/自定义等事件上发送通知。

监控器检查您提供的数据,并使用简单的默认实现(参见 SystemMonitor#rateAndStore)来判断何时情况变得紧急/严重。《SystemEvent》 通过哈希进行比较,因此出现时间也可以基于相似性。根据您使用的 Notifier,将发送通知。

SystemEventStorage 的默认实现将您的数据存储在 APC 和 Memcached 的混合中。因此,它需要这两个 PHP 扩展。

用法

初始化所有事物。所有以 My 开头的类都需要由被监控的应用程序/框架提供。

// sends notificaitons on urgent events
$notifier = new SeverityNotifier(new MyCustomNotifier(), SystemEvent::SEVERITY_URGENT);
// main class which collects all the data
$monitor = new SystemMonitor(new SystemEventStorage(), new MyRequestEnvImpl(), $notifier);

从应用程序中的某个地方(例如请求关闭时)报告性能数据

register_shutdown_function(function() {
    $requestStats = new RequestStatsEvent();
    // data from your db class
    $requestStats->usedQueries = DB::$num_of_queries;
    $requestStats->usedConnections = DB::$num_of_connections;
    // data from your runtime
    $requestStats->peakMemory = number_format(memory_get_peak_usage(true) / 1024 / 1024);
    
    // retrieve the monitor instance, e.g. via a DIC/a registry/singleton/whatever
    // $monitor = .. 
    $monitor->collectStats($requestStats);
});

Monitor 收集已发生的异常数据

set_exception_handler(function() {
    $event = new RequestExceptionEvent();
    $event->exception = $exception;
    
    // retrieve the monitor instance, e.g. via a DIC/a registry/singleton/whatever
    // $monitor = .. 
    $monitor->collectException($event);
});

您可以对错误做同样的事情。要收集致命错误的数据,有一些已知的解决方案可以使用(在关闭函数中检查 error_get_last()

备注

这个库是从一个框架中提取出来的,包含一些为了向后兼容而保留的“奇怪”东西。这将在此版本 2.0 中进行更改。