Commit 502c0997 authored by zhouwei's avatar zhouwei

调整告警时间

parent 95f38259
...@@ -33,8 +33,9 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F ...@@ -33,8 +33,9 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
private final WebClient.Builder webClientBuilder; private final WebClient.Builder webClientBuilder;
private final TokenRouteMappingService tokenRouteMappingService; private final TokenRouteMappingService tokenRouteMappingService;
// 添加错误统计 // 添加错误统计
private static final int WINDOW_SIZE_SECONDS = 10*60; // 1分钟窗口 private static final int WINDOW_SIZE_SECONDS = 100; // 1分钟窗口
private static final double ERROR_THRESHOLD = 0.5; // 75%错误率阈值 private static final double ERROR_THRESHOLD = 0.5; // 75%错误率阈值
private static Long lastAlarmTime = System.currentTimeMillis();
@Autowired @Autowired
AlertService alertService; AlertService alertService;
private final Map<String, CircularQueue> errorStatsMap = new ConcurrentHashMap<>(); private final Map<String, CircularQueue> errorStatsMap = new ConcurrentHashMap<>();
...@@ -113,7 +114,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F ...@@ -113,7 +114,7 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
Long lastAlarmTime = alarmSentMap.get(primaryUrl); Long lastAlarmTime = alarmSentMap.get(primaryUrl);
// 如果从未发送过告警,或者距离上次告警已经超过冷却时间 // 如果从未发送过告警,或者距离上次告警已经超过冷却时间
if (lastAlarmTime == null || (currentTime - lastAlarmTime) > WINDOW_SIZE_SECONDS*1000) { if (lastAlarmTime == null || (currentTime - lastAlarmTime) > 20*60*1000) {
String alarmMessage = String.format( String alarmMessage = String.format(
"Service degradation detected: %s has high error rate (%.2f%%), switching to fallback service", "Service degradation detected: %s has high error rate (%.2f%%), switching to fallback service",
primaryUrl, errorRate * 100); primaryUrl, errorRate * 100);
...@@ -234,9 +235,8 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F ...@@ -234,9 +235,8 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
ServerHttpRequest request = exchange.getRequest(); ServerHttpRequest request = exchange.getRequest();
String fullUrl = baseUrl + (baseUrl.endsWith("/") ? uri.substring(1) : uri); String fullUrl = baseUrl + (baseUrl.endsWith("/") ? uri.substring(1) : uri);
log.info("Trying request to: {}", fullUrl); log.info("Starting {} request to: {}", isSourceRequest ? "source" : "target", fullUrl);
log.info("Using cached request body: {}", cachedBody); long startTime = System.currentTimeMillis();
WebClient.RequestBodySpec requestBodySpec = webClientBuilder.build() WebClient.RequestBodySpec requestBodySpec = webClientBuilder.build()
.method(request.getMethod()) .method(request.getMethod())
.uri(fullUrl + (request.getQueryParams().isEmpty() ? "" : .uri(fullUrl + (request.getQueryParams().isEmpty() ? "" :
...@@ -261,7 +261,16 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F ...@@ -261,7 +261,16 @@ public class FailoverGatewayFilterFactory extends AbstractGatewayFilterFactory<F
return requestBodySpec return requestBodySpec
.exchange() .exchange()
.flatMap(clientResponse -> handleResponse(exchange, clientResponse, isSourceRequest, errorStats)); .flatMap(clientResponse -> handleResponse(exchange, clientResponse, isSourceRequest, errorStats))
.doFinally(signalType -> {
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
log.info("{} request completed: {} - took {}ms, signal: {}",
isSourceRequest ? "Source" : "Target",
fullUrl,
duration,
signalType);
});
} }
private Mono<Void> handleResponse(ServerWebExchange exchange, private Mono<Void> handleResponse(ServerWebExchange exchange,
......
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