一、什么是预警?预警可以理解成我们日常生活中的体检,每个人不管是富贵,还是贫穷,有一个健康的身体才是事业和生活的基础。项目的健康我们形象比喻成“1”,项目的N多子功能/子模块,比如登录模块、注册模块等都是“1”后面的“0”,如果项目都出问题了(比如 Http Status Code 502, 页面或者接口响应慢),相当于前面的“1”已经不存在了,即使后面再多的“0”也没有任何意义。二、预警能起到什么作用?从医学的名词来表述,包括了三个方面:治未病早发现,早治疗确诊了,马上治疗三、Asf 预警解决方案关于 Asf 框架的入门教程,请看这里 >>> Get Start关于 Asf 框架支持的配置项及配置详情,请看这里 >>> Get StartAsf PHP扩展框架已经内置了一套预警机制,预警范围包括了两个方面,每一个方面又包括两种不同的预警处理方法。PHP脚本执行异常预警Fatal ErrorWarningNoticeStrictDeprecatedUnknown ErrorE_PARSEE_WARNINGE_NOTICEE_STRICTE_DEPRECATEDE_OTHERSE_ERRORE_USER_WARNINGE_USER_NOTICE—E_CORE_ERRORE_CORE_WARNING—-E_COMPILE_ERRORE_COMPILE_WARNING—-E_USER_ERRORE_RECOVERABLE_ERROR—-PHP脚本执行时间耗时预警 (非CLI模式下)配置项类型默认值 (秒)含义asf.dispathcer.timeout.max_script_timedouble1.0脚本超时预警asf.dispathcer.timeout.max_db_timedouble0.1SQL超时预警asf.dispathcer.timeout.max_cache_timedouble0.1NoSQL超时预警asf.dispathcer.timeout.max_curl_timedouble0.1CURL超时预警四、本地日志记录方案 Demo本地日志记录方案,支持 Log Buffer,性能指标请看这里 >>> Get Start4.1 PHP脚本执行异常预警日志格式规范请参考这里 >>> Get Start[dongshuang@box3 /data/www/box3.cn/public]$ cat index.php<?phpuse Asf\Application as App;$configs = array( ‘asf’ => array( ‘root_path’ => dirname(DIR) . ‘/apps’, ’log_path’ => ‘/data/logs’, /* 日志保存目录,写入权限 / ‘dispatcher’ => [ ’log’ => [’err’ => 1] / 开启 / ] ));$app = new App($configs);$app->run();4.1.1 /data/logs/Asf_Err_Log 样本[dongshuang@box3 /data/logs]$ tailf Asf_Err_Log2019-02-22 16:01:11 Asia/Shanghai | NOTICE | 28984 | Notice: Undefined variable: b in /data/www/box3.cn/apps/Bootstrap.php on line 212019-02-22 16:01:11 Asia/Shanghai | WARNING | 31486 | Warning: include(test.php): failed to open stream: No such file or directory in /data/www/box3.cn/apps/Bootstrap.php on line 202019-02-22 16:02:24 Asia/Shanghai | ERROR | 4137 | Fatal Error: Asf\Application::run() No method listaaction in DeviceService in /data/www/box3.cn/public/index.php on line 23Stack trace:## settled_uri = /v1/device/lista#0 /data/www/box3.cn/public/index.php(23): Asf\Application->run()#1 {main}4.2 PHP脚本执行时间耗时预警[dongshuang@box3 /data/www/box3.cn/public]$ cat index.php<?phpuse Asf\Application as App;$configs = array( ‘asf’ => array( ‘root_path’ => dirname(DIR) . ‘/apps’, ’log_path’ => ‘/data/logs’, / 日志保存目录,写入权限 / ‘dispatcher’ => [ ’log’ => [’timeout’ => 1] / 开启 */ ] ));$app = new App($configs);$app->run();4.2.1 /data/logs/Asf_Timeout_Log 样本[dongshuang@box3 /data/logs]$ tailf Asf_Timeout_Log2019-02-25 09:06:48 UTC | INFO | 26026 | 127.0.0.1:6379 Redis::set(110) executing too slow 0.201406 sec2019-02-25 09:06:48 UTC | INFO | 26026 | 127.0.0.1:6379 Redis::get(111) executing too slow 0.201349 sec2019-02-25 09:06:48 UTC | INFO | 26026 | /index/redis executing too slow 1.008510 sec五、函数回调(callable)方案 Demo5.1 PHP脚本执行异常预警[dongshuang@box3 /data/www/box3.cn/public]$ cat index.php<?phpuse Asf\Application as App;function myErrorHandler($errno, $errstr, $errfile, $errline){ var_dump($errno, $errstr, $errfile, $errline);}$configs = array( ‘asf’ => array( ‘root_path’ => dirname(DIR) . ‘/apps’, ));$app = new App($configs);$app->setErrorHandler(‘myErrorHandler’);$app->run();5.1.1 异常预警 - 结果样本int(999)string(33) “Fatal Error: Class ‘ck’ not found"string(53) “/data/www/box3.cn/apps/api/services/Index.php"int(42)5.2 PHP脚本执行时间耗时预警[dongshuang@box3 /data/www/box3.cn/public]$ cat index.php<?phpuse Asf\Application as App;function myTimeoutHandler($errno, $errstr){ var_dump($errno, $errstr); }$configs = array( ‘asf’ => array( ‘root_path’ => dirname(DIR) . ‘/apps’, ));$app = new App($configs);$app->setTimeoutHandler(‘myTimeoutHandler’);$app->run();5.2.1 耗时预警 - 结果样本int(976)string(44) “/index/redis executing too slow 0.508643 sec"六、Asf 预警错误号区间段说明6.1 《异常预警》错误号区间段 990 ~ 999errno = 999, 表示含义: Fatal Error (框架会对异常信息进行有效拦截, 防止服务端信息泄露)errno = 998, 表示含义: Warningerrno = 997, 表示含义: Noticeerrno = 996, 表示含义: Stricterrno = 995, 表示含义: Deprecatederrno = 994, 表示含义: Unkown Error6.2 《超时预警》错误号区间段 970 ~ 989errno = 970, 表示含义: MySQL 单条SQL执行时间超过预设阀值errno = 971, 表示含义: Redis 单条NoSQL执行时间超过预设阀值errno = 972, 表示含义: Memcached 单条NoSQL执行时间超过预设阀值errno = 973, 表示含义: PgSQL 单条SQL执行时间超过预设阀值errno = 974, 表示含义: SQLite 单条SQL执行时间超过预设阀值errno = 975, 表示含义: CURL 请求时间超过预设阀值errno = 976, 表示含义: 脚本完整生命周期请求时间超过预设阀值持续开放更多的功能七、结束语感谢大家的阅读,希望能给大家带来帮助,如果使用中有疑问请联系我们,当然了我们也可以聊点更好玩的 ^_^