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:
2026-08-30 22:01:02 +08:00
parent f1d1fe5c78
commit 4c70ca4101
10 changed files with 569 additions and 33 deletions
@@ -87,7 +87,7 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_default_path() throws Exception {
// 默认路径:品牌检查客户端通过共享池创建单例 RestClient,
// 工厂为带 keep-alive 连接池的 JdkClientHttpRequestFactory。
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties());
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckBatchResult result = client.checkTitleText(" ");
assertTrue(result.brands().isEmpty(), "空标题安全跳过,不创建无效资源");
@@ -98,8 +98,8 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_multiple_items() throws Exception {
// 批量场景:Coze/品牌/紫鸟三个客户端各自持有独立 RestClient,
// 但底层连接池共用同一 HttpClient 实例,不重复创建。
SimilarAsinCozeClient coze = new SimilarAsinCozeClient(new SimilarAsinProperties(), new ObjectMapper(), null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties());
SimilarAsinCozeClient coze = new SimilarAsinCozeClient(new SimilarAsinProperties(), new ObjectMapper(), null, null);
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
HttpClient cozeClient = clientOf(factoryOf(restClientOf(coze)));
@@ -115,7 +115,7 @@ class HttpClientConnectionReuseTest {
void test_task_077_brand_normal_repeated_operation_is_idempotent() throws Exception {
// 幂等:同一客户端重复触发请求创建逻辑只产生一个 RestClient,
// 重复调用返回同一实例,不重复创建客户端对象。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties());
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
assertSame(restClientOf(brand), restClientOf(brand), "品牌客户端复用同一 RestClient");
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
@@ -125,7 +125,7 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_empty_input() throws Exception {
// 空输入:空品牌列表不发起任何 HTTP 请求、不创建客户端资源。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties());
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms");
assertTrue(result.brands().isEmpty());
@@ -136,7 +136,7 @@ class HttpClientConnectionReuseTest {
@Test
void test_task_077_brand_boundary_single_item() throws Exception {
// 单元素:单客户端单请求走共享池,工厂带连接池,行为与批量一致。
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties());
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
assertPooled(factoryOf(restClientOf(brand)));
}
@@ -146,7 +146,7 @@ class HttpClientConnectionReuseTest {
// 不随实例数量线性增长连接资源。
int instances = 8;
for (int i = 0; i < instances; i++) {
restClientOf(new BrandCheckClient(new BrandCheckProperties()));
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null));
restClientOf(new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()));
}
assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(),