EsTool.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Tool\ShanTaoTool;
  3. use Tool\ShanTaoTool\Bean\ElasticSearch\CreateLogDocParamBean;
  4. use Tool\ShanTaoTool\Bean\ElasticSearch\GetLogDocParamBean;
  5. use Tool\ShanTaoTool\ElasticSearch\ElasticSerach;
  6. use Tool\ShanTaoTool\Exception\ElasticSearchException;
  7. class EsTool
  8. {
  9. /**
  10. * 写入请求日志
  11. * @param CreateLogDocParamBean $createLogDocParamBean
  12. */
  13. public static function createLogDoc(CreateLogDocParamBean $createLogDocParamBean)
  14. {
  15. //获取当前索引,以天为单位
  16. $index = date("Y-m-d");
  17. //1.创建索引,存在在报错
  18. ElasticSerach::getElasticSearchClient();
  19. try{
  20. $mapping = [
  21. "properties"=>[
  22. "request_id"=>[
  23. "type"=>"int",
  24. "fielddata"=>true
  25. ],
  26. "request_path"=>[
  27. "type"=>"string"
  28. ],
  29. "request_path_md5"=>[
  30. "type"=>"string"
  31. ],
  32. "request_param"=>[
  33. "type"=>"string"
  34. ],
  35. "request_response"=>[
  36. "type"=>"string"
  37. ],
  38. "request_sql_log"=>[
  39. "type"=>"string"
  40. ],
  41. "request_log"=>[
  42. "type"=>"string"
  43. ],
  44. "request_project_name"=>[
  45. "type"=>"string"
  46. ],
  47. "createdAt"=>[
  48. "type"=>"date",
  49. "format"=>"yyyy-MM-dd HH:mm:ss"
  50. ],
  51. "updatedAt"=>[
  52. "type"=>"date",
  53. "format"=>"yyyy-MM-dd HH:mm:ss"
  54. ]
  55. ]
  56. ];
  57. ElasticSerach::createIndex($index,$mapping);
  58. }catch (\Exception $exception){}
  59. //2.写入文档
  60. $responseDatas = is_bool($createLogDocParamBean->getRequestResponse())?"":$createLogDocParamBean->getRequestResponse();
  61. $doc = [
  62. "request_path"=>$createLogDocParamBean->getRequestPath(),
  63. "request_path_md5"=>md5($createLogDocParamBean->getRequestPath()),
  64. "request_param"=>$createLogDocParamBean->getRequestParam(),
  65. "request_response"=>$responseDatas,
  66. "request_sql_log"=>$createLogDocParamBean->getRequestSqlLog(),
  67. "request_log"=>$createLogDocParamBean->getRequestLog(),
  68. "request_project_name"=>$createLogDocParamBean->getRequestProjectName(),
  69. "created_at"=>is_numeric($createLogDocParamBean->getCreatedAt())?date("Y-m-d H:i:s",$createLogDocParamBean->getCreatedAt()):$createLogDocParamBean->getCreatedAt(),
  70. "updated_at"=>is_numeric($createLogDocParamBean->getUpdatedAt())?date("Y-m-d H:i:s",$createLogDocParamBean->getUpdatedAt()):$createLogDocParamBean->getUpdatedAt(),
  71. "request_id"=>$createLogDocParamBean->getRequestId(),
  72. "request_time"=>is_numeric($createLogDocParamBean->getUpdatedAt())?($createLogDocParamBean->getUpdatedAt()-$createLogDocParamBean->getCreatedAt())*1000:0,
  73. "unique_trace_id"=>$createLogDocParamBean->getUniqueTraceId()?$createLogDocParamBean->getUniqueTraceId():""
  74. ];
  75. return ElasticSerach::createDoc($index,$doc);
  76. }
  77. /**
  78. * 根据索引和项目名称获取日志数据
  79. * @param GetLogDocParamBean $getLogDocParamBean
  80. * @return array
  81. * @throws Exception\ElasticSearchException
  82. */
  83. public static function getLogDoc(GetLogDocParamBean $getLogDocParamBean)
  84. {
  85. ElasticSerach::getElasticSearchClient();
  86. return ElasticSerach::getLogDoc($getLogDocParamBean);
  87. }
  88. /**
  89. * 根据索引和链路ID获取链路数据
  90. * @param string $traceId 链路ID
  91. * @param string $index 索引
  92. * @return array
  93. * @throws Exception\ElasticSearchException
  94. */
  95. public static function getTraceLogDoc(string $traceId,$index)
  96. {
  97. ElasticSerach::getElasticSearchClient();
  98. return ElasticSerach::getTraceLogDoc($traceId,$index);
  99. }
  100. /**
  101. * 删除索引
  102. * @param string $index
  103. */
  104. public static function deleteIndex(string $index)
  105. {
  106. ElasticSerach::getElasticSearchClient();
  107. return ElasticSerach::deleteIndex($index);
  108. }
  109. }