weiboad / fierysdk

此包已被废弃,不再维护。未建议替代包。

fiery APM 集成 PHP 追踪 SDK

0.5.4 2017-06-28 03:07 UTC

This package is not auto-updated.

Last update: 2022-06-11 16:24:45 UTC


README

RagnarSDK

Latest Stable Version Latest Unstable Version License

其他语言介绍: 中文文档

介绍

  • 用于复杂系统的线上追踪。
  • 支持在线调试、level 日志、异常和错误收集、性能监控和依赖关系图。
  • 针对 https://github.com/weiboad/fiery

要求

  • PHP5.3+ 且支持 bcmath
  • utf-8 编码的项目

第1步 容器变量

Nginx

复制 nginx/fiery_fastcgi_pararms -> nginx/conf 并编辑 vhost 配置

server{
    listen 80;
    charset utf-8;
    root /path/xxx/xxx/src/public;
    server_name xxx.com;
    
    location /{
        index index.php index.html index.htm;
        if (-f $request_filename) {
            break;
        }
        if (-d $request_filename) {
            break;
        }
        if ($request_filename !~ (\.css|images|index\.php.*) ) {
            rewrite ^/(.*)$ /index.php/$1 last;
            break;
        }
    }

    location ~ /index.php/ {
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        include fiery_fastcgi_params; # here is the point
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
    }

    location ~ \.php$ {
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;
        include fiery_fastcgi_params; # here is the point
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
    }
}
# reload the nginx config
nginx -s reload

Apache
<VirtualHost *:80>
    ServerAdmin webmaster@demo.com
    DocumentRoot "e:\wwwroot\demo"
    ServerName my.demo.com
    ErrorLog "logs/my.demo.com-error.log"
    CustomLog "logs/my.demo.com-access.log" common
    
    # start config the variable for fierysdk
    SetEnv RAGNAR_LOGPATH /data1/ragnar/  # here is the point
    SetEnv RAGNAR_IDC 0  # here is the point
    SetEnv RAGNAR_IP 192.168.1.123  # here is the point

    <Directory "e:\wwwroot\demo">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Fierysdk 安装

运行命令以获取此 composer

composer require weiboad/fierysdk

在 PHP 项目的 bootstrap 中插入代码

    require_once("vendor/autoload.php");
    
    
    use \Adinf\RagnarSDK\RagnarSDK as RagnarSDK;
    use \Adinf\RagnarSDK\RagnarConst as RagnarConst;
    
    //for the Temporary disable this sdk
    //RagnarSDK::disable();
    
    //default log level is info you can Temporary open the debug level by send header
    RagnarSDK::setLogLevel(RagnarConst::LOG_TYPE_INFO); 
    
    //filter url
    //when the url contained parameter on path such as 
    //http://wwwei.com/usr/$uid(an var alway change)/fetch 
    //you must write an rule on this callback filted to http://wwwei.com/usr/releaced/fetch
    
    RagnarSDK::setUrlFilterCallback(function ($url, $hashquery) {
        if (trim($url) == "") {
            return "";
        }
        if (stripos($url, 'http') !== 0) {
            $url = "http://" . $url;
        }

        $urlinfo = parse_url($url);

        if(!$urlinfo){
            return $url."#PARSERERROR";
        }

        if (!isset($urlinfo["scheme"])) {
            $urlinfo["scheme"] = "http";
        }

        if (!isset($urlinfo["path"])) {
            $urlinfo["path"] = "/";
        }

        if (!isset($urlinfo["query"])) {
            $urlinfo["query"] = "";
        }

        if ($hashquery) {
            return $urlinfo["scheme"] . "://" . $urlinfo["host"] . $urlinfo["path"] . "?" . $urlinfo["query"];
        } else {
            return $urlinfo["scheme"] . "://" . $urlinfo["host"] . $urlinfo["path"];
        }
     });
         
    //this must run at latest
    //ragnar_projectname is you project name will use on log folder name
    RagnarSDK::init("ragnar_projectname");
    
    //Ragnar level log example
    // this is info log you can see this on tracing page on fiery 
    RagnarSDK::RecordLog(RagnarConst::LOG_TYPE_INFO, __FILE__, __LINE__, "module1_msg", array("msg"=>"i wish i can fly!"));
    
    // this is debug log 
    RagnarSDK::RecordLog(RagnarConst::LOG_TYPE_DEBUG, __FILE__, __LINE__, "module2_msg",array("msg"=>"i wish im rich!");
    
    //customize performance dig point example will display on tracing page on fiery
    //dig start
    $digpooint = RagnarSDK::digLogStart(__FILE__,__LINE__,"ragnar_test");
    
        //run something.....
    //dig end
    RagnarSDK::digLogEnd($digpooint,array("happy"=>1));
    

Ragnar 日志级别

  • 日志级别:设置日志级别以决定要转储的定制日志
  • 日志搜索:在级别中的所有日志将在追踪页面上显示。
  • 异常和错误:将在错误统计页面上显示
  • 性能:一种简单的方法记录函数耗时并在 curl、mysql API 上进行统计。

日志级别

  • LOG_TYPE_TRACE 跟踪日志用于低级别调试
  • LOG_TYPE_DEBUG 调试日志
  • LOG_TYPE_NOTICE 系统上的通知日志
  • LOG_TYPE_INFO 提示工作状态的日志
  • LOG_TYPE_ERROR 系统错误时将记录此级别
  • LOG_TYPE_EMEGENCY 紧急日志,将向管理员发送短信或电子邮件
  • LOG_TYPE_EXCEPTION 异常日志
  • LOG_TYPE_PERFORMENCE 性能日志,所有数据点都将使用此日志

Curl 数据点

Curl 数据点

    //curl must fill
    $digpooint = RagnarSDK::digLogStart(__FILE__, __LINE__, "curl");
    
    //curl init ....
    
    $nextrpcidheader = RagnarSDK::getCurlChildCallParam($digpooint);
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, $nextrpcidheader);
    
    $result = //curl exec ...
    
    $ext = array("errorno" => $errno, "error" => curl_error($this->ch));
    $info = curl_getinfo($this->ch);
    
    //digCurlEnd($digPoint, $url, $method, $postParam, $getParam, $curlInfo, $errCode, $errMsg, $result)
    
    RagnarSDK::digLogEnd($digpooint, array(
                "url" => $info['url'], "method" => self::get_method(),
                "param" => array("post" => $this->post_fields, "get" => $this->query_fields),
                "info" => $info,
                "error" => $ext,
                "result" => json_decode($result,true),//must array
    );

Mysql

Mysql 数据点

    //this for record the exception when the error
    RagnarSDK::RecordLog(\Adinf\RagnarSDK\RagnarSDK::LOG_TYPE_EXCEPTION, __FILE__, __LINE__, "mysql", array("fun" => "query", "sql" => $sql, "error" => $ex->getMessage()));
    
    //start monitor the performance
    $digPoint = RagnarSDK::digLogStart(__FILE__, __LINE__, "mysql");
    
    //do some sql execute
    
    //for the mysql performance dig point end
    //RagnarSDK::digLogEnd($digPoint, array("sql" => $sql, "data" => "sql的参数", "op" => "select\delete\update\...", "fun" => "execute_sql"));
    RagnarSDK::digMysqlEnd($digPoint, $sql, "sql的参数", "select\delete\update\...", "execute_sql");
    //if is error
    if(error){
        RagnarSDK::RecordLog(RagnarConst::LOG_TYPE_EXCEPTION, __FILE__, __LINE__, "mysql", array("fun" => "execute", "sql" => $sql, "error" => $error));
    }

临时更改

通过发送头部信息在线更改日志级别

    X-RAGNAR-TRACEID   traceid
    X-RAGNAR-RPCID     rpcid (spanid)
    X-RAGNAR-LOGLEVEL  log level