增加日志链路页面
This commit is contained in:
+35
-7
@@ -1,16 +1,20 @@
|
||||
package com.central.log.controller;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.log.model.TraceLog;
|
||||
import com.central.log.service.TraceLogService;
|
||||
import com.central.search.client.service.IQueryService;
|
||||
import com.central.search.model.SearchDto;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 系统日志
|
||||
*
|
||||
@@ -18,19 +22,43 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
*/
|
||||
@RestController
|
||||
public class SysLogController {
|
||||
@Autowired
|
||||
/**
|
||||
* 系统日志索引名
|
||||
*/
|
||||
private static final String SYS_LOG_INDEXNAME = "sys-log-*";
|
||||
|
||||
@Resource
|
||||
private IQueryService queryService;
|
||||
|
||||
@Resource
|
||||
private TraceLogService traceLogService;
|
||||
|
||||
@ApiOperation(value = "系统日志全文搜索列表")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
|
||||
@ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer"),
|
||||
@ApiImplicitParam(name = "queryStr", value = "搜索关键字", dataType = "String")
|
||||
@ApiImplicitParam(name = "queryStr", value = "搜索关键字", dataType = "String"),
|
||||
@ApiImplicitParam(name = "sortCol", value = "排序字段", dataType = "String"),
|
||||
@ApiImplicitParam(name = "sortOrder", value = "排序顺序", dataType = "String"),
|
||||
@ApiImplicitParam(name = "isHighlighter", value = "是否显示高亮", dataType = "String")
|
||||
})
|
||||
@GetMapping(value = "/sysLog")
|
||||
public PageResult<JsonNode> getPage(SearchDto searchDto) {
|
||||
searchDto.setIsHighlighter(true);
|
||||
searchDto.setSortCol("timestamp");
|
||||
return queryService.strQuery("sys-log-*", searchDto);
|
||||
public PageResult<JsonNode> sysLog(SearchDto searchDto) {
|
||||
return queryService.strQuery(SYS_LOG_INDEXNAME, searchDto);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "系统日志链路列表")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "queryStr", value = "搜索关键字", dataType = "String"),
|
||||
@ApiImplicitParam(name = "sortCol", value = "排序字段", dataType = "String"),
|
||||
@ApiImplicitParam(name = "sortOrder", value = "排序顺序", dataType = "String"),
|
||||
@ApiImplicitParam(name = "isHighlighter", value = "是否显示高亮", dataType = "String")
|
||||
})
|
||||
@GetMapping(value = "/traceLog")
|
||||
public PageResult<TraceLog> traceLog(SearchDto searchDto) {
|
||||
PageResult<JsonNode> pageResult = queryService.strQuery(SYS_LOG_INDEXNAME, searchDto);
|
||||
List<JsonNode> jsonNodeList = pageResult.getData();
|
||||
List<TraceLog> logList = traceLogService.transTraceLog(jsonNodeList);
|
||||
return PageResult.<TraceLog>builder().data(logList).code(0).count((long) logList.size()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.central.log.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 日志链路对象
|
||||
*
|
||||
* @author zlt
|
||||
* @version 1.0
|
||||
* @date 2022/1/27
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
public class TraceLog {
|
||||
private String spanId;
|
||||
private String parentId;
|
||||
private String appName;
|
||||
private String serverIp;
|
||||
private String serverPort;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.central.log.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.utils.JsonUtil;
|
||||
import com.central.log.model.TraceLog;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 日志链路service
|
||||
*
|
||||
* @author zlt
|
||||
* @version 1.0
|
||||
* @date 2022/1/27
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Service
|
||||
public class TraceLogService {
|
||||
public List<TraceLog> transTraceLog(List<JsonNode> jsonNodeList) {
|
||||
List<TraceLog> logList = new ArrayList<>();
|
||||
Set<String> logSet = new HashSet<>();
|
||||
jsonNodeList.forEach(e -> {
|
||||
TraceLog log = JsonUtil.toObject(e, TraceLog.class);
|
||||
String spanId = log.getSpanId();
|
||||
if (StrUtil.isNotEmpty(spanId)) {
|
||||
if (spanId.length() == 1) {
|
||||
log.setParentId("-1");
|
||||
} else {
|
||||
log.setParentId(spanId.substring(0, spanId.length() - 2));
|
||||
}
|
||||
if (checkLog(genLogKey(log), logSet)) {
|
||||
logList.add(log);
|
||||
}
|
||||
}
|
||||
});
|
||||
return logList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过集合来去重
|
||||
*/
|
||||
private boolean checkLog(String logKey, Set<String> logSet) {
|
||||
if (logSet.contains(logKey)) {
|
||||
return false;
|
||||
} else {
|
||||
logSet.add(logKey);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private String genLogKey(TraceLog log) {
|
||||
return StrUtil.format("{}_{}_{}_{}", log.getAppName(), log.getSpanId(), log.getServerIp(), log.getServerPort());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user