Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
php_utils
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bp
php_utils
Commits
8770af2e
Commit
8770af2e
authored
Jun 21, 2021
by
yujiwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update:add 新增mon时间统一打点
parent
a9b7e8d1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
0 deletions
+106
-0
MonUtil.php
src/Mon/MonUtil.php
+29
-0
TimeCostUtil.php
src/Mon/TimeCostUtil.php
+77
-0
No files found.
src/Mon/MonUtil.php
View file @
8770af2e
...
...
@@ -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
);
}
}
/**
* 标量是任何可以测量的一维变量,可以通过显示的加入符号让系统进行值的累加,不加符号时添加的新值会覆盖旧值
*
...
...
src/Mon/TimeCostUtil.php
0 → 100644
View file @
8770af2e
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment