Commit 1368a385 authored by yujiwei's avatar yujiwei

update:mongodb add mon打点

parent ccd5ae20
......@@ -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
......
......@@ -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;
}
/**
......
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