Commit 1368a385 authored by yujiwei's avatar yujiwei

update:mongodb add mon打点

parent ccd5ae20
...@@ -84,6 +84,23 @@ class MonUtil{ ...@@ -84,6 +84,23 @@ class MonUtil{
return $result; 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 $module
* @param string $index * @param string $index
......
...@@ -7,12 +7,13 @@ use MongoDB\Driver\Manager; ...@@ -7,12 +7,13 @@ use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadConcern; use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference; use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern; use MongoDB\Driver\WriteConcern;
use Api\PhpUtils\Mon\MonUtil;
/** /**
* MongoDB基类 * MongoDB基类
* *
* 1. MongoDB配置信息在yaconf的mongo.ini中,configIndex, databaseName, collectionName 在子类中设置 * 1. MongoDB配置信息在yaconf的mongo.ini中,configIndex, databaseName, collectionName 在子类中设置
* *
* 2. writeConcern, readConcern, readPreference, typeMap 是可以从MongoDB\Driver\Manager继承的, * 2. writeConcern, readConcern, readPreference, typeMap 是可以从MongoDB\Driver\Manager继承的,
* 可以参考扩展API文档修改 * 可以参考扩展API文档修改
* $options = [ * $options = [
...@@ -22,16 +23,16 @@ use MongoDB\Driver\WriteConcern; ...@@ -22,16 +23,16 @@ use MongoDB\Driver\WriteConcern;
* 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), * 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
* ]; * ];
* @see https://www.php.net/manual/zh/book.mongodb.php * @see https://www.php.net/manual/zh/book.mongodb.php
* *
* 3. 提供常用的CURD方法,共他方法会调用 __call()方法 * 3. 提供常用的CURD方法,共他方法会调用 __call()方法
* *
* 4. MongoDB建议对重要的数据使用 {w: “marjority”} 的选项 (WriteConcern::MAJORITY)。 * 4. MongoDB建议对重要的数据使用 {w: “marjority”} 的选项 (WriteConcern::MAJORITY)。
* 可以保证数据在复制到多数节点后才返回成功结果。使用该机制可以有效防止数据回滚的发生。 * 可以保证数据在复制到多数节点后才返回成功结果。使用该机制可以有效防止数据回滚的发生。
* 另外你可以使用 {j:1} (可以和 w:”majrotiy” 结合使用)来指定数据必须在写入WAL日志之后才向应用返回成功确认。这个会导致写入性能有所下降,但是对于重要的数据可以考虑使用 * 另外你可以使用 {j:1} (可以和 w:”majrotiy” 结合使用)来指定数据必须在写入WAL日志之后才向应用返回成功确认。这个会导致写入性能有所下降,但是对于重要的数据可以考虑使用
* *
* 5. 异常的捕获与处理 及 getMatchedCount(), getModifiedCount(), getUpsertedCount() 在业务中按需获取,写入retry(1s,2s,4s...) * 5. 异常的捕获与处理 及 getMatchedCount(), getModifiedCount(), getUpsertedCount() 在业务中按需获取,写入retry(1s,2s,4s...)
* *
* 6. 根据镜像中php的monogodb扩展版本选择使用1.4版本 * 6. 根据镜像中php的monogodb扩展版本选择使用1.4版本
* @see https://docs.mongodb.com/drivers/php/ 驱动兼容对照表 * @see https://docs.mongodb.com/drivers/php/ 驱动兼容对照表
* @see https://docs.mongodb.com/php-library/v1.4/ 官方文档 * @see https://docs.mongodb.com/php-library/v1.4/ 官方文档
*/ */
...@@ -123,7 +124,7 @@ abstract class MongoBase ...@@ -123,7 +124,7 @@ abstract class MongoBase
if (!$this->collection) { if (!$this->collection) {
return false; return false;
} }
return call_user_func_array([$this->collection, $method], $args); return call_user_func_array([$this->collection, $method], $args);
} }
...@@ -150,7 +151,7 @@ abstract class MongoBase ...@@ -150,7 +151,7 @@ abstract class MongoBase
* *
* @return object * @return object
*/ */
public function getClient() public function getClient()
{ {
return $this->client; return $this->client;
} }
...@@ -160,7 +161,7 @@ abstract class MongoBase ...@@ -160,7 +161,7 @@ abstract class MongoBase
* *
* @return object * @return object
*/ */
public function getDatabase() public function getDatabase()
{ {
return $this->database; return $this->database;
} }
...@@ -170,7 +171,7 @@ abstract class MongoBase ...@@ -170,7 +171,7 @@ abstract class MongoBase
* *
* @return object * @return object
*/ */
public function getCollection() public function getCollection()
{ {
return $this->collection; return $this->collection;
} }
...@@ -179,11 +180,11 @@ abstract class MongoBase ...@@ -179,11 +180,11 @@ abstract class MongoBase
* 集合的初始化参数 * 集合的初始化参数
* *
* 可以继承初始化配置文件中,默认的,或都子类中的 * 可以继承初始化配置文件中,默认的,或都子类中的
* *
* @param array $collectionOptions * @param array $collectionOptions
* @return array * @return array
*/ */
protected function getCollectionOptions(array $collectionOptions = []) protected function getCollectionOptions(array $collectionOptions = [])
{ {
$collectionOptions += [ $collectionOptions += [
'readConcern' => $this->client->getreadConcern() ?: new ReadConcern(ReadConcern::LOCAL), //3.2之前的服务器版本不支持此功能 'readConcern' => $this->client->getreadConcern() ?: new ReadConcern(ReadConcern::LOCAL), //3.2之前的服务器版本不支持此功能
...@@ -191,7 +192,7 @@ abstract class MongoBase ...@@ -191,7 +192,7 @@ abstract class MongoBase
'typeMap' => ['root' => 'array', 'document' => 'array'], 'typeMap' => ['root' => 'array', 'document' => 'array'],
'writeConcern' => $this->client->getWriteConcern() ?: new WriteConcern(self::W, 3 * self::WTIMEOUTMS), 'writeConcern' => $this->client->getWriteConcern() ?: new WriteConcern(self::W, 3 * self::WTIMEOUTMS),
]; ];
return $collectionOptions; return $collectionOptions;
} }
...@@ -201,7 +202,7 @@ abstract class MongoBase ...@@ -201,7 +202,7 @@ abstract class MongoBase
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertOne/ * @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertOne/
* @param array|object $document The document to insert * @param array|object $document The document to insert
* @param array $options Command options * @param array $options Command options
* @return InsertOneResult * @return \MongoDB\InsertOneResult
* @throws InvalidArgumentException for parameter/option parsing errors * @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors) * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/ */
...@@ -210,7 +211,13 @@ abstract class MongoBase ...@@ -210,7 +211,13 @@ abstract class MongoBase
$options += [ $options += [
'writeConcern' => new WriteConcern(self::W, 3 * self::WTIMEOUTMS), // W=1要求进行写入确认, 超时时间3000毫秒 '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 ...@@ -219,7 +226,7 @@ abstract class MongoBase
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertMany/ * @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-insertMany/
* @param array[]|object[] $documents The documents to insert * @param array[]|object[] $documents The documents to insert
* @param array $options Command options * @param array $options Command options
* @return InsertManyResult * @return \MongoDB\InsertManyResult
* @throws InvalidArgumentException for parameter/option parsing errors * @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors) * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/ */
...@@ -229,16 +236,21 @@ abstract class MongoBase ...@@ -229,16 +236,21 @@ abstract class MongoBase
'ordered' => true, // 默认值为true, 当单个写入失败时,该操作将停止而不执行剩余的写入操作并引发异常 'ordered' => true, // 默认值为true, 当单个写入失败时,该操作将停止而不执行剩余的写入操作并引发异常
'writeConcern' => new WriteConcern(self::W, 5 * self::WTIMEOUTMS), '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;
} }
/** /**
* Deletes at most one document matching the filter. * Deletes at most one document matching the filter.
* *
* @see https://docs.mongodb.com/php-library/v1.4/reference/method/MongoDBCollection-deleteOne/ * @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|object $filter Query by which to delete documents
* @param array $options Command options * @param array $options Command options
* @return DeleteResult * @return \MongoDB\DeleteResult
* @throws UnsupportedException if options are not supported by the selected server * @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors * @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors) * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
...@@ -248,9 +260,13 @@ abstract class MongoBase ...@@ -248,9 +260,13 @@ abstract class MongoBase
$options += [ $options += [
'writeConcern' => new WriteConcern(self::W, 3 * self::WTIMEOUTMS), '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;
} }
/** /**
* Deletes all documents matching the filter. * Deletes all documents matching the filter.
* *
...@@ -286,9 +302,13 @@ abstract class MongoBase ...@@ -286,9 +302,13 @@ abstract class MongoBase
$options += [ $options += [
'maxTimeMS' => 3 * self::MAXTIMEMS, '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;
} }
/** /**
* Finds documents matching the query. * Finds documents matching the query.
* *
...@@ -382,7 +402,7 @@ abstract class MongoBase ...@@ -382,7 +402,7 @@ abstract class MongoBase
]; ];
return $this->collection->updateOne($filter, $update, $options); return $this->collection->updateOne($filter, $update, $options);
} }
/** /**
* Updates all documents matching the filter. * Updates all documents matching the filter.
* *
......
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