feat: 请求日志添加模型字段 (#113)

* feat: 请求日志添加模型字段

* fix: 调整gemini判断模型顺序
This commit is contained in:
tbphp
2025-08-03 12:29:18 +08:00
committed by GitHub
parent 2d9a7859aa
commit 2be2ea697e
9 changed files with 115 additions and 40 deletions

View File

@@ -114,11 +114,11 @@ func (ps *ProxyServer) executeRequestWithRetry(
}
logrus.Debugf("Max retries exceeded for group %s after %d attempts. Parsed Error: %s", group.Name, retryCount, logMessage)
ps.logRequest(c, group, &models.APIKey{KeyValue: lastError.KeyValue}, 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, channelHandler, bodyBytes)
} 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, nil, startTime, http.StatusServiceUnavailable, retryCount, app_errors.ErrMaxRetriesExceeded, isStream, "")
ps.logRequest(c, group, nil, startTime, http.StatusServiceUnavailable, retryCount, app_errors.ErrMaxRetriesExceeded, isStream, "", channelHandler, bodyBytes)
}
return
}
@@ -127,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, nil, startTime, http.StatusServiceUnavailable, retryCount, err, isStream, "")
ps.logRequest(c, group, nil, startTime, http.StatusServiceUnavailable, retryCount, err, isStream, "", channelHandler, bodyBytes)
return
}
@@ -184,7 +184,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, startTime, 499, retryCount+1, err, isStream, upstreamURL)
ps.logRequest(c, group, apiKey, startTime, 499, retryCount+1, err, isStream, upstreamURL, channelHandler, bodyBytes)
return
}
@@ -227,7 +227,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, startTime, resp.StatusCode, retryCount+1, nil, isStream, upstreamURL)
ps.logRequest(c, group, apiKey, startTime, resp.StatusCode, retryCount+1, nil, isStream, upstreamURL, channelHandler, bodyBytes)
for key, values := range resp.Header {
for _, value := range values {
@@ -254,6 +254,8 @@ func (ps *ProxyServer) logRequest(
finalError error,
isStream bool,
upstreamAddr string,
channelHandler channel.ChannelProxy,
bodyBytes []byte,
) {
if ps.requestLogService == nil {
return
@@ -274,6 +276,11 @@ func (ps *ProxyServer) logRequest(
IsStream: isStream,
UpstreamAddr: utils.TruncateString(upstreamAddr, 500),
}
if channelHandler != nil && bodyBytes != nil {
logEntry.Model = channelHandler.ExtractModel(c, bodyBytes)
}
if apiKey != nil {
logEntry.KeyValue = apiKey.KeyValue
}