123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace Tool\MayouTool;
- use Tool\MayouTool\Bean\ElasticSearch\CreateLogDocParamBean;
- use Tool\MayouTool\Bean\ElasticSearch\GetLogDocParamBean;
- use Tool\MayouTool\ElasticSearch\ElasticSerach;
- use Tool\MayouTool\Exception\ElasticSearchException;
- class EsTool
- {
- /**
- * 写入请求日志
- * @param CreateLogDocParamBean $createLogDocParamBean
- */
- public static function createLogDoc(CreateLogDocParamBean $createLogDocParamBean)
- {
- //获取当前索引,以天为单位
- $index = date("Y-m-d");
- //1.创建索引,存在在报错
- ElasticSerach::getElasticSearchClient();
- try{
- $mapping = [
- "properties"=>[
- "request_id"=>[
- "type"=>"int",
- "fielddata"=>true
- ],
- "request_path"=>[
- "type"=>"string"
- ],
- "request_path_md5"=>[
- "type"=>"string"
- ],
- "request_param"=>[
- "type"=>"string"
- ],
- "request_response"=>[
- "type"=>"string"
- ],
- "request_sql_log"=>[
- "type"=>"string"
- ],
- "request_log"=>[
- "type"=>"string"
- ],
- "request_project_name"=>[
- "type"=>"string"
- ],
- "createdAt"=>[
- "type"=>"date",
- "format"=>"yyyy-MM-dd HH:mm:ss"
- ],
- "updatedAt"=>[
- "type"=>"date",
- "format"=>"yyyy-MM-dd HH:mm:ss"
- ]
- ]
- ];
- ElasticSerach::createIndex($index,$mapping);
- }catch (\Exception $exception){}
- //2.写入文档
- $responseDatas = is_bool($createLogDocParamBean->getRequestResponse())?"":$createLogDocParamBean->getRequestResponse();
- $doc = [
- "request_path"=>$createLogDocParamBean->getRequestPath(),
- "request_path_md5"=>md5($createLogDocParamBean->getRequestPath()),
- "request_param"=>$createLogDocParamBean->getRequestParam(),
- "request_response"=>$responseDatas,
- "request_sql_log"=>$createLogDocParamBean->getRequestSqlLog(),
- "request_log"=>$createLogDocParamBean->getRequestLog(),
- "request_project_name"=>$createLogDocParamBean->getRequestProjectName(),
- "created_at"=>is_numeric($createLogDocParamBean->getCreatedAt())?date("Y-m-d H:i:s",$createLogDocParamBean->getCreatedAt()):$createLogDocParamBean->getCreatedAt(),
- "updated_at"=>is_numeric($createLogDocParamBean->getUpdatedAt())?date("Y-m-d H:i:s",$createLogDocParamBean->getUpdatedAt()):$createLogDocParamBean->getUpdatedAt(),
- "request_id"=>$createLogDocParamBean->getRequestId(),
- "request_time"=>is_numeric($createLogDocParamBean->getUpdatedAt())?($createLogDocParamBean->getUpdatedAt()-$createLogDocParamBean->getCreatedAt())*1000:0,
- "unique_trace_id"=>$createLogDocParamBean->getUniqueTraceId()?$createLogDocParamBean->getUniqueTraceId():""
- ];
- return ElasticSerach::createDoc($index,$doc);
- }
- /**
- * 根据索引和项目名称获取日志数据
- * @param GetLogDocParamBean $getLogDocParamBean
- * @return array
- * @throws Exception\ElasticSearchException
- */
- public static function getLogDoc(GetLogDocParamBean $getLogDocParamBean)
- {
- ElasticSerach::getElasticSearchClient();
- return ElasticSerach::getLogDoc($getLogDocParamBean);
- }
- /**
- * 根据索引和链路ID获取链路数据
- * @param string $traceId 链路ID
- * @param string $index 索引
- * @return array
- * @throws Exception\ElasticSearchException
- */
- public static function getTraceLogDoc(string $traceId,$index)
- {
- ElasticSerach::getElasticSearchClient();
- return ElasticSerach::getTraceLogDoc($traceId,$index);
- }
- /**
- * 删除索引
- * @param string $index
- */
- public static function deleteIndex(string $index)
- {
- ElasticSerach::getElasticSearchClient();
- return ElasticSerach::deleteIndex($index);
- }
- }
|