Commit 406bf6fa authored by yujiwei's avatar yujiwei

update:依赖基础数据库增加mon 打点

parent 55b92d6d
...@@ -84,26 +84,30 @@ class MonUtil{ ...@@ -84,26 +84,30 @@ class MonUtil{
return $result; return $result;
} }
/**
public static function mongoMon($mongo_host,$operator,$request_time = -799) * @param $db_host string 链接数据的集群
* @param $operator string 数据库操作
* @param $db_name string 数据库名字 比如mysql ,mongodb
* @param int $request_time
*/
public static function dbMon($db_host,$operator,$db_name,$request_time = -799)
{ {
if ( empty($operator) || !is_string($mongo_host)) { if ( empty($operator) || !is_string($db_host) || empty($db_name)) {
return ; return ;
} }
$host = '';
if (empty($mongo_host)) { if (empty($db_host)) {
$host = 'unknow'; $host = 'unknow';
} else { } else {
$host = str_replace(array(".", ":", "/"),array("_", "_", "-"), $mongo_host); $host = str_replace(array(".", ":", "/"),array("_", "_", "-"), $db_host);
} }
//记录请求量 //记录请求量
self::counting('mongodb.'.$host , (string)$operator,0); self::counting($db_name.'.'.$host.(string)$operator,0,1);
//耗时打点 //耗时打点
if(!empty($request_time) && is_numeric($request_time) && $request_time != -799){ if(!empty($request_time) && is_numeric($request_time) && $request_time != -799){
$result = self::timing((string)('mongodb.'.$host.$operator), "TotalTime", $request_time); $result = self::timing(($db_name.'.'.$host.$operator), "TotalTime", $request_time);
} }
} }
......
...@@ -217,7 +217,7 @@ abstract class MongoBase ...@@ -217,7 +217,7 @@ abstract class MongoBase
$start_time = microtime(true); $start_time = microtime(true);
$res = $this->collection->insertOne($document, $options); $res = $this->collection->insertOne($document, $options);
$end_time = microtime(true); $end_time = microtime(true);
MonUtil::mongoMon($this->host ?? 'unknow','insertOne',$end_time-$start_time); MonUtil::dbMon($this->host ?? 'unknow','insertOne','mongodb',$end_time-$start_time);
return $res; return $res;
} }
...@@ -241,7 +241,7 @@ abstract class MongoBase ...@@ -241,7 +241,7 @@ abstract class MongoBase
$start_time = microtime(true); $start_time = microtime(true);
$res = $this->collection->insertMany($documents, $options); $res = $this->collection->insertMany($documents, $options);
$end_time = microtime(true); $end_time = microtime(true);
MonUtil::mongoMon($this->host ?? 'unknow','insertMany',$end_time-$start_time); MonUtil::dbMon($this->host ?? 'unknow','insertMany','mongodb',$end_time-$start_time);
return $res; return $res;
} }
...@@ -265,7 +265,7 @@ abstract class MongoBase ...@@ -265,7 +265,7 @@ abstract class MongoBase
$start_time = microtime(true); $start_time = microtime(true);
$res = $this->collection->deleteOne($filter, $options); $res = $this->collection->deleteOne($filter, $options);
$end_time = microtime(true); $end_time = microtime(true);
MonUtil::mongoMon($this->host ?? 'unknow','deleteOne',$end_time-$start_time); MonUtil::dbMon($this->host ?? 'unknow','deleteOne','mongodb',$end_time-$start_time);
return $res; return $res;
} }
...@@ -307,7 +307,7 @@ abstract class MongoBase ...@@ -307,7 +307,7 @@ abstract class MongoBase
$start_time = microtime(true); $start_time = microtime(true);
$res = $this->collection->findOne($filter, $options); $res = $this->collection->findOne($filter, $options);
$end_time = microtime(true); $end_time = microtime(true);
MonUtil::mongoMon($this->host ?? 'unknow','findOne',$end_time-$start_time); MonUtil::dbMon($this->host ?? 'unknow','findOne','mongodb',$end_time-$start_time);
return $res; return $res;
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Api\PhpUtils\Mysql; namespace Api\PhpUtils\Mysql;
use Api\PhpUtils\Mon\MonUtil;
use Api\PhpUtils\Mysql\Medoo; use Api\PhpUtils\Mysql\Medoo;
use Api\PhpUtils\Log\FileLog; use Api\PhpUtils\Log\FileLog;
...@@ -183,6 +184,9 @@ abstract class MysqlClusterBase ...@@ -183,6 +184,9 @@ abstract class MysqlClusterBase
} }
self::$dbCurrentConnect = self::$dbConnect[$type.':'.$dbShardingIndex]; self::$dbCurrentConnect = self::$dbConnect[$type.':'.$dbShardingIndex];
//增加mon 打点
$start_time = microtime(true);
self::$sqlResut = call_user_func_array([self::$dbConnect[$type.':'.$dbShardingIndex], $method], $arguments); self::$sqlResut = call_user_func_array([self::$dbConnect[$type.':'.$dbShardingIndex], $method], $arguments);
if (!self::catchError($method, $arguments)) { if (!self::catchError($method, $arguments)) {
...@@ -193,6 +197,9 @@ abstract class MysqlClusterBase ...@@ -193,6 +197,9 @@ abstract class MysqlClusterBase
return false; return false;
} }
} }
$end_time = microtime(true);
MonUtil::dbMon(self::dbCurrentConnect['server'].'_'.self::dbCurrentConnect['port'] ?? 'unknow_mysql',$method,'mysql',$end_time-$start_time);
return self::formatResult($method, $arguments); return self::formatResult($method, $arguments);
} }
...@@ -201,7 +208,7 @@ abstract class MysqlClusterBase ...@@ -201,7 +208,7 @@ abstract class MysqlClusterBase
* *
* @param [string] $type 读或写 * @param [string] $type 读或写
* @param boolean $flush 是否强制重新连接,重试等场景使用 * @param boolean $flush 是否强制重新连接,重试等场景使用
* @return void * @return
*/ */
public static function getConnection($type, $dbShardingIndex, $flush = false) public static function getConnection($type, $dbShardingIndex, $flush = false)
{ {
......
...@@ -4,6 +4,7 @@ namespace Api\PhpUtils\Redis; ...@@ -4,6 +4,7 @@ namespace Api\PhpUtils\Redis;
use Api\PhpUtils\Http\Request; use Api\PhpUtils\Http\Request;
use Api\PhpUtils\Cache\ApcuUtil; use Api\PhpUtils\Cache\ApcuUtil;
use Api\PhpUtils\Log\FileLog; use Api\PhpUtils\Log\FileLog;
use Api\PhpUtils\Mon\MonUtil;
use Redis; use Redis;
class RedisUtil class RedisUtil
...@@ -653,6 +654,12 @@ class RedisUtil ...@@ -653,6 +654,12 @@ class RedisUtil
if (!$this->redis) { if (!$this->redis) {
return false; return false;
} }
return call_user_func_array([$this->redis, $method], $args); //增加mon打点
$start_time = microtime(true);
$res = call_user_func_array([$this->redis, $method], $args);
$end_time = microtime(true);
MonUtil::dbMon($this->connectServer ?? 'unknow_redis',$method,'redis',$end_time-$start_time);
return $res;
} }
} }
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