diff --git a/backend/crates/gateway-admin/src/model/observability.rs b/backend/crates/gateway-admin/src/model/observability.rs index 882a183a3..9c90850e0 100644 --- a/backend/crates/gateway-admin/src/model/observability.rs +++ b/backend/crates/gateway-admin/src/model/observability.rs @@ -889,12 +889,14 @@ impl UsageErrorOrigin { } /// 使用记录表格的窄读模型 -#[derive(Debug, Clone, PartialEq)] +#[derive(Clone, PartialEq)] pub struct UsageListRecord { pub platform_icon: Option, pub outcome: RequestOutcome, /// 已保存的上游错误对象含非空字符串 code、type 或 message pub has_upstream_error: bool, + pub upstream_error_message: Option, + pub retry_after_ms: Option, pub error_code: Option, pub error_message: Option, pub error_kind: Option, @@ -961,6 +963,17 @@ pub struct UsageListRecord { pub started_at: DateTime, } +// 上游错误原文只交付给管理端,避免整行调试输出带出正文 +impl std::fmt::Debug for UsageListRecord { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter + .debug_struct("UsageListRecord") + .field("id", &self.id) + .field("outcome", &self.outcome) + .finish_non_exhaustive() + } +} + /// 受控请求详情中的上游错误字段,不参与错误分类或普通日志输出 #[derive(Clone, PartialEq)] pub struct UsageUpstreamError { diff --git a/backend/crates/gateway-admin/tests/use_case/observability/mod.rs b/backend/crates/gateway-admin/tests/use_case/observability/mod.rs index c02a7fb77..28c6dd514 100644 --- a/backend/crates/gateway-admin/tests/use_case/observability/mod.rs +++ b/backend/crates/gateway-admin/tests/use_case/observability/mod.rs @@ -454,6 +454,8 @@ fn total_record( platform_icon: None, provider_account_upstream_peer: None, has_upstream_error: false, + upstream_error_message: None, + retry_after_ms: None, error_code: None, error_message: None, error_kind: None, diff --git a/backend/crates/gateway-api/src/admin/observability/presenter.rs b/backend/crates/gateway-api/src/admin/observability/presenter.rs index b0a49f16d..cc180c8bb 100644 --- a/backend/crates/gateway-api/src/admin/observability/presenter.rs +++ b/backend/crates/gateway-api/src/admin/observability/presenter.rs @@ -263,6 +263,8 @@ pub(crate) fn usage_list_record_view( upstream_status_code: record.upstream_status_code, error_code: record.error_code, error_message: record.error_message, + upstream_error_message: record.upstream_error_message, + retry_after_ms: record.retry_after_ms, error_kind: record.error_kind, websocket_close_code: record.websocket_close_code, continuation_unavailable_reason: record.continuation_unavailable_reason, diff --git a/backend/crates/gateway-api/src/admin/observability/wire.rs b/backend/crates/gateway-api/src/admin/observability/wire.rs index cc4da9b49..a5ecda6eb 100644 --- a/backend/crates/gateway-api/src/admin/observability/wire.rs +++ b/backend/crates/gateway-api/src/admin/observability/wire.rs @@ -92,6 +92,8 @@ pub struct UsageListRecordView { pub upstream_status_code: Option, pub error_code: Option, pub error_message: Option, + pub upstream_error_message: Option, + pub retry_after_ms: Option, pub error_kind: Option, pub websocket_close_code: Option, pub continuation_unavailable_reason: Option, diff --git a/backend/crates/gateway-api/tests/admin/observability/response.rs b/backend/crates/gateway-api/tests/admin/observability/response.rs index 3f8be2de8..819bbdedd 100644 --- a/backend/crates/gateway-api/tests/admin/observability/response.rs +++ b/backend/crates/gateway-api/tests/admin/observability/response.rs @@ -990,6 +990,8 @@ async fn usage_route_projects_request_facts_and_billing() { platform_icon: None, provider_account_upstream_peer: None, has_upstream_error: false, + upstream_error_message: None, + retry_after_ms: None, error_code: None, error_message: None, error_kind: None, @@ -1092,6 +1094,8 @@ async fn usage_route_projects_request_facts_and_billing() { failed.upstream_status_code = Some(429); failed.error_code = Some("server_error".to_owned()); failed.error_message = Some("The upstream service is temporarily unavailable.".to_owned()); + failed.upstream_error_message = Some("Retry once capacity is available.".to_owned()); + failed.retry_after_ms = Some(30_000); records.push(failed); let mut closed = records[0].clone(); closed.id = "request_ws_closed".to_owned(); @@ -1134,6 +1138,11 @@ async fn usage_route_projects_request_facts_and_billing() { assert!(value["data"]["items"][0]["errorCode"].is_null()); assert!(value["data"]["items"][0]["errorMessage"].is_null()); assert_eq!(value["data"]["items"][3]["errorCode"], "server_error"); + assert_eq!( + value["data"]["items"][3]["upstreamErrorMessage"], + "Retry once capacity is available." + ); + assert_eq!(value["data"]["items"][3]["retryAfterMs"], 30_000); assert_eq!(value["data"]["items"][3]["clientStatusCode"], 503); assert_eq!(value["data"]["items"][3]["upstreamStatusCode"], 429); assert_eq!(value["data"]["items"][3]["errorOrigin"], "upstream"); diff --git a/backend/crates/gateway-api/tests/key_usage/fixtures.rs b/backend/crates/gateway-api/tests/key_usage/fixtures.rs index 8cb8336cd..fef7b2c7b 100644 --- a/backend/crates/gateway-api/tests/key_usage/fixtures.rs +++ b/backend/crates/gateway-api/tests/key_usage/fixtures.rs @@ -104,6 +104,8 @@ fn usage_record() -> UsageListRecord { platform_icon: None, provider_account_upstream_peer: None, has_upstream_error: false, + upstream_error_message: Some("private-sentinel-upstream".into()), + retry_after_ms: Some(30_000), error_code: None, error_message: None, error_kind: None, diff --git a/backend/crates/gateway-store/src/postgres/observability/mapping.rs b/backend/crates/gateway-store/src/postgres/observability/mapping.rs index 58a1a1305..21e3bf155 100644 --- a/backend/crates/gateway-store/src/postgres/observability/mapping.rs +++ b/backend/crates/gateway-store/src/postgres/observability/mapping.rs @@ -370,6 +370,8 @@ pub(crate) fn usage_list_record_from_row( )?; Ok(UsageListRecord { has_upstream_error: get(row, "has_upstream_error")?, + upstream_error_message: get(row, "upstream_error_message")?, + retry_after_ms: optional_unsigned(row, "retry_after_ms")?, error_code: get(row, "error_code")?, error_message: get(row, "error_message")?, error_kind: get(row, "error_kind")?, diff --git a/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs b/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs index b868f45d0..1e406cffd 100644 --- a/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs +++ b/backend/crates/gateway-store/src/postgres/observability/queries/usage.rs @@ -130,7 +130,7 @@ pub(crate) fn push_usage_filter( pub(crate) const USAGE_LIST_RECORD_SELECT: &str = "select mr.id, mr.outcome, mr.provider_error_code as error_code, mr.error_message, mr.client_status_code, mr.client_api_key_name_snapshot as client_api_key_name, mr.endpoint, mr.client_transport, mr.requested_model_id, mr.error_kind, mr.upstream_status_code, - facts.has_upstream_error, facts.websocket_close_code, + facts.has_upstream_error, facts.websocket_close_code, facts.upstream_error_message, mr.retry_after_ms, mr.continuation_unavailable_reason, mr.upstream_connection_exit_reason, (mr.recovery_request_id is not null and mr.recovered_at is not null) as recovered, mr.provider_kind, mr.provider_account_ref, @@ -166,6 +166,13 @@ const SAVED_ERROR_FACTS: &str = "(select exists ( ) as field(value) where jsonb_typeof(field.value) = 'string' and field.value <> '\"\"'::jsonb ) as has_upstream_error, + coalesce( + case when jsonb_typeof(saved_error.body -> 'message') = 'string' + then nullif(saved_error.body ->> 'message', '') end, + case when saved_upstream.body ->> 'type' = 'websocket.close' + and jsonb_typeof(saved_upstream.body -> 'reason') = 'string' + then nullif(saved_upstream.body ->> 'reason', '') end + ) as upstream_error_message, case when saved_upstream.body ->> 'type' = 'websocket.close' and jsonb_typeof(saved_upstream.body -> 'code') = 'number' and (saved_upstream.body ->> 'code') ~ '^[0-9]{1,5}$' diff --git a/backend/crates/gateway-store/tests/postgres/observability/mod.rs b/backend/crates/gateway-store/tests/postgres/observability/mod.rs index 834366d1a..1750babbf 100644 --- a/backend/crates/gateway-store/tests/postgres/observability/mod.rs +++ b/backend/crates/gateway-store/tests/postgres/observability/mod.rs @@ -125,7 +125,7 @@ async fn usage_error_origins_agree_for_saved_errors_without_failure_status() { "update model_requests set client_status_code = 200, upstream_status_code = 200, error_kind = 'unavailable', request_observation_json = jsonb_set(request_observation_json, '{error}', - '{\"message\":\"Upstream error summary\"}'::jsonb) + '{\"message\":\"Upstream error summary\",\"retryAfterMs\":30000}'::jsonb) where id = 'req_observe_failed'", ) .execute(&database.pool) @@ -191,6 +191,13 @@ async fn usage_error_origins_agree_for_saved_errors_without_failure_status() { assert_eq!(detail.request.provider_error_code, None); assert_eq!(list.client_status_code, Some(200)); assert_eq!(list.upstream_status_code, Some(200)); + let expected_message = error + .get("message") + .and_then(serde_json::Value::as_str) + .filter(|value| !value.is_empty()); + assert_eq!(list.upstream_error_message.as_deref(), expected_message); + assert_eq!(list.retry_after_ms, Some(30_000)); + assert_eq!(list.retry_after_ms, detail.request.retry_after_ms); assert_eq!(list.error_origin(), origin, "list: {body}"); assert_eq!(detail.request.error_origin(), origin, "detail: {body}"); } @@ -437,6 +444,12 @@ async fn usage_error_facts_preserve_recovery_and_validate_saved_close_frames() { .await .expect("detail with saved error facts"); assert_eq!(detail.request.websocket_close_code, close_code); + let close_reason = detail + .request + .upstream_error + .as_ref() + .and_then(|error| error.close_reason.as_deref()); + assert_eq!(record.upstream_error_message.as_deref(), close_reason); assert!(detail.request.recovered); assert_eq!( detail.request.continuation_unavailable_reason, diff --git a/docs/api.md b/docs/api.md index dc73665de..1d60f1c50 100644 --- a/docs/api.md +++ b/docs/api.md @@ -3148,7 +3148,8 @@ Client Key 的频率、并发和费用预算限制分别为 `key_rate_limited` 或 WS 关闭帧的 `closeReason`、`lastEventType`;不重新分类错误,也不包含原始请求、任意响应头或本地原因链。 缺失、非 JSON 或未识别的结构返回 `null`,完整受控诊断通过诊断包获取。 详情的 `message` 是网关摘要,不等同于上游 `upstreamError.message`; -`retryAfterMs` 是已记录的重试等待毫秒数,未记录时为 `null` +列表的 `upstreamErrorMessage` 优先取上游消息,其次取 WS 关闭原因,两者缺失时为 `null`。 +列表与详情的 `retryAfterMs` 是已记录的重试等待毫秒数,未记录时为 `null` 详情 `attempts[].downstreamCommitted` 为 `true` 时表示已开始客户端响应,为 `false` 时表示未开始。 中间尝试的运维事件未保存交付边界,返回 `null`;不能据此推断客户端尚未收到响应 diff --git a/frontend/src/api/modules/usage.ts b/frontend/src/api/modules/usage.ts index 7d0a6633e..d7ea249d6 100644 --- a/frontend/src/api/modules/usage.ts +++ b/frontend/src/api/modules/usage.ts @@ -87,6 +87,7 @@ export interface UsageErrorFacts { errorCode: string | null errorKind: string | null websocketCloseCode: number | null + retryAfterMs: number | null continuationUnavailableReason: string | null upstreamConnectionExitReason: string | null recovered: boolean @@ -98,6 +99,7 @@ export interface UsageListRecord extends UsageErrorFacts { clientStatusCode: number | null upstreamStatusCode: number | null errorMessage: string | null + upstreamErrorMessage: string | null clientApiKeyName: string | null id: string provider: string | null @@ -140,7 +142,6 @@ export interface UsageRecord extends UsageErrorFacts { closeReason: string | null lastEventType: string | null } | null - retryAfterMs: number | null id: string requestId: string clientApiKeyId: string | null diff --git a/frontend/src/components/usage-records-table/UsageErrorCell.vue b/frontend/src/components/usage-records-table/UsageErrorCell.vue index d9bf730d1..f92c1eac7 100644 --- a/frontend/src/components/usage-records-table/UsageErrorCell.vue +++ b/frontend/src/components/usage-records-table/UsageErrorCell.vue @@ -1,11 +1,9 @@