Commit 79e977f5 authored by yujiwei's avatar yujiwei

update:change mon 打点

parent 6495e3a3
...@@ -17,6 +17,7 @@ class Request ...@@ -17,6 +17,7 @@ class Request
private $success_code = 0; private $success_code = 0;
private $fail_code = 999999; private $fail_code = 999999;
private $request_time= 0;
private $result = []; private $result = [];
protected $con_result = []; protected $con_result = [];
...@@ -38,11 +39,12 @@ class Request ...@@ -38,11 +39,12 @@ class Request
* @param array $headers 自定义headers * @param array $headers 自定义headers
* @param int $retry 重试次数(默认不重试) * @param int $retry 重试次数(默认不重试)
* @param bool $use_mon 是否使用监控打点(默认是) * @param bool $use_mon 是否使用监控打点(默认是)
* @param string $module 监控打点的模块名 默认是域名
* @param string $proxy 代理(比如http://192.11.222.124:8000) * @param string $proxy 代理(比如http://192.11.222.124:8000)
* @return array * @return array
* @throws * @throws
*/ */
public function get($url, $params = [], $timeout = 0, $headers = [], $retry = 0, $use_mon = true, $proxy = '') public function get($url, $params = [], $timeout = 0, $headers = [], $retry = 0, $use_mon = true,$module='', $proxy = '')
{ {
try { try {
//验参 //验参
...@@ -70,10 +72,11 @@ class Request ...@@ -70,10 +72,11 @@ class Request
if (!empty($proxy)) { if (!empty($proxy)) {
$options['proxy'] = $proxy; $options['proxy'] = $proxy;
} }
$options['on_stats'] = function (TransferStats $stats) use ($use_mon, $url) { $options['on_stats'] = function (TransferStats $stats) use ($use_mon, $module,$url) {
$this->result['http_code'] = $stats->getHandlerStat('http_code'); $this->result['http_code'] = $stats->getHandlerStat('http_code');
if (!empty($use_mon)) { $this->request_time = round($stats->getHandlerStat('total_time'),4) * 1000;
MonUtil::proxyMon($url, $stats->getHandlerStat('http_code'), '',round($stats->getHandlerStat('total_time'),4) * 1000); if ($use_mon) {
MonUtil::proxyMon($url, $stats->getHandlerStat('http_code'), $module,$this->request_time);
} }
}; };
//异步get请求 //异步get请求
...@@ -96,6 +99,10 @@ class Request ...@@ -96,6 +99,10 @@ class Request
$this->result['msg'] = $e->getMessage(); $this->result['msg'] = $e->getMessage();
} finally { } finally {
$this->addLog('get'); $this->addLog('get');
if ($use_mon && isset($this->result['code'])) {
MonUtil::proxyMon($url, $this->result['code'], $module,$this->request_time,false);
}
return $this->result; return $this->result;
} }
} }
...@@ -148,8 +155,9 @@ class Request ...@@ -148,8 +155,9 @@ class Request
} }
$options['on_stats'] = function (TransferStats $stats) use ($use_mon, $url,$module) { $options['on_stats'] = function (TransferStats $stats) use ($use_mon, $url,$module) {
$this->result['http_code'] = $stats->getHandlerStat('http_code'); $this->result['http_code'] = $stats->getHandlerStat('http_code');
$this->request_time = round($stats->getHandlerStat('total_time'),4) * 1000;
if ($use_mon) { if ($use_mon) {
MonUtil::proxyMon($url, $stats->getHandlerStat('http_code'), $module,round($stats->getHandlerStat('total_time'),4) * 1000); MonUtil::proxyMon($url, $stats->getHandlerStat('http_code'), $module,$this->request_time);
} }
}; };
//异步post请求 //异步post请求
...@@ -172,6 +180,10 @@ class Request ...@@ -172,6 +180,10 @@ class Request
$this->result['msg'] = $e->getMessage(); $this->result['msg'] = $e->getMessage();
} finally { } finally {
$this->addLog('post'); $this->addLog('post');
if ($use_mon && isset($this->result['code'])) {
MonUtil::proxyMon($url, $this->result['code'], $module,$this->request_time,false);
}
return $this->result; return $this->result;
} }
} }
......
...@@ -47,10 +47,11 @@ class MonUtil{ ...@@ -47,10 +47,11 @@ class MonUtil{
* @param string $module 请求模块 * @param string $module 请求模块
* @param string $url 请求第三方服务url * @param string $url 请求第三方服务url
* @param int $code 请求返回状态码 * @param int $code 请求返回状态码
* @param bool $http_code 是否是http code true 为http_code | false 为业务code
* @param int $request_time 请求总时长 * @param int $request_time 请求总时长
* @return string 打点信息或"" * @return string 打点信息或""
*/ */
public static function proxyMon($url, $code, $module='',$request_time = -899){ public static function proxyMon($url, $code, $module='',$request_time = -899,$http_code= false){
$result = ""; $result = "";
if(is_string($url) && !empty($url)){ if(is_string($url) && !empty($url)){
...@@ -69,6 +70,10 @@ class MonUtil{ ...@@ -69,6 +70,10 @@ class MonUtil{
$module = $domain.'.'.($request_module ?? 'other_depent'); $module = $domain.'.'.($request_module ?? 'other_depent');
} }
if ($http_code === true) {
$module .= 'http_';
}
//接口返回状态码打点 //接口返回状态码打点
if(!empty($code) && is_numeric($code)){ if(!empty($code) && is_numeric($code)){
$result = self::counting($module . "." . (string)$request_uri, (string)$code,1) . "\n"; $result = self::counting($module . "." . (string)$request_uri, (string)$code,1) . "\n";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment