Commit 8770af2e authored by yujiwei's avatar yujiwei

update:add 新增mon时间统一打点

parent a9b7e8d1
......@@ -116,6 +116,35 @@ class MonUtil{
return $result;
}
/**
* @param $module string 项目模块
* @param $event string 事件
* @param int $count
* @return string
*/
public static function send_count_msg(string $module,string $event, $count = 1)
{
if (!empty($_SERVER['YIDIAN_LOCAL_IP']))
{
$msg = sprintf("Ydbot.statsd.superfe.bp.%s.%s.1sec:%s|c", $module,$event, $count);
self::base($msg);
}
}
/**
* @param string $module string 项目模块
* @param $func_name string 函数或者监听的事件名称
* @param $cost
*/
public static function send_time_cost_msg(string $module,$func_name, $cost)
{
if (!empty($_SERVER['YIDIAN_LOCAL_IP']))
{
$msg = sprintf("Ydbot.statsd.superfe.bp.%s.%s.1sec:%s|ms", $module, $func_name, $cost);
self::base($msg);
}
}
/**
* 标量是任何可以测量的一维变量,可以通过显示的加入符号让系统进行值的累加,不加符号时添加的新值会覆盖旧值
*
......
<?php
namespace Api\PhpUtils\Mon;
class TimeCostUtil
{
private $time_costs;
public static $one;
private function __construct()
{
$this->time_costs = array();
}
public static function getSingleton()
{
if (self::$one === null)
{
self::$one = new TimeCostUtil();
self::$one->record_time('req_all_cost');
}
return self::$one;
}
public function record_time($name)
{
$this->time_costs[$name][] = microtime(true);
}
/**
* 获取消耗的时间
* @param $name
* @return bool|mixed
*/
public function get_time_cost($name)
{
if (isset($this->time_costs[$name]) && count($this->time_costs[$name]) === 2)
{
return $this->time_costs[$name][1] - $this->time_costs[$name][0];
}
return false;
}
/**
* 获取所有消耗的时间
* @param $modules
* @return array
*/
public function get_all_time_cost($modules='')
{
if(empty($modules)) {
$modules = $_SERVER['SERVER_NAME'] ?? '';
}
//因grafana打点路径以"."分割,将服务器名中的"."转化为"_"
$modules = str_replace(".", "_", $modules);
if (self::$one !== null) {
self::$one->record_time('req_all_cost');
}
$result = array();
foreach ($this->time_costs as $name => $times)
{
if (count($times) === 2)
{
$result[$name] = $times[1] - $times[0];
MonUtil::send_time_cost_msg($modules,$name, $result[$name]);
} else {
$result[$name] = false;
}
}
return $result;
}
}
\ No newline at end of file
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