-
Notifications
You must be signed in to change notification settings - Fork 11.2k
增加调试日志并记录请求体 #2731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
增加调试日志并记录请求体 #2731
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,22 +4,29 @@ import ( | |||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| basecommon "github.com/QuantumNous/new-api/common" | ||||||||||||||||||||||||||||||||||
| "github.com/QuantumNous/new-api/dto" | ||||||||||||||||||||||||||||||||||
| "github.com/QuantumNous/new-api/logger" | ||||||||||||||||||||||||||||||||||
| "github.com/QuantumNous/new-api/relay/common" | ||||||||||||||||||||||||||||||||||
| "github.com/gin-gonic/gin" | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func ModelMappedHelper(c *gin.Context, info *common.RelayInfo, request dto.Request) error { | ||||||||||||||||||||||||||||||||||
| // map model name | ||||||||||||||||||||||||||||||||||
| modelMapping := c.GetString("model_mapping") | ||||||||||||||||||||||||||||||||||
| logger.LogDebug(c, "模型映射检查: 原模型=%q, 映射配置=%s", info.OriginModelName, modelMapping) | ||||||||||||||||||||||||||||||||||
| chain := []string{info.OriginModelName} | ||||||||||||||||||||||||||||||||||
| if modelMapping != "" && modelMapping != "{}" { | ||||||||||||||||||||||||||||||||||
| modelMap := make(map[string]string) | ||||||||||||||||||||||||||||||||||
| err := json.Unmarshal([]byte(modelMapping), &modelMap) | ||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||
| return fmt.Errorf("unmarshal_model_mapping_failed") | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| logger.LogDebug(c, "模型映射解析: 原模型=%q, 映射表=%s", info.OriginModelName, basecommon.GetJsonString(modelMap)) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // 支持链式模型重定向,最终使用链尾的模型 | ||||||||||||||||||||||||||||||||||
| currentModel := info.OriginModelName | ||||||||||||||||||||||||||||||||||
| visitedModels := map[string]bool{ | ||||||||||||||||||||||||||||||||||
|
|
@@ -42,6 +49,7 @@ func ModelMappedHelper(c *gin.Context, info *common.RelayInfo, request dto.Reque | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| visitedModels[mappedModel] = true | ||||||||||||||||||||||||||||||||||
| currentModel = mappedModel | ||||||||||||||||||||||||||||||||||
| chain = append(chain, currentModel) | ||||||||||||||||||||||||||||||||||
| info.IsModelMapped = true | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||
|
|
@@ -50,7 +58,14 @@ func ModelMappedHelper(c *gin.Context, info *common.RelayInfo, request dto.Reque | |||||||||||||||||||||||||||||||||
| if info.IsModelMapped { | ||||||||||||||||||||||||||||||||||
| info.UpstreamModelName = currentModel | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| logger.LogDebug(c, "模型映射跳过: 原模型=%q, 未配置映射表", info.OriginModelName) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| upstreamModel := info.UpstreamModelName | ||||||||||||||||||||||||||||||||||
| if upstreamModel == "" { | ||||||||||||||||||||||||||||||||||
| upstreamModel = info.OriginModelName | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| logger.LogDebug(c, "模型映射结果: 原模型=%q, 映射链路=%s, 上游模型=%q, 已映射=%t", info.OriginModelName, strings.Join(chain, "->"), upstreamModel, info.IsModelMapped) | ||||||||||||||||||||||||||||||||||
| if request != nil { | ||||||||||||||||||||||||||||||||||
| request.SetModelName(info.UpstreamModelName) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+64
to
71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default upstream model before mutating the request.
✅ Suggested fix- upstreamModel := info.UpstreamModelName
- if upstreamModel == "" {
- upstreamModel = info.OriginModelName
- }
+ if info.UpstreamModelName == "" {
+ info.UpstreamModelName = info.OriginModelName
+ }
+ upstreamModel := info.UpstreamModelName
logger.LogDebug(c, "模型映射结果: 原模型=%q, 映射链路=%s, 上游模型=%q, 已映射=%t", info.OriginModelName, strings.Join(chain, "->"), upstreamModel, info.IsModelMapped)
if request != nil {
request.SetModelName(info.UpstreamModelName)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid persisting raw request bodies to error logs.
Storing full request bodies in error logs risks leaking sensitive data and can bloat storage (especially for large or binary payloads). Consider truncation and/or masking, and optionally gating by content type.
✅ Suggested fix (truncate + flag)
requestBody, bodyErr := common.GetRequestBody(c) if bodyErr == nil { - other["request_body"] = string(requestBody) + const maxLogBytes = 2048 + bodyForLog := requestBody + if len(bodyForLog) > maxLogBytes { + bodyForLog = bodyForLog[:maxLogBytes] + other["request_body_truncated"] = true + } + other["request_body"] = string(bodyForLog) } else { other["request_body_error"] = bodyErr.Error() }🤖 Prompt for AI Agents