提交
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
package com.tashow.cloud.app.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.tashow.cloud.app.config;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 飞书客户端配置
|
||||
* 用于初始化FeiShuAlertClient的相关依赖
|
||||
*/
|
||||
@Configuration
|
||||
public class FeiShuClientConfig {
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
/* @PostConstruct
|
||||
public void initFeiShuClient() {
|
||||
FeiShuAlertClient.setRedisTemplate(stringRedisTemplate);
|
||||
}*/
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
package com.tashow.cloud.app.controller;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.lark.oapi.core.utils.Decryptor;
|
||||
import com.tashow.cloud.app.service.feishu.FeiShuCardDataService;
|
||||
@@ -11,27 +12,27 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class FeishuController {
|
||||
private static final Logger log = LoggerFactory.getLogger(FeishuController.class);
|
||||
private static final String ACTION_COMPLETE_ALARM = "complete_alarm";
|
||||
|
||||
private final FeiShuAlertClient feiShuAlertClient;
|
||||
private final FeiShuCardDataService feiShuCardDataService;
|
||||
private final LarkConfig larkConfig;
|
||||
|
||||
|
||||
@Autowired
|
||||
public FeishuController(FeiShuAlertClient feiShuAlertClient,
|
||||
FeiShuCardDataService feiShuCardDataService,
|
||||
LarkConfig larkConfig) {
|
||||
public FeishuController(FeiShuAlertClient feiShuAlertClient, FeiShuCardDataService feiShuCardDataService, LarkConfig larkConfig) {
|
||||
this.feiShuAlertClient = feiShuAlertClient;
|
||||
this.feiShuCardDataService = feiShuCardDataService;
|
||||
this.larkConfig = larkConfig;
|
||||
}
|
||||
|
||||
@RequestMapping("/card1")
|
||||
|
||||
@RequestMapping("/card")
|
||||
@PermitAll
|
||||
public String card(@RequestBody JSONObject data) {
|
||||
try {
|
||||
@@ -41,30 +42,23 @@ public class FeishuController {
|
||||
if (value != null && ACTION_COMPLETE_ALARM.equals(value.getStr("action"))) {
|
||||
String messageId = data.getStr("open_message_id");
|
||||
Map<String, Object> templateData = feiShuCardDataService.getCardData(messageId);
|
||||
templateData.put("open_id", data.getStr("open_id"));
|
||||
templateData.put("complete_time", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
JSONObject fromValue = action.getJSONObject("form_value");
|
||||
templateData.put("notes", fromValue.getStr("notes_input"));
|
||||
return feiShuAlertClient.buildCardWithData(larkConfig.getSuccessCards(), templateData);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.containsKey("encrypt")) {
|
||||
Decryptor decryptor = new Decryptor(larkConfig.getEncryptKey());
|
||||
return decryptor.decrypt(data.getStr("encrypt"));
|
||||
}
|
||||
|
||||
return "{}";
|
||||
} catch (Exception e) {
|
||||
log.error("卡片处理异常", e);
|
||||
return "{\"code\":1,\"msg\":\"处理异常: " + e.getMessage() + "\"}";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送并存储卡片消息
|
||||
*/
|
||||
public String sendAndStoreCardMessage(String chatId, String templateId, Map<String, Object> templateData) throws Exception {
|
||||
String messageId = feiShuAlertClient.sendCardMessage(chatId, templateId, templateData);
|
||||
if (messageId != null) {
|
||||
feiShuCardDataService.saveCardData(messageId, templateData);
|
||||
}
|
||||
return messageId;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,10 +13,6 @@ import java.util.Map;
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class TestController {
|
||||
|
||||
private final BuriedPointProducer buriedPointProducer;
|
||||
private final BuriedPointMapper buriedPointMapper;
|
||||
|
||||
/**
|
||||
* 基础测试接口
|
||||
*/
|
||||
@@ -25,18 +21,5 @@ public class TestController {
|
||||
public String test() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试埋点拦截器
|
||||
* 这个接口会被埋点拦截器自动记录请求信息
|
||||
*/
|
||||
@GetMapping("/test/buried-point")
|
||||
@PermitAll
|
||||
public Map<String, Object> testBuriedPoint() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("success", true);
|
||||
result.put("message", "埋点拦截器测试成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.tashow.cloud.app.ext;
|
||||
|
||||
import com.lark.oapi.core.request.EventReq;
|
||||
import com.lark.oapi.core.response.EventResp;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class HttpTranslator {
|
||||
|
||||
private Map<String, List<String>> toHeaderMap(HttpServletRequest req) {
|
||||
Map<String, List<String>> headers = new HashMap<>();
|
||||
Enumeration<String> names = req.getHeaderNames();
|
||||
while (names.hasMoreElements()) {
|
||||
String name = names.nextElement();
|
||||
List<String> values = Collections.list(req.getHeaders(name));
|
||||
headers.put(name, values);
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
public EventReq translate(HttpServletRequest request) throws IOException {
|
||||
String bodyStr = request.getReader().lines()
|
||||
.collect(Collectors.joining(System.lineSeparator()));
|
||||
EventReq req = new EventReq();
|
||||
req.setHeaders(toHeaderMap(request));
|
||||
req.setBody(bodyStr.getBytes(StandardCharsets.UTF_8));
|
||||
req.setHttpPath(request.getRequestURI());
|
||||
return req;
|
||||
}
|
||||
|
||||
public void write(HttpServletResponse response, EventResp eventResp) throws IOException {
|
||||
response.setStatus(eventResp.getStatusCode());
|
||||
eventResp.getHeaders().entrySet().stream().forEach(keyValues -> {
|
||||
String key = keyValues.getKey();
|
||||
List<String> values = keyValues.getValue();
|
||||
values.stream().forEach(v -> response.addHeader(key, v));
|
||||
});
|
||||
if (eventResp.getBody() != null) {
|
||||
response.getWriter().write(new String(eventResp.getBody()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.tashow.cloud.app.ext;
|
||||
|
||||
import com.lark.oapi.card.CardActionHandler;
|
||||
import com.lark.oapi.core.request.EventReq;
|
||||
import com.lark.oapi.core.response.EventResp;
|
||||
import com.lark.oapi.event.EventDispatcher;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Servlet的适配器,用于适配基于Servlet技术栈实现的Web服务
|
||||
*/
|
||||
public class ServletAdapter {
|
||||
|
||||
private static final HttpTranslator HTTP_TRANSLATOR = new HttpTranslator();
|
||||
|
||||
/**
|
||||
* 处理消息事件
|
||||
*
|
||||
* @param req
|
||||
* @param response
|
||||
* @param eventDispatcher
|
||||
* @throws Throwable
|
||||
*/
|
||||
public void handleEvent(HttpServletRequest req, HttpServletResponse response,
|
||||
EventDispatcher eventDispatcher) throws Throwable {
|
||||
// 转换请求对象
|
||||
EventReq eventReq = HTTP_TRANSLATOR.translate(req);
|
||||
|
||||
// 处理请求
|
||||
EventResp resp = eventDispatcher.handle(eventReq);
|
||||
|
||||
// 回写结果
|
||||
HTTP_TRANSLATOR.write(response, resp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理卡片消息
|
||||
*
|
||||
* @param req
|
||||
* @param response
|
||||
* @param handler
|
||||
* @throws Throwable
|
||||
*/
|
||||
public void handleCardAction(HttpServletRequest req, HttpServletResponse response,
|
||||
CardActionHandler handler) throws Throwable {
|
||||
// 转换请求对象
|
||||
EventReq eventReq = HTTP_TRANSLATOR.translate(req);
|
||||
|
||||
// 处理请求
|
||||
EventResp resp = handler.handle(eventReq);
|
||||
|
||||
// 回写结果
|
||||
HTTP_TRANSLATOR.write(response, resp);
|
||||
}
|
||||
}
|
||||
@@ -26,69 +26,21 @@ import java.net.UnknownHostException;
|
||||
public class BuriedPointInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final BuriedPointProducer buriedPointProducer;
|
||||
|
||||
private static final String ATTRIBUTE_STOPWATCH = "BuriedPoint.StopWatch";
|
||||
private static final String ATTRIBUTE_REQUEST_ID = "BuriedPoint.RequestId";
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
if (!(handler instanceof HandlerMethod)) {
|
||||
if (!(handler instanceof HandlerMethod handlerMethod)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
request.setAttribute(ATTRIBUTE_STOPWATCH, stopWatch);
|
||||
|
||||
int requestId = (int)(Math.abs(IdUtil.getSnowflakeNextId()) % Integer.MAX_VALUE);
|
||||
request.setAttribute(ATTRIBUTE_REQUEST_ID, requestId);
|
||||
HandlerMethod handlerMethod = (HandlerMethod) handler;
|
||||
String method = request.getMethod() + " " + request.getRequestURI()+ JsonUtils.toJsonString(request.getParameterMap());
|
||||
String controllerName = handlerMethod.getBeanType().getSimpleName();
|
||||
String actionName = handlerMethod.getMethod().getName();
|
||||
BuriedMessages message = new BuriedMessages();
|
||||
message.setId(requestId);
|
||||
message.setEventTime(new java.util.Date());
|
||||
message.setService(SpringUtils.getApplicationName());
|
||||
message.setMethod(method);
|
||||
message.setUserId(getUserId(request));
|
||||
message.setSessionId(request.getSession().getId());
|
||||
message.setClientIp(ServletUtils.getClientIP(request));
|
||||
message.setServerIp(getServerIp());
|
||||
message.setEventType("API_REQUEST_START");
|
||||
message.setPagePath(controllerName + "#" + actionName);
|
||||
message.setStatusCode(BuriedMessages.STATUS_PROCESSING);
|
||||
BuriedMessages message = new BuriedMessages(
|
||||
request,
|
||||
handlerMethod
|
||||
);
|
||||
buriedPointProducer.asyncSendMessage(message);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("[埋点] 收集请求开始数据: {}", message);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[埋点] 埋点数据收集异常", e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录用户ID
|
||||
* 如果未登录返回匿名标识
|
||||
*/
|
||||
private String getUserId(HttpServletRequest request) {
|
||||
Object userAttribute = request.getSession().getAttribute("USER_ID");
|
||||
if (userAttribute != null) {
|
||||
return userAttribute.toString();
|
||||
}
|
||||
return "anonymous";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务器IP
|
||||
*/
|
||||
private String getServerIp() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
package com.tashow.cloud.app.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.tashow.cloud.app.model.BuriedPointFailRecord;
|
||||
import com.tashow.cloud.app.model.MqMessageRecord;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 埋点消息发送失败记录Mapper接口
|
||||
* 埋点消息发送记录Mapper接口
|
||||
*/
|
||||
@Mapper
|
||||
public interface BuriedPointFailRecordMapper extends BaseMapper<BuriedPointFailRecord> {
|
||||
}
|
||||
public interface BuriedPointFailRecordMapper extends BaseMapper<MqMessageRecord> {
|
||||
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
package com.tashow.cloud.app.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.tashow.cloud.app.mq.message.BuriedMessages;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 埋点数据实体类
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
@TableName(value = "app_burying")
|
||||
public class BuriedPoint {
|
||||
@@ -21,7 +22,6 @@ public class BuriedPoint {
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 事件唯一ID
|
||||
*/
|
||||
@@ -107,9 +107,22 @@ public class BuriedPoint {
|
||||
@TableField(value = "status")
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 重试次数
|
||||
*/
|
||||
@TableField(value = "retry_count")
|
||||
private Integer retryCount;
|
||||
}
|
||||
|
||||
public BuriedPoint(BuriedMessages message) {
|
||||
this.eventId = message.getId();
|
||||
this.eventTime = System.currentTimeMillis();
|
||||
this.userId = message.getUserId();
|
||||
this.eventType = message.getEventType();
|
||||
this.service = message.getService();
|
||||
this.method = message.getMethod();
|
||||
this.sessionId = message.getSessionId();
|
||||
this.clientIp = message.getClientIp();
|
||||
this.serverIp = message.getServerIp();
|
||||
this.status = message.getStatusCode();
|
||||
this.pagePath = message.getPagePath();
|
||||
this.elementId = message.getElementId();
|
||||
this.createTime = new Date();
|
||||
this.updateTime = new Date();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,24 +11,17 @@ import java.util.Date;
|
||||
* 埋点消息发送失败记录实体类
|
||||
*/
|
||||
@Data
|
||||
@TableName("buried_point_fail_record")
|
||||
public class BuriedPointFailRecord {
|
||||
|
||||
@TableName("mq_message_record")
|
||||
public class MqMessageRecord {
|
||||
/**
|
||||
* 状态常量定义
|
||||
*/
|
||||
public static final int STATUS_UNPROCESSED = 0; // 未处理
|
||||
public static final int STATUS_PROCESSING = 1; // 处理中
|
||||
public static final int STATUS_SUCCESS = 2; // 处理成功
|
||||
public static final int STATUS_FAILED = 3; // 处理失败
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 消息关联ID
|
||||
*/
|
||||
private String correlationId;
|
||||
public static final int STATUS_UNPROCESSED = 10; // 未处理
|
||||
public static final int STATUS_SUCCESS = 20; // 处理成功
|
||||
public static final int STATUS_FAILED = 30; // 发送失败
|
||||
@TableId
|
||||
private Integer id;
|
||||
|
||||
|
||||
/**
|
||||
* 交换机名称
|
||||
@@ -71,7 +71,7 @@ public class BuriedPointConfiguration implements WebMvcConfigurer {
|
||||
"/v3/api-docs/**",
|
||||
"/webjars/**",
|
||||
"/static/**",
|
||||
"/card1",
|
||||
"/card",
|
||||
"/error"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.tashow.cloud.app.mq.consumer.buriedPoint;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.tashow.cloud.app.mapper.BuriedPointMapper;
|
||||
import com.tashow.cloud.app.mapper.BuriedPointFailRecordMapper;
|
||||
import com.tashow.cloud.app.model.BuriedPoint;
|
||||
import com.tashow.cloud.app.model.BuriedPointFailRecord;
|
||||
import com.tashow.cloud.app.model.MqMessageRecord;
|
||||
import com.tashow.cloud.app.mq.message.BuriedMessages;
|
||||
import com.tashow.cloud.app.service.feishu.BuriedPointMonitorService;
|
||||
import com.rabbitmq.client.Channel;
|
||||
@@ -16,12 +16,8 @@ import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import com.tashow.cloud.common.util.json.JsonUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
|
||||
/**
|
||||
* 埋点消息消费者
|
||||
*/
|
||||
@@ -30,123 +26,39 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class BuriedPointConsumer extends AbstractRabbitMQConsumer<BuriedMessages> {
|
||||
|
||||
private final BuriedPointMapper buriedPointMapper;
|
||||
private final BuriedPointFailRecordMapper buriedPointFailRecordMapper;
|
||||
private final BuriedPointMonitorService buriedPointMonitorService;
|
||||
|
||||
@Value("${spring.application.name:tashow-app}")
|
||||
private String applicationName;
|
||||
|
||||
@Override
|
||||
public int getMaxRetryAllowed() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@RabbitHandler
|
||||
public void handleMessage(BuriedMessages message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
|
||||
onMessage(message, channel, deliveryTag);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理埋点消息
|
||||
* @param message 消息对象
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean processMessage(BuriedMessages message) {
|
||||
return saveToDatabase(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getRetryCount(BuriedMessages message) {
|
||||
try {
|
||||
BuriedPoint buriedPoint = buriedPointMapper.selectByEventId(message.getId());
|
||||
if (buriedPoint != null && buriedPoint.getRetryCount() != null) {
|
||||
if ((buriedPoint.getStatus() == BuriedMessages.STATUS_ERROR ||
|
||||
buriedPoint.getStatus() == BuriedMessages.STATUS_PROCESSING)) {
|
||||
return buriedPoint.getRetryCount() - 1;
|
||||
}
|
||||
return buriedPoint.getRetryCount();
|
||||
}
|
||||
|
||||
String correlationId = String.valueOf(message.getId());
|
||||
BuriedPointFailRecord failRecord = findFailRecord(correlationId);
|
||||
return failRecord != null ? failRecord.getRetryCount() : 0;
|
||||
} catch (Exception e) {
|
||||
log.warn("[埋点消费者] 获取重试次数失败", e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据关联ID查找失败记录
|
||||
*/
|
||||
private BuriedPointFailRecord findFailRecord(String correlationId) {
|
||||
LambdaQueryWrapper<BuriedPointFailRecord> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(BuriedPointFailRecord::getCorrelationId, correlationId);
|
||||
return buriedPointFailRecordMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateMessageStatus(BuriedMessages message) {
|
||||
try {
|
||||
BuriedPoint buriedPoint = buriedPointMapper.selectByEventId(message.getId());
|
||||
if (buriedPoint != null) {
|
||||
buriedPoint.setStatus(message.getStatusCode());
|
||||
buriedPoint.setUpdateTime(new Date());
|
||||
buriedPoint.setRetryCount(message.getRetryCount());
|
||||
buriedPointMapper.updateById(buriedPoint);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点消费者] 更新状态失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateRetryCount(BuriedMessages message) {
|
||||
try {
|
||||
BuriedPoint buriedPoint = buriedPointMapper.selectByEventId(message.getId());
|
||||
if (buriedPoint != null) {
|
||||
updateBuriedPointRetryCount(buriedPoint, message);
|
||||
return;
|
||||
}
|
||||
|
||||
String correlationId = String.valueOf(message.getId());
|
||||
BuriedPointFailRecord failRecord = findFailRecord(correlationId);
|
||||
if (failRecord != null) {
|
||||
updateFailRecordRetryCount(failRecord, message);
|
||||
} else {
|
||||
saveToFailRecord(message, "");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点消费者] 更新重试次数失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新埋点表中的重试次数
|
||||
*/
|
||||
private void updateBuriedPointRetryCount(BuriedPoint buriedPoint, BuriedMessages message) {
|
||||
buriedPoint.setRetryCount(message.getRetryCount());
|
||||
buriedPoint.setUpdateTime(new Date());
|
||||
buriedPointMapper.updateById(buriedPoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新失败记录表中的重试次数
|
||||
*/
|
||||
private void updateFailRecordRetryCount(BuriedPointFailRecord failRecord, BuriedMessages message) {
|
||||
failRecord.setRetryCount(message.getRetryCount());
|
||||
failRecord.setUpdateTime(new Date());
|
||||
failRecord.setMessageContent(JsonUtils.toJsonString(message));
|
||||
buriedPointFailRecordMapper.updateById(failRecord);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveToDatabase(BuriedMessages message) {
|
||||
try {
|
||||
BuriedPoint existingPoint = buriedPointMapper.selectByEventId(message.getId());
|
||||
if (existingPoint != null) {
|
||||
return updateExistingBuriedPoint(existingPoint, message);
|
||||
existingPoint.setStatus(message.getStatusCode());
|
||||
existingPoint.setUpdateTime(new Date());
|
||||
return buriedPointMapper.updateById(existingPoint) > 0;
|
||||
}
|
||||
BuriedPoint buriedPoint = new BuriedPoint(message);
|
||||
buriedPoint.setService(applicationName);
|
||||
buriedPointMapper.insert(buriedPoint);
|
||||
|
||||
return createNewBuriedPoint(message);
|
||||
if(buriedPoint.getStatus() == BuriedMessages.STATUS_ERROR){
|
||||
buriedPointMonitorService.checkFailRecordsAndAlert("埋点数据处理异常");
|
||||
}
|
||||
return true;
|
||||
} catch (DuplicateKeyException e) {
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
@@ -154,106 +66,4 @@ public class BuriedPointConsumer extends AbstractRabbitMQConsumer<BuriedMessages
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新已存在的埋点记录
|
||||
*/
|
||||
private boolean updateExistingBuriedPoint(BuriedPoint existingPoint, BuriedMessages message) {
|
||||
existingPoint.setStatus(message.getStatusCode());
|
||||
existingPoint.setUpdateTime(new Date());
|
||||
|
||||
if (message.getRetryCount() != null) {
|
||||
int newRetryCount = Math.max(existingPoint.getRetryCount(), message.getRetryCount());
|
||||
existingPoint.setRetryCount(newRetryCount);
|
||||
message.setRetryCount(newRetryCount);
|
||||
}
|
||||
|
||||
return buriedPointMapper.updateById(existingPoint) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的埋点记录
|
||||
*/
|
||||
private boolean createNewBuriedPoint(BuriedMessages message) {
|
||||
BuriedPoint buriedPoint = new BuriedPoint();
|
||||
buriedPoint.setEventId(message.getId());
|
||||
buriedPoint.setEventTime(System.currentTimeMillis());
|
||||
buriedPoint.setUserId(message.getUserId());
|
||||
buriedPoint.setEventType(message.getEventType());
|
||||
buriedPoint.setService(applicationName);
|
||||
buriedPoint.setMethod(message.getMethod());
|
||||
buriedPoint.setSessionId(message.getSessionId());
|
||||
buriedPoint.setClientIp(message.getClientIp());
|
||||
buriedPoint.setServerIp(message.getServerIp());
|
||||
buriedPoint.setStatus(message.getStatusCode());
|
||||
buriedPoint.setRetryCount(message.getRetryCount());
|
||||
buriedPoint.setPagePath(message.getPagePath());
|
||||
buriedPoint.setElementId(message.getElementId());
|
||||
buriedPoint.setDuration(message.getDuration());
|
||||
buriedPoint.setCreateTime(new Date());
|
||||
buriedPoint.setUpdateTime(new Date());
|
||||
|
||||
buriedPointMapper.insert(buriedPoint);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveToFailRecord(BuriedMessages message, String cause) {
|
||||
try {
|
||||
String correlationId = String.valueOf(message.getId());
|
||||
BuriedPointFailRecord existingRecord = findFailRecord(correlationId);
|
||||
|
||||
if (existingRecord != null) {
|
||||
updateExistingFailRecord(existingRecord, message, cause);
|
||||
} else {
|
||||
createNewFailRecord(message, cause);
|
||||
checkFailRecordsAndAlert();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点消费者] 保存失败记录失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新已存在的失败记录
|
||||
*/
|
||||
private void updateExistingFailRecord(BuriedPointFailRecord record, BuriedMessages message, String cause) {
|
||||
record.setExchange(BuriedMessages.EXCHANGE);
|
||||
record.setRoutingKey(BuriedMessages.ROUTING_KEY);
|
||||
record.setCause(message.getErrorMessage() + cause);
|
||||
record.setMessageContent(JsonUtils.toJsonString(message));
|
||||
record.setRetryCount(message.getRetryCount());
|
||||
record.setStatus(BuriedPointFailRecord.STATUS_UNPROCESSED);
|
||||
record.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.updateById(record);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的失败记录
|
||||
*/
|
||||
private void createNewFailRecord(BuriedMessages message, String cause) {
|
||||
String correlationId = String.valueOf(message.getId());
|
||||
BuriedPointFailRecord failRecord = new BuriedPointFailRecord();
|
||||
failRecord.setCorrelationId(correlationId);
|
||||
failRecord.setExchange(BuriedMessages.EXCHANGE);
|
||||
failRecord.setRoutingKey(BuriedMessages.ROUTING_KEY);
|
||||
failRecord.setCause(message.getErrorMessage() + cause);
|
||||
failRecord.setMessageContent(JsonUtils.toJsonString(message));
|
||||
failRecord.setRetryCount(message.getRetryCount());
|
||||
failRecord.setStatus(BuriedPointFailRecord.STATUS_UNPROCESSED);
|
||||
failRecord.setCreateTime(new Date());
|
||||
failRecord.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.insert(failRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查失败记录数量并发送告警
|
||||
*/
|
||||
private void checkFailRecordsAndAlert() {
|
||||
try {
|
||||
buriedPointMonitorService.checkFailRecordsAndAlert("埋点处理异常,请检查系统");
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点消费者] 检查失败记录异常", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,93 +1,100 @@
|
||||
package com.tashow.cloud.app.mq.handler;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.tashow.cloud.app.mapper.BuriedPointFailRecordMapper;
|
||||
import com.tashow.cloud.app.model.BuriedPointFailRecord;
|
||||
import com.tashow.cloud.app.model.MqMessageRecord;
|
||||
import com.tashow.cloud.app.service.feishu.BuriedPointMonitorService;
|
||||
import com.tashow.cloud.mq.handler.FailRecordHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 埋点失败记录处理器
|
||||
* MQ消息记录处理器
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class BuriedPointFailRecordHandler implements FailRecordHandler {
|
||||
|
||||
private final BuriedPointFailRecordMapper buriedPointFailRecordMapper;
|
||||
private final BuriedPointMonitorService buriedPointMonitorService;
|
||||
|
||||
/**
|
||||
* 保存消息发送失败记录
|
||||
* 保存消息记录=
|
||||
*/
|
||||
@Override
|
||||
public void saveFailRecord(String correlationId, String exchange, String routingKey, String cause, String messageContent) {
|
||||
public void saveMessageRecord(Integer id, String exchange, String routingKey, String cause, String messageContent, int status) {
|
||||
try {
|
||||
// 先查询是否已存在记录
|
||||
BuriedPointFailRecord existingRecord = findExistingRecord(correlationId);
|
||||
|
||||
MqMessageRecord existingRecord = findExistingRecord(id);
|
||||
if (existingRecord != null) {
|
||||
updateExistingRecord(existingRecord, exchange, routingKey, cause, messageContent);
|
||||
existingRecord.setRetryCount(existingRecord.getRetryCount() + 1);
|
||||
existingRecord.setMessageContent(messageContent);
|
||||
existingRecord.setStatus(status);
|
||||
existingRecord.setCause(cause);
|
||||
existingRecord.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.updateById(existingRecord);
|
||||
} else {
|
||||
createNewFailRecord(correlationId, exchange, routingKey, cause, messageContent);
|
||||
checkAlertThreshold(cause);
|
||||
MqMessageRecord record = new MqMessageRecord();
|
||||
record.setId(id);
|
||||
record.setExchange(exchange);
|
||||
record.setRoutingKey(routingKey);
|
||||
record.setCause(cause);
|
||||
record.setMessageContent(messageContent);
|
||||
record.setRetryCount(0);
|
||||
record.setStatus(status);
|
||||
record.setCreateTime(new Date());
|
||||
record.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.insert(record);
|
||||
if (status == MqMessageRecord.STATUS_FAILED) {
|
||||
buriedPointMonitorService.checkFailRecordsAndAlert(cause);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点处理器] 保存失败记录异常", e);
|
||||
log.error("[MQ消息处理器] 保存消息记录异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新消息状态
|
||||
*/
|
||||
@Override
|
||||
public void updateMessageStatus(Integer id) {
|
||||
try {
|
||||
MqMessageRecord record = findExistingRecord(id);
|
||||
if (record != null) {
|
||||
record.setStatus(MqMessageRecord.STATUS_SUCCESS);
|
||||
record.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.updateById(record);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[MQ消息处理器] 更新消息状态异常: {}", id, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新消息状态并设置失败原因
|
||||
*/
|
||||
@Override
|
||||
public void updateMessageStatusWithCause(Integer id, String cause) {
|
||||
try {
|
||||
MqMessageRecord record = findExistingRecord(id);
|
||||
if (record != null) {
|
||||
record.setStatus(MqMessageRecord.STATUS_FAILED);
|
||||
record.setCause(cause);
|
||||
record.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.updateById(record);
|
||||
buriedPointMonitorService.checkFailRecordsAndAlert(cause);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[MQ消息处理器] 更新消息状态和原因异常: {}", id, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找已存在的失败记录
|
||||
*/
|
||||
private BuriedPointFailRecord findExistingRecord(String correlationId) {
|
||||
LambdaQueryWrapper<BuriedPointFailRecord> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(BuriedPointFailRecord::getCorrelationId, correlationId);
|
||||
private MqMessageRecord findExistingRecord(Integer id) {
|
||||
LambdaQueryWrapper<MqMessageRecord> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(MqMessageRecord::getId, id);
|
||||
return buriedPointFailRecordMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新已存在的失败记录
|
||||
*/
|
||||
private void updateExistingRecord(BuriedPointFailRecord record, String exchange, String routingKey,
|
||||
String cause, String messageContent) {
|
||||
record.setExchange(exchange);
|
||||
record.setRoutingKey(routingKey);
|
||||
record.setCause(cause);
|
||||
record.setMessageContent(messageContent);
|
||||
record.setStatus(BuriedPointFailRecord.STATUS_UNPROCESSED);
|
||||
record.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.updateById(record);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新的失败记录
|
||||
*/
|
||||
private void createNewFailRecord(String correlationId, String exchange, String routingKey,
|
||||
String cause, String messageContent) {
|
||||
BuriedPointFailRecord failRecord = new BuriedPointFailRecord();
|
||||
failRecord.setCorrelationId(correlationId);
|
||||
failRecord.setExchange(exchange);
|
||||
failRecord.setRoutingKey(routingKey);
|
||||
failRecord.setCause(cause);
|
||||
failRecord.setMessageContent(messageContent);
|
||||
failRecord.setRetryCount(0);
|
||||
failRecord.setStatus(BuriedPointFailRecord.STATUS_UNPROCESSED);
|
||||
failRecord.setCreateTime(new Date());
|
||||
failRecord.setUpdateTime(new Date());
|
||||
buriedPointFailRecordMapper.insert(failRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否达到告警阈值
|
||||
*/
|
||||
@Override
|
||||
public boolean checkAlertThreshold(String cause) {
|
||||
return buriedPointMonitorService.checkFailRecordsAndAlert(cause);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,27 @@
|
||||
package com.tashow.cloud.app.mq.message;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.tashow.cloud.common.util.json.JsonUtils;
|
||||
import com.tashow.cloud.common.util.servlet.ServletUtils;
|
||||
import com.tashow.cloud.common.util.spring.SpringUtils;
|
||||
import com.tashow.cloud.mq.core.BaseMqMessage;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.Data;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Date;
|
||||
|
||||
import static com.tashow.cloud.web.apilog.core.interceptor.ApiAccessLogInterceptor.ATTRIBUTE_HANDLER_METHOD;
|
||||
|
||||
/**
|
||||
* 埋点消息
|
||||
*/
|
||||
@Data
|
||||
public class BuriedMessages extends BaseMqMessage {
|
||||
private static final String ATTRIBUTE_REQUEST_ID = "BuriedPoint.RequestId";
|
||||
|
||||
|
||||
/**
|
||||
* 交换机名称
|
||||
@@ -85,15 +97,46 @@ public class BuriedMessages extends BaseMqMessage {
|
||||
* 元素ID
|
||||
*/
|
||||
private String elementId;
|
||||
|
||||
/**
|
||||
* 持续时间
|
||||
*/
|
||||
private Long duration;
|
||||
|
||||
/**
|
||||
* 服务名称
|
||||
*/
|
||||
private String service;
|
||||
private String service;
|
||||
|
||||
public BuriedMessages() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从请求创建埋点消息
|
||||
*
|
||||
* @param request HTTP请求
|
||||
* @param handlerMethod 处理方法
|
||||
*/
|
||||
public BuriedMessages(HttpServletRequest request, HandlerMethod handlerMethod) {
|
||||
try {
|
||||
int requestId = (int)(Math.abs(IdUtil.getSnowflakeNextId()) % Integer.MAX_VALUE);
|
||||
this.setId(requestId);
|
||||
this.eventTime = new Date();
|
||||
this.service = SpringUtils.getApplicationName();
|
||||
this.method = request.getMethod() + " " + request.getRequestURI() +
|
||||
JsonUtils.toJsonString(request.getParameterMap());
|
||||
Object userId = request.getSession().getAttribute("USER_ID");
|
||||
this.userId = userId != null ? userId.toString() : "anonymous";
|
||||
this.sessionId = request.getSession().getId();
|
||||
this.clientIp = ServletUtils.getClientIP(request);
|
||||
try {
|
||||
this.serverIp = InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
this.serverIp = "unknown";
|
||||
}
|
||||
String controllerName = handlerMethod.getBeanType().getSimpleName();
|
||||
String actionName = handlerMethod.getMethod().getName();
|
||||
this.pagePath = controllerName + "#" + actionName;
|
||||
this.eventType = "API_REQUEST_START";
|
||||
this.setStatusCode(STATUS_PROCESSING);
|
||||
request.setAttribute(ATTRIBUTE_REQUEST_ID, this.getId());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("创建埋点消息失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.tashow.cloud.app.mq.producer.buriedPoint;
|
||||
import com.tashow.cloud.app.mq.message.BuriedMessages;
|
||||
import com.tashow.cloud.common.util.json.JsonUtils;
|
||||
import com.tashow.cloud.mq.rabbitmq.producer.AbstractRabbitMQProducer;
|
||||
import org.springframework.amqp.core.ReturnedMessage;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@@ -11,10 +12,14 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class BuriedPointProducer extends AbstractRabbitMQProducer<BuriedMessages> {
|
||||
|
||||
@Override
|
||||
public void returnedMessage(ReturnedMessage returned) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExchange() {
|
||||
return "BuriedMessages.EXCHANGE";
|
||||
return BuriedMessages.EXCHANGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -22,8 +27,5 @@ public class BuriedPointProducer extends AbstractRabbitMQProducer<BuriedMessages
|
||||
return BuriedMessages.ROUTING_KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String convertMessageToString(BuriedMessages message) {
|
||||
return JsonUtils.toJsonString(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,19 @@
|
||||
package com.tashow.cloud.app.service.feishu;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.tashow.cloud.app.mapper.BuriedPointFailRecordMapper;
|
||||
import com.tashow.cloud.app.model.BuriedPointFailRecord;
|
||||
import com.tashow.cloud.app.mapper.BuriedPointMapper;
|
||||
import com.tashow.cloud.app.model.BuriedPoint;
|
||||
import com.tashow.cloud.app.model.MqMessageRecord;
|
||||
import com.tashow.cloud.app.mq.message.BuriedMessages;
|
||||
import com.tashow.cloud.sdk.feishu.client.FeiShuAlertClient;
|
||||
import com.tashow.cloud.sdk.feishu.config.LarkConfig;
|
||||
import com.tashow.cloud.sdk.feishu.util.ChartImageGenerator;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 埋点监控服务
|
||||
@@ -20,11 +22,12 @@ import java.util.*;
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BuriedPointMonitorService {
|
||||
|
||||
private static final int ALERT_THRESHOLD = 3;
|
||||
private static final int MONITORING_HOURS = 12;
|
||||
private final Map<String, Long> alertCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final BuriedPointFailRecordMapper buriedPointFailRecordMapper;
|
||||
private final BuriedPointMapper buriedPointMapper;
|
||||
private final FeiShuAlertClient feiShuAlertClient;
|
||||
private final FeiShuCardDataService feiShuCardDataService;
|
||||
private final LarkConfig larkConfig;
|
||||
@@ -36,122 +39,194 @@ public class BuriedPointMonitorService {
|
||||
try {
|
||||
Date now = new Date();
|
||||
Date hoursAgo = getDateHoursAgo(now, MONITORING_HOURS);
|
||||
|
||||
Long failCount = getUnprocessedFailCount(hoursAgo, now);
|
||||
|
||||
if (failCount > ALERT_THRESHOLD) {
|
||||
sendAlertMessage(failCount.intValue(), hoursAgo, now, cause);
|
||||
return true;
|
||||
boolean sentAlert = false;
|
||||
List<Date[]> timeRanges = getHourRanges(hoursAgo, now);
|
||||
long mqFailCount = countFailures(buriedPointFailRecordMapper, MqMessageRecord.class, hoursAgo, now);
|
||||
long buriedFailCount = countFailures(buriedPointMapper, BuriedPoint.class, hoursAgo, now);
|
||||
if (mqFailCount > ALERT_THRESHOLD||buriedFailCount > ALERT_THRESHOLD) {
|
||||
if (!hasRecentlySentAlert(cause)) {
|
||||
sendAlert(mqFailCount, cause, getMqStats(timeRanges));
|
||||
alertCache.put(cause, System.currentTimeMillis());
|
||||
sentAlert = true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
return sentAlert;
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点监控] 检查失败记录异常", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否最近已发送过相同类型的告警
|
||||
*/
|
||||
private boolean hasRecentlySentAlert(String alertType) {
|
||||
Long lastSentTime = alertCache.get(alertType);
|
||||
if (lastSentTime == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
long hourInMillis = MONITORING_HOURS * 60 * 60 * 1000L;
|
||||
return (System.currentTimeMillis() - lastSentTime) < hourInMillis;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取未处理的失败记录数量
|
||||
* 获取消息队列统计数据
|
||||
*/
|
||||
public Long getUnprocessedFailCount(Date startDate, Date endDate) {
|
||||
LambdaQueryWrapper<BuriedPointFailRecord> query = new LambdaQueryWrapper<>();
|
||||
query.ge(BuriedPointFailRecord::getCreateTime, startDate)
|
||||
.le(BuriedPointFailRecord::getCreateTime, endDate)
|
||||
.eq(BuriedPointFailRecord::getStatus, BuriedPointFailRecord.STATUS_UNPROCESSED);
|
||||
return buriedPointFailRecordMapper.selectCount(query);
|
||||
private List<ChartImageGenerator.MonitoringDataPoint> getMqStats(List<Date[]> timeRanges) {
|
||||
Map<Date, Integer> successData = batchQueryMqStatus(timeRanges, MqMessageRecord.STATUS_SUCCESS);
|
||||
Map<Date, Integer> failedData = batchQueryMqFailures(timeRanges);
|
||||
|
||||
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:00");
|
||||
return timeRanges.stream()
|
||||
.map(range -> new ChartImageGenerator.MonitoringDataPoint(
|
||||
timeFormat.format(range[0]),
|
||||
successData.getOrDefault(range[0], 0),
|
||||
failedData.getOrDefault(range[0], 0)
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取埋点表统计数据
|
||||
*/
|
||||
private List<ChartImageGenerator.MonitoringDataPoint> getBuriedStats(List<Date[]> timeRanges) {
|
||||
// 批量查询每个时间区间的数据
|
||||
Map<Date, Integer> successData = batchQueryBuriedStatus(timeRanges, BuriedMessages.STATUS_SUCCESS);
|
||||
Map<Date, Integer> failedData = batchQueryBuriedStatus(timeRanges, BuriedMessages.STATUS_ERROR);
|
||||
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:00");
|
||||
return timeRanges.stream()
|
||||
.map(range -> new ChartImageGenerator.MonitoringDataPoint(
|
||||
timeFormat.format(range[0]),
|
||||
successData.getOrDefault(range[0], 0),
|
||||
failedData.getOrDefault(range[0], 0)
|
||||
))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询MQ状态数据
|
||||
*/
|
||||
private Map<Date, Integer> batchQueryMqStatus(List<Date[]> timeRanges, int status) {
|
||||
Map<Date, Integer> result = new HashMap<>();
|
||||
for (Date[] range : timeRanges) {
|
||||
LambdaQueryWrapper<MqMessageRecord> query = new LambdaQueryWrapper<>();
|
||||
query.ge(MqMessageRecord::getCreateTime, range[0])
|
||||
.lt(MqMessageRecord::getCreateTime, range[1])
|
||||
.eq(MqMessageRecord::getStatus, status);
|
||||
result.put(range[0], buriedPointFailRecordMapper.selectCount(query).intValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询MQ失败数据
|
||||
*/
|
||||
private Map<Date, Integer> batchQueryMqFailures(List<Date[]> timeRanges) {
|
||||
Map<Date, Integer> result = new HashMap<>();
|
||||
for (Date[] range : timeRanges) {
|
||||
LambdaQueryWrapper<MqMessageRecord> query = new LambdaQueryWrapper<>();
|
||||
query.ge(MqMessageRecord::getCreateTime, range[0])
|
||||
.lt(MqMessageRecord::getCreateTime, range[1])
|
||||
.in(MqMessageRecord::getStatus, Arrays.asList(
|
||||
MqMessageRecord.STATUS_UNPROCESSED, MqMessageRecord.STATUS_FAILED));
|
||||
result.put(range[0], buriedPointFailRecordMapper.selectCount(query).intValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询埋点状态数据
|
||||
*/
|
||||
private Map<Date, Integer> batchQueryBuriedStatus(List<Date[]> timeRanges, int status) {
|
||||
Map<Date, Integer> result = new HashMap<>();
|
||||
for (Date[] range : timeRanges) {
|
||||
LambdaQueryWrapper<BuriedPoint> query = new LambdaQueryWrapper<>();
|
||||
query.ge(BuriedPoint::getCreateTime, range[0])
|
||||
.lt(BuriedPoint::getCreateTime, range[1])
|
||||
.eq(BuriedPoint::getStatus, status);
|
||||
result.put(range[0], buriedPointMapper.selectCount(query).intValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送告警消息
|
||||
* 计算失败数量
|
||||
*/
|
||||
private void sendAlertMessage(int failCount, Date startDate, Date endDate, String errorMessage) {
|
||||
private <T> long countFailures(Object mapper, Class<T> entityClass, Date startDate, Date endDate) {
|
||||
try {
|
||||
List<ChartImageGenerator.MonitoringDataPoint> monitoringData =
|
||||
queryHourlyFailRecordData(startDate, endDate);
|
||||
if (entityClass == BuriedPoint.class) {
|
||||
LambdaQueryWrapper<BuriedPoint> query = new LambdaQueryWrapper<>();
|
||||
query.ge(BuriedPoint::getCreateTime, startDate)
|
||||
.le(BuriedPoint::getCreateTime, endDate)
|
||||
.eq(BuriedPoint::getStatus, BuriedMessages.STATUS_ERROR);
|
||||
return ((BuriedPointMapper)mapper).selectCount(query);
|
||||
} else {
|
||||
LambdaQueryWrapper<MqMessageRecord> query = new LambdaQueryWrapper<>();
|
||||
query.ge(MqMessageRecord::getCreateTime, startDate)
|
||||
.le(MqMessageRecord::getCreateTime, endDate)
|
||||
.eq(MqMessageRecord::getStatus, MqMessageRecord.STATUS_FAILED);
|
||||
return ((BuriedPointFailRecordMapper)mapper).selectCount(query);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送告警
|
||||
*/
|
||||
private void sendAlert(long failCount, String alertMsg, List<ChartImageGenerator.MonitoringDataPoint> data) {
|
||||
try {
|
||||
String imageKey = feiShuAlertClient.uploadImage(data, alertMsg);
|
||||
String title = alertMsg.split(":")[0].trim();
|
||||
|
||||
HashMap<String, Object> templateData = new HashMap<>();
|
||||
String chatId = larkConfig.getChatId();
|
||||
|
||||
String imageKey = feiShuAlertClient.uploadImage(monitoringData, errorMessage);
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
templateData.put("alert_title", "埋点数据异常告警");
|
||||
templateData.put("image_key", imageKey);
|
||||
templateData.put("current_time", sdf.format(new Date()));
|
||||
templateData.put("fail_count", failCount);
|
||||
Map<String, Object> templateData = Map.of(
|
||||
"alert_title", title,
|
||||
"image_key", Map.of("img_key", imageKey),
|
||||
"current_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),
|
||||
"fail_count", failCount
|
||||
);
|
||||
|
||||
String messageId = feiShuAlertClient.sendCardMessage(
|
||||
chatId,
|
||||
larkConfig.getExceptionCards(),
|
||||
templateData
|
||||
larkConfig.getChatId(),
|
||||
larkConfig.getExceptionCards(),
|
||||
new HashMap<>(templateData)
|
||||
);
|
||||
feiShuCardDataService.saveCardData(messageId, templateData);
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点监控] 发送告警失败", e);
|
||||
log.error("[埋点监控] 发送告警失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询按小时统计的失败记录数据
|
||||
* 获取小时范围列表
|
||||
*/
|
||||
public List<ChartImageGenerator.MonitoringDataPoint> queryHourlyFailRecordData(Date startDate, Date endDate) {
|
||||
List<ChartImageGenerator.MonitoringDataPoint> result = new ArrayList<>();
|
||||
|
||||
try {
|
||||
Date limitedStartDate = getDateHoursAgo(endDate, MONITORING_HOURS);
|
||||
Date actualStartDate = startDate.after(limitedStartDate) ? startDate : limitedStartDate;
|
||||
private List<Date[]> getHourRanges(Date startDate, Date endDate) {
|
||||
List<Date[]> ranges = new ArrayList<>();
|
||||
Calendar cal = Calendar.getInstance();
|
||||
|
||||
cal.setTime(endDate);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
Date endHour = cal.getTime();
|
||||
|
||||
cal.add(Calendar.HOUR_OF_DAY, -(MONITORING_HOURS - 1));
|
||||
Date startHour = startDate.after(cal.getTime()) ? startDate : cal.getTime();
|
||||
|
||||
cal.setTime(startHour);
|
||||
while (!cal.getTime().after(endHour)) {
|
||||
Date hourStart = cal.getTime();
|
||||
cal.add(Calendar.HOUR_OF_DAY, 1);
|
||||
Date hourEnd = cal.getTime().after(endDate) ? endDate : cal.getTime();
|
||||
ranges.add(new Date[]{hourStart, hourEnd});
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("HH:00");
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
||||
long hoursDiff = (endDate.getTime() - actualStartDate.getTime()) / (60 * 60 * 1000) + 1;
|
||||
int hours = (int) Math.min(hoursDiff, MONITORING_HOURS);
|
||||
|
||||
for (int i = 0; i < hours; i++) {
|
||||
calendar.setTime(actualStartDate);
|
||||
calendar.add(Calendar.HOUR_OF_DAY, i);
|
||||
Date currentHourStart = calendar.getTime();
|
||||
calendar.add(Calendar.HOUR_OF_DAY, 1);
|
||||
Date nextHourStart = calendar.getTime();
|
||||
|
||||
int successCount = getHourlyRecordCount(currentHourStart, nextHourStart, BuriedPointFailRecord.STATUS_SUCCESS);
|
||||
int failCount = getHourlyFailedCount(currentHourStart, nextHourStart);
|
||||
|
||||
result.add(new ChartImageGenerator.MonitoringDataPoint(
|
||||
sdf.format(currentHourStart),
|
||||
successCount,
|
||||
failCount
|
||||
));
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点监控] 查询小时数据失败", e);
|
||||
return Collections.emptyList();
|
||||
if (hourEnd.equals(endDate)) break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定状态的记录数量
|
||||
*/
|
||||
private int getHourlyRecordCount(Date startHour, Date endHour, int status) {
|
||||
LambdaQueryWrapper<BuriedPointFailRecord> query = new LambdaQueryWrapper<>();
|
||||
query.ge(BuriedPointFailRecord::getCreateTime, startHour)
|
||||
.lt(BuriedPointFailRecord::getCreateTime, endHour)
|
||||
.eq(BuriedPointFailRecord::getStatus, status);
|
||||
return buriedPointFailRecordMapper.selectCount(query).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取失败记录数量
|
||||
*/
|
||||
private int getHourlyFailedCount(Date startHour, Date endHour) {
|
||||
LambdaQueryWrapper<BuriedPointFailRecord> query = new LambdaQueryWrapper<>();
|
||||
query.ge(BuriedPointFailRecord::getCreateTime, startHour)
|
||||
.lt(BuriedPointFailRecord::getCreateTime, endHour)
|
||||
.in(BuriedPointFailRecord::getStatus,
|
||||
Arrays.asList(BuriedPointFailRecord.STATUS_UNPROCESSED, BuriedPointFailRecord.STATUS_FAILED));
|
||||
return buriedPointFailRecordMapper.selectCount(query).intValue();
|
||||
return ranges;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.tashow.cloud.app.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.tashow.cloud.app.mapper.BuriedPointFailRecordMapper;
|
||||
import com.tashow.cloud.app.model.BuriedPointFailRecord;
|
||||
import com.tashow.cloud.app.model.MqMessageRecord;
|
||||
import com.tashow.cloud.app.mq.message.BuriedMessages;
|
||||
import com.tashow.cloud.app.mq.producer.buriedPoint.BuriedPointProducer;
|
||||
import com.tashow.cloud.common.util.json.JsonUtils;
|
||||
@@ -20,7 +20,7 @@ import java.util.List;
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BuriedPointFailRecordService implements MessageRetryService<BuriedPointFailRecord> {
|
||||
public class BuriedPointFailRecordService implements MessageRetryService<MqMessageRecord> {
|
||||
|
||||
private final BuriedPointFailRecordMapper buriedPointFailRecordMapper;
|
||||
private final BuriedPointProducer buriedPointProducer;
|
||||
@@ -29,10 +29,10 @@ public class BuriedPointFailRecordService implements MessageRetryService<BuriedP
|
||||
* 获取未处理的失败记录
|
||||
*/
|
||||
@Override
|
||||
public List<BuriedPointFailRecord> getUnprocessedRecords() {
|
||||
LambdaQueryWrapper<BuriedPointFailRecord> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(BuriedPointFailRecord::getStatus, BuriedPointFailRecord.STATUS_UNPROCESSED)
|
||||
.orderByAsc(BuriedPointFailRecord::getCreateTime);
|
||||
public List<MqMessageRecord> getUnprocessedRecords() {
|
||||
LambdaQueryWrapper<MqMessageRecord> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(MqMessageRecord::getStatus, MqMessageRecord.STATUS_FAILED)
|
||||
.orderByAsc(MqMessageRecord::getCreateTime);
|
||||
return buriedPointFailRecordMapper.selectList(queryWrapper);
|
||||
}
|
||||
|
||||
@@ -40,39 +40,14 @@ public class BuriedPointFailRecordService implements MessageRetryService<BuriedP
|
||||
* 重试失败消息
|
||||
*/
|
||||
@Override
|
||||
public boolean retryFailedMessage(String recordId) {
|
||||
public void retryFailedMessage(Integer recordId) {
|
||||
try {
|
||||
Long id = Long.valueOf(recordId);
|
||||
BuriedPointFailRecord record = buriedPointFailRecordMapper.selectById(id);
|
||||
|
||||
|
||||
MqMessageRecord record = buriedPointFailRecordMapper.selectById(id);
|
||||
BuriedMessages message = JsonUtils.parseObject(record.getMessageContent(), BuriedMessages.class);
|
||||
if (message == null) {
|
||||
updateStatus(record, BuriedPointFailRecord.STATUS_FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
buriedPointProducer.asyncSendMessage(message, record.getCorrelationId());
|
||||
updateStatus(record, BuriedPointFailRecord.STATUS_SUCCESS);
|
||||
return true;
|
||||
buriedPointProducer.asyncSendMessage(message);
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点重试] 重试失败", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新记录状态
|
||||
*/
|
||||
@Override
|
||||
public boolean updateStatus(BuriedPointFailRecord record, int status) {
|
||||
try {
|
||||
record.setStatus(status);
|
||||
record.setUpdateTime(new Date());
|
||||
return buriedPointFailRecordMapper.updateById(record) > 0;
|
||||
} catch (Exception e) {
|
||||
log.error("[埋点重试] 更新状态失败", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.tashow.cloud.app.task;
|
||||
|
||||
import com.tashow.cloud.app.model.BuriedPointFailRecord;
|
||||
import com.tashow.cloud.app.model.MqMessageRecord;
|
||||
import com.tashow.cloud.app.service.impl.BuriedPointFailRecordService;
|
||||
import com.tashow.cloud.mq.retry.AbstractMessageRetryTask;
|
||||
import com.tashow.cloud.mq.retry.MessageRetryService;
|
||||
@@ -15,7 +14,7 @@ import org.springframework.stereotype.Component;
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class BuriedPointRetryTask extends AbstractMessageRetryTask<BuriedPointFailRecord> {
|
||||
public class BuriedPointRetryTask extends AbstractMessageRetryTask<MqMessageRecord> {
|
||||
|
||||
private final BuriedPointFailRecordService buriedPointFailRecordService;
|
||||
|
||||
@@ -24,22 +23,19 @@ public class BuriedPointRetryTask extends AbstractMessageRetryTask<BuriedPointFa
|
||||
* 每天凌晨执行一次
|
||||
*/
|
||||
@Scheduled(cron = "0 0 0 * * ?")
|
||||
// @Scheduled(cron = "0/10 * * * * ?")
|
||||
public void execute() {
|
||||
retryFailedMessages();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MessageRetryService<BuriedPointFailRecord> getMessageRetryService() {
|
||||
protected MessageRetryService<MqMessageRecord> getMessageRetryService() {
|
||||
return buriedPointFailRecordService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRecordId(BuriedPointFailRecord record) {
|
||||
return String.valueOf(record.getId());
|
||||
protected Integer getRecordId(MqMessageRecord record) {
|
||||
return record.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getCorrelationId(BuriedPointFailRecord record) {
|
||||
return record.getCorrelationId();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user