fix: 冗余日志group和key字段

This commit is contained in:
tbphp
2025-07-16 22:39:41 +08:00
parent dc72e5850a
commit a64d48439f
5 changed files with 30 additions and 106 deletions

View File

@@ -9,7 +9,6 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"time"
"gpt-load/internal/channel"
@@ -115,12 +114,11 @@ func (ps *ProxyServer) executeRequestWithRetry(
}
logrus.Debugf("Max retries exceeded for group %s after %d attempts. Parsed Error: %s", group.Name, retryCount, logMessage)
keyID, _ := strconv.ParseUint(lastError.KeyID, 10, 64)
ps.logRequest(c, group, uint(keyID), startTime, lastError.StatusCode, retryCount, errors.New(logMessage), isStream, lastError.UpstreamAddr)
ps.logRequest(c, group, &models.APIKey{KeyValue: lastError.KeyValue}, startTime, lastError.StatusCode, retryCount, errors.New(logMessage), isStream, lastError.UpstreamAddr)
} else {
response.Error(c, app_errors.ErrMaxRetriesExceeded)
logrus.Debugf("Max retries exceeded for group %s after %d attempts.", group.Name, retryCount)
ps.logRequest(c, group, 0, startTime, http.StatusServiceUnavailable, retryCount, app_errors.ErrMaxRetriesExceeded, isStream, "")
ps.logRequest(c, group, nil, startTime, http.StatusServiceUnavailable, retryCount, app_errors.ErrMaxRetriesExceeded, isStream, "")
}
return
}
@@ -129,7 +127,7 @@ func (ps *ProxyServer) executeRequestWithRetry(
if err != nil {
logrus.Errorf("Failed to select a key for group %s on attempt %d: %v", group.Name, retryCount+1, err)
response.Error(c, app_errors.NewAPIError(app_errors.ErrNoKeysAvailable, err.Error()))
ps.logRequest(c, group, 0, startTime, http.StatusServiceUnavailable, retryCount, err, isStream, "")
ps.logRequest(c, group, nil, startTime, http.StatusServiceUnavailable, retryCount, err, isStream, "")
return
}
@@ -177,7 +175,7 @@ func (ps *ProxyServer) executeRequestWithRetry(
if err != nil || (resp != nil && resp.StatusCode >= 400) {
if err != nil && app_errors.IsIgnorableError(err) {
logrus.Debugf("Client-side ignorable error for key %s, aborting retries: %v", utils.MaskAPIKey(apiKey.KeyValue), err)
ps.logRequest(c, group, apiKey.ID, startTime, 499, retryCount+1, err, isStream, upstreamURL)
ps.logRequest(c, group, apiKey, startTime, 499, retryCount+1, err, isStream, upstreamURL)
return
}
@@ -210,7 +208,7 @@ func (ps *ProxyServer) executeRequestWithRetry(
StatusCode: statusCode,
ErrorMessage: errorMessage,
ParsedErrorMessage: parsedError,
KeyID: fmt.Sprintf("%d", apiKey.ID),
KeyValue: apiKey.KeyValue,
Attempt: retryCount + 1,
UpstreamAddr: upstreamURL,
})
@@ -220,7 +218,7 @@ func (ps *ProxyServer) executeRequestWithRetry(
// ps.keyProvider.UpdateStatus(apiKey, group, true) // 请求成功不再重置成功次数减少IO消耗
logrus.Debugf("Request for group %s succeeded on attempt %d with key %s", group.Name, retryCount+1, utils.MaskAPIKey(apiKey.KeyValue))
ps.logRequest(c, group, apiKey.ID, startTime, resp.StatusCode, retryCount+1, nil, isStream, upstreamURL)
ps.logRequest(c, group, apiKey, startTime, resp.StatusCode, retryCount+1, nil, isStream, upstreamURL)
for key, values := range resp.Header {
for _, value := range values {
@@ -240,7 +238,7 @@ func (ps *ProxyServer) executeRequestWithRetry(
func (ps *ProxyServer) logRequest(
c *gin.Context,
group *models.Group,
keyID uint,
apiKey *models.APIKey,
startTime time.Time,
statusCode int,
retries int,
@@ -256,7 +254,7 @@ func (ps *ProxyServer) logRequest(
logEntry := &models.RequestLog{
GroupID: group.ID,
KeyID: keyID,
GroupName: group.Name,
IsSuccess: finalError == nil && statusCode < 400,
SourceIP: c.ClientIP(),
StatusCode: statusCode,
@@ -267,6 +265,9 @@ func (ps *ProxyServer) logRequest(
IsStream: isStream,
UpstreamAddr: utils.TruncateString(upstreamAddr, 500),
}
if apiKey != nil {
logEntry.KeyValue = apiKey.KeyValue
}
if finalError != nil {
logEntry.ErrorMessage = finalError.Error()