refactor(logging): replace gin-specific context handling with generic context-based request metadata utilities

- Introduced reusable utilities in `requestmeta` to manage endpoint and response status in request contexts.
- Refactored plugins and handlers to use context-based metadata, removing direct dependency on `gin`.
- Updated tests to validate new context utilities and replaced `gin`-based context handling.

Fixed: #3166
This commit is contained in:
Luis Pater
2026-04-30 23:36:07 +08:00
parent 8b286e8fb3
commit 4035abc0cd
5 changed files with 174 additions and 68 deletions
+4 -24
View File
@@ -11,7 +11,7 @@ import (
"sync/atomic"
"time"
"github.com/gin-gonic/gin"
internallogging "github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
coreusage "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage"
)
@@ -401,21 +401,8 @@ func dedupKey(apiName, modelName string, detail RequestDetail) string {
func resolveAPIIdentifier(ctx context.Context, record coreusage.Record) string {
if ctx != nil {
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
path := ginCtx.FullPath()
if path == "" && ginCtx.Request != nil {
path = ginCtx.Request.URL.Path
}
method := ""
if ginCtx.Request != nil {
method = ginCtx.Request.Method
}
if path != "" {
if method != "" {
return method + " " + path
}
return path
}
if endpoint := strings.TrimSpace(internallogging.GetEndpoint(ctx)); endpoint != "" {
return endpoint
}
}
if record.Provider != "" {
@@ -425,14 +412,7 @@ func resolveAPIIdentifier(ctx context.Context, record coreusage.Record) string {
}
func resolveSuccess(ctx context.Context) bool {
if ctx == nil {
return true
}
ginCtx, ok := ctx.Value("gin").(*gin.Context)
if !ok || ginCtx == nil {
return true
}
status := ginCtx.Writer.Status()
status := internallogging.GetResponseStatus(ctx)
if status == 0 {
return true
}