**新增功能**: - 新建ops_upstream_error_events表存储上游服务错误详情 - 支持记录上游429/529/5xx错误的详细上下文信息 - 提供按时间范围查询上游错误事件的API **后端改动**: 1. 模型层(ops_models.go, ops_port.go): - 新增UpstreamErrorEvent结构体 - 扩展Repository接口支持上游错误事件CRUD 2. 仓储层(ops_repo.go): - 实现InsertUpstreamErrorEvent写入上游错误 - 实现GetUpstreamErrorEvents按时间范围查询 3. 服务层(ops_service.go, ops_upstream_context.go): - ops_service: 新增GetUpstreamErrorEvents查询方法 - ops_upstream_context: 封装上游错误上下文构建逻辑 4. Handler层(ops_error_logger.go): - 新增GetUpstreamErrorsHandler处理上游错误查询请求 5. Gateway层集成: - antigravity_gateway_service.go: 429/529错误时记录上游事件 - gateway_service.go: OpenAI 429/5xx错误时记录 - gemini_messages_compat_service.go: Gemini 429/5xx错误时记录 - openai_gateway_service.go: OpenAI 429/5xx错误时记录 - ratelimit_service.go: 429限流错误时记录 **数据记录字段**: - request_id: 关联ops_logs主记录 - platform/model: 上游服务标识 - status_code/error_message: 错误详情 - request_headers/response_body: 调试信息(可选) - created_at: 错误发生时间
107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Gin context keys used by Ops error logger for capturing upstream error details.
|
|
// These keys are set by gateway services and consumed by handler/ops_error_logger.go.
|
|
const (
|
|
OpsUpstreamStatusCodeKey = "ops_upstream_status_code"
|
|
OpsUpstreamErrorMessageKey = "ops_upstream_error_message"
|
|
OpsUpstreamErrorDetailKey = "ops_upstream_error_detail"
|
|
OpsUpstreamErrorsKey = "ops_upstream_errors"
|
|
)
|
|
|
|
func setOpsUpstreamError(c *gin.Context, upstreamStatusCode int, upstreamMessage, upstreamDetail string) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
if upstreamStatusCode > 0 {
|
|
c.Set(OpsUpstreamStatusCodeKey, upstreamStatusCode)
|
|
}
|
|
if msg := strings.TrimSpace(upstreamMessage); msg != "" {
|
|
c.Set(OpsUpstreamErrorMessageKey, msg)
|
|
}
|
|
if detail := strings.TrimSpace(upstreamDetail); detail != "" {
|
|
c.Set(OpsUpstreamErrorDetailKey, detail)
|
|
}
|
|
}
|
|
|
|
// OpsUpstreamErrorEvent describes one upstream error attempt during a single gateway request.
|
|
// It is stored in ops_error_logs.upstream_errors as a JSON array.
|
|
type OpsUpstreamErrorEvent struct {
|
|
AtUnixMs int64 `json:"at_unix_ms,omitempty"`
|
|
|
|
// Context
|
|
Platform string `json:"platform,omitempty"`
|
|
AccountID int64 `json:"account_id,omitempty"`
|
|
|
|
// Outcome
|
|
UpstreamStatusCode int `json:"upstream_status_code,omitempty"`
|
|
UpstreamRequestID string `json:"upstream_request_id,omitempty"`
|
|
|
|
// Kind: http_error | request_error | retry_exhausted | failover
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
Detail string `json:"detail,omitempty"`
|
|
}
|
|
|
|
func appendOpsUpstreamError(c *gin.Context, ev OpsUpstreamErrorEvent) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
if ev.AtUnixMs <= 0 {
|
|
ev.AtUnixMs = time.Now().UnixMilli()
|
|
}
|
|
ev.Platform = strings.TrimSpace(ev.Platform)
|
|
ev.UpstreamRequestID = strings.TrimSpace(ev.UpstreamRequestID)
|
|
ev.Kind = strings.TrimSpace(ev.Kind)
|
|
ev.Message = strings.TrimSpace(ev.Message)
|
|
ev.Detail = strings.TrimSpace(ev.Detail)
|
|
if ev.Message != "" {
|
|
ev.Message = sanitizeUpstreamErrorMessage(ev.Message)
|
|
}
|
|
|
|
var existing []*OpsUpstreamErrorEvent
|
|
if v, ok := c.Get(OpsUpstreamErrorsKey); ok {
|
|
if arr, ok := v.([]*OpsUpstreamErrorEvent); ok {
|
|
existing = arr
|
|
}
|
|
}
|
|
|
|
evCopy := ev
|
|
existing = append(existing, &evCopy)
|
|
c.Set(OpsUpstreamErrorsKey, existing)
|
|
}
|
|
|
|
func getOpsUpstreamErrors(c *gin.Context) []*OpsUpstreamErrorEvent {
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
if v, ok := c.Get(OpsUpstreamErrorsKey); ok {
|
|
if arr, ok := v.([]*OpsUpstreamErrorEvent); ok {
|
|
return arr
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func marshalOpsUpstreamErrors(events []*OpsUpstreamErrorEvent) *string {
|
|
if len(events) == 0 {
|
|
return nil
|
|
}
|
|
// Ensure we always store a valid JSON value.
|
|
raw, err := json.Marshal(events)
|
|
if err != nil || len(raw) == 0 {
|
|
return nil
|
|
}
|
|
s := string(raw)
|
|
return &s
|
|
}
|