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
1368a385
Commit
1368a385
authored
Jun 22, 2021
by
yujiwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update:mongodb add mon打点
parent
ccd5ae20
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
25 deletions
+62
-25
MonUtil.php
src/Mon/MonUtil.php
+17
-0
MongoBase.php
src/Mongo/MongoBase.php
+45
-25
No files found.
src/Mon/MonUtil.php
View file @
1368a385
...
...
@@ -84,6 +84,23 @@ class MonUtil{
return
$result
;
}
public
static
function
mongoMon
(
$mysql_host
,
$operator
,
$request_time
=
-
799
)
{
if
(
empty
(
$mysql_host
)
||
empty
(
$operator
))
{
return
;
}
//记录请求量
self
::
counting
(
'mongodb.'
.
$mysql_host
,
(
string
)
$operator
,
0
)
.
"
\n
"
;
//耗时打点
if
(
!
empty
(
$request_time
)
&&
is_numeric
(
$request_time
)
&&
$request_time
!=
-
799
){
$result
=
self
::
timing
((
string
)(
'mongodb.'
.
$mysql_host
.
$operator
),
"TotalTime"
,
$request_time
);
}
}
/**
* @param string $module
* @param string $index
...
...
src/Mongo/MongoBase.php
View file @
1368a385
...
...
@@ -7,6 +7,7 @@ use MongoDB\Driver\Manager;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
use
Api\PhpUtils\Mon\MonUtil
;
/**
* MongoDB基类
...
...
@@ -201,7 +202,7 @@ abstract class MongoBase
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertOne/
* @param array|object $document The document to insert
* @param array $options Command options
* @return InsertOneResult
* @return
\MongoDB\
InsertOneResult
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
...
...
@@ -210,7 +211,13 @@ abstract class MongoBase
$options
+=
[
'writeConcern'
=>
new
WriteConcern
(
self
::
W
,
3
*
self
::
WTIMEOUTMS
),
// W=1要求进行写入确认, 超时时间3000毫秒
];
return
$this
->
collection
->
insertOne
(
$document
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
insertOne
(
$document
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'insertOne'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
...
...
@@ -219,7 +226,7 @@ abstract class MongoBase
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertMany/
* @param array[]|object[] $documents The documents to insert
* @param array $options Command options
* @return InsertManyResult
* @return
\MongoDB\
InsertManyResult
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
...
...
@@ -229,7 +236,12 @@ abstract class MongoBase
'ordered'
=>
true
,
// 默认值为true, 当单个写入失败时,该操作将停止而不执行剩余的写入操作并引发异常
'writeConcern'
=>
new
WriteConcern
(
self
::
W
,
5
*
self
::
WTIMEOUTMS
),
];
return
$this
->
collection
->
insertMany
(
$documents
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
insertMany
(
$documents
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'insertMany'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
...
...
@@ -238,7 +250,7 @@ abstract class MongoBase
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-deleteOne/
* @param array|object $filter Query by which to delete documents
* @param array $options Command options
* @return DeleteResult
* @return
\MongoDB\
DeleteResult
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
...
...
@@ -248,7 +260,11 @@ abstract class MongoBase
$options
+=
[
'writeConcern'
=>
new
WriteConcern
(
self
::
W
,
3
*
self
::
WTIMEOUTMS
),
];
return
$this
->
collection
->
deleteOne
(
$filter
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
deleteOne
(
$filter
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'deleteOne'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
...
...
@@ -286,7 +302,11 @@ abstract class MongoBase
$options
+=
[
'maxTimeMS'
=>
3
*
self
::
MAXTIMEMS
,
];
return
$this
->
collection
->
findOne
(
$filter
,
$options
);
$start_time
=
microtime
(
true
);
$res
=
$this
->
collection
->
findOne
(
$filter
,
$options
);
$end_time
=
microtime
(
true
);
MonUtil
::
mongoMon
(
$this
->
client
->
getManager
()
->
getServers
(),
'findOne'
,
$end_time
-
$start_time
);
return
$res
;
}
/**
...
...
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