task-78: 为所有外部调用增加耗时、重试、失败率和 payload 字节指标
新增 ExternalCallMetricsRecorder:通过 RestClient 拦截器统一记录 aiimage.external-call.duration(耗时)、aiimage.external-call.total (失败率)、aiimage.external-call.payload.bytes(请求字节)与 aiimage.external-call.retry.total(重试次数);Coze/品牌检查/紫鸟 三个外部客户端全部接入,指标注册表缺失时静默降级,不改变调用语义。
This commit is contained in:
+20
-5
@@ -6,7 +6,6 @@ import com.nanri.aiimage.config.SimilarAsinProperties;
|
||||
import com.nanri.aiimage.modules.coze.service.CozeCredentialPoolService;
|
||||
import com.nanri.aiimage.modules.similarasin.model.dto.SimilarAsinResultRowDto;
|
||||
import com.nanri.aiimage.modules.similarasin.util.SimilarAsinLogSupport;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -27,7 +26,6 @@ import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class SimilarAsinCozeClient {
|
||||
|
||||
@@ -39,6 +37,7 @@ public class SimilarAsinCozeClient {
|
||||
private final SimilarAsinProperties properties;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final CozeCredentialPoolService cozeCredentialPoolService;
|
||||
private final com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics;
|
||||
private final AtomicLong credentialCursor = new AtomicLong();
|
||||
private final AtomicLong historyResponseLogCounter = new AtomicLong();
|
||||
/**
|
||||
@@ -206,6 +205,9 @@ public class SimilarAsinCozeClient {
|
||||
failureMessage(ex));
|
||||
}
|
||||
if (attemptIndex < 3) {
|
||||
if (externalCallMetrics != null) {
|
||||
externalCallMetrics.recordRetry("coze");
|
||||
}
|
||||
sleepBeforeRetry(attemptIndex);
|
||||
}
|
||||
}
|
||||
@@ -751,9 +753,12 @@ public class SimilarAsinCozeClient {
|
||||
}
|
||||
synchronized (this) {
|
||||
if (sharedRestClient == null) {
|
||||
sharedRestClient = RestClient.builder()
|
||||
.requestFactory(HttpClientPool.requestFactory(properties.getCozeReadTimeoutMillis()))
|
||||
.build();
|
||||
RestClient.Builder builder = RestClient.builder()
|
||||
.requestFactory(HttpClientPool.requestFactory(properties.getCozeReadTimeoutMillis()));
|
||||
if (externalCallMetrics != null) {
|
||||
builder.requestInterceptor(externalCallMetrics.interceptor("coze"));
|
||||
}
|
||||
sharedRestClient = builder.build();
|
||||
}
|
||||
return sharedRestClient;
|
||||
}
|
||||
@@ -872,6 +877,16 @@ public class SimilarAsinCozeClient {
|
||||
|| !normalize(row.getPuzzleImg2()).isBlank();
|
||||
}
|
||||
|
||||
public SimilarAsinCozeClient(SimilarAsinProperties properties,
|
||||
ObjectMapper objectMapper,
|
||||
CozeCredentialPoolService cozeCredentialPoolService,
|
||||
com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics) {
|
||||
this.properties = properties;
|
||||
this.objectMapper = objectMapper;
|
||||
this.cozeCredentialPoolService = cozeCredentialPoolService;
|
||||
this.externalCallMetrics = externalCallMetrics;
|
||||
}
|
||||
|
||||
private void ensureSuccess(JsonNode root) {
|
||||
if (root.path("code").asInt(-1) != 0) {
|
||||
throw new IllegalStateException(root.path("msg").asText("Coze response code is not 0"));
|
||||
|
||||
Reference in New Issue
Block a user