Asf PHP扩展框架之预警模块介绍

栏目: PHP · 发布时间: 7年前

内容简介:预警可以理解成我们日常生活中的体检,每个人不管是富贵,还是贫穷,有一个健康的身体才是事业和生活的基础。项目的健康我们形象比喻成“1”,项目的N多子功能/子模块,比如登录模块、注册模块等都是“1”后面的“0”,如果项目都出问题了(比如 Http Status Code 502, 页面或者接口响应慢),相当于前面的“1”已经不存在了,即使后面再多的“0”也没有任何意义。从医学的名词来表述,包括了三个方面:

一、什么是预警?

预警可以理解成我们日常生活中的体检,每个人不管是富贵,还是贫穷,有一个健康的身体才是事业和生活的基础。

项目的健康我们形象比喻成“1”,项目的N多子功能/子模块,比如登录模块、注册模块等都是“1”后面的“0”,如果项目都出问题了(比如 Http Status Code 502, 页面或者接口响应慢),相当于前面的“1”已经不存在了,即使后面再多的“0”也没有任何意义。

二、预警能起到什么作用?

从医学的名词来表述,包括了三个方面:

  • 治未病
  • 早发现,早治疗
  • 确诊了,马上治疗

三、Asf 预警解决方案

关于 Asf 框架的入门教程,请看这里 >>> Get Start

关于 Asf 框架支持的配置项及配置详情,请看这里 >>> Get Start

Asf PHP扩展框架已经内置了一套预警机制,预警范围包括了两个方面,每一个方面又包括两种不同的预警处理方法。

  • PHP脚本执行异常预警

    Fatal Error Warning Notice Strict Deprecated Unknown Error
    E_PARSE E_WARNING E_NOTICE E_STRICT E_DEPRECATED E_OTHERS
    E_ERROR E_USER_WARNING E_USER_NOTICE - - -
    E_CORE_ERROR E_CORE_WARNING - - - -
    E_COMPILE_ERROR E_COMPILE_WARNING - - - -
    E_USER_ERROR E_RECOVERABLE_ERROR - - - -
  • PHP脚本执行时间耗时预警 (非CLI模式下)

    配置项 类型 默认值 (秒) 含义
    asf.dispathcer.timeout.max_script_time double 1.0 脚本超时预警
    asf.dispathcer.timeout.max_db_time double 0.1 SQL超时预警
    asf.dispathcer.timeout.max_cache_time double 0.1 NoSQL超时预警
    asf.dispathcer.timeout.max_curl_time double 0.1 CURL超时预警

四、本地日志记录方案 Demo

本地日志记录方案,支持 Log Buffer,性能指标请看这里 >>> Get Start

4.1 PHP 脚本执行异常预警

日志格式规范请参考这里 >>> Get Start

[dongshuang@box3 /data/www/box3.cn/public]$ cat index.php
<?php
use 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_Log
2019-02-22 16:01:11 Asia/Shanghai | NOTICE | 28984 | Notice: Undefined variable: b in /data/www/box3.cn/apps/Bootstrap.php on line 21
2019-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 20
2019-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 23
Stack 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
<?php
use 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_Log
2019-02-25 09:06:48 UTC | INFO | 26026 | 127.0.0.1:6379 Redis::set(110) executing too slow 0.201406 sec
2019-02-25 09:06:48 UTC | INFO | 26026 | 127.0.0.1:6379 Redis::get(111) executing too slow 0.201349 sec
2019-02-25 09:06:48 UTC | INFO | 26026 | /index/redis executing too slow 1.008510 sec

五、函数回调(callable)方案 Demo

5.1 PHP脚本执行异常预警

[dongshuang@box3 /data/www/box3.cn/public]$ cat index.php
<?php
use 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
<?php
use 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 ~ 999

  • errno = 999, 表示含义: Fatal Error (框架会对异常信息进行有效拦截, 防止服务端信息泄露)
  • errno = 998, 表示含义: Warning
  • errno = 997, 表示含义: Notice
  • errno = 996, 表示含义: Strict
  • errno = 995, 表示含义: Deprecated
  • errno = 994, 表示含义: Unkown Error

6.2 《超时预警》错误号区间段 970 ~ 989

  • errno = 970, 表示含义: MySQL 单条 SQL 执行时间超过预设阀值
  • errno = 971, 表示含义: Redis 单条NoSQL执行时间超过预设阀值
  • errno = 972, 表示含义: Memcached 单条NoSQL执行时间超过预设阀值
  • errno = 973, 表示含义: PgSQL 单条SQL执行时间超过预设阀值
  • errno = 974, 表示含义: SQLite 单条SQL执行时间超过预设阀值
  • errno = 975, 表示含义: CURL 请求时间超过预设阀值
  • errno = 976, 表示含义: 脚本完整生命周期请求时间超过预设阀值
  • 持续开放更多的功能

七、结束语

感谢大家的阅读,希望能给大家带来帮助,如果使用中有疑问请联系我们,当然了我们也可以聊点更好玩的 ^_^


以上所述就是小编给大家介绍的《Asf PHP扩展框架之预警模块介绍》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Computer Age Statistical Inference

Computer Age Statistical Inference

Bradley Efron、Trevor Hastie / Cambridge University Press / 2016-7-21 / USD 74.99

The twenty-first century has seen a breathtaking expansion of statistical methodology, both in scope and in influence. 'Big data', 'data science', and 'machine learning' have become familiar terms in ......一起来看看 《Computer Age Statistical Inference》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试