logRequest is a helper function to create and record a request log.
( c *gin.Context, originalGroup *models.Group, group *models.Group, apiKey *models.APIKey, startTime time.Time, statusCode int, finalError error, isStream bool, upstreamAddr string, channelHandler channel.ChannelProxy, bodyBytes []byte, requestType string, )
| 299 | |
| 300 | // logRequest is a helper function to create and record a request log. |
| 301 | func (ps *ProxyServer) logRequest( |
| 302 | c *gin.Context, |
| 303 | originalGroup *models.Group, |
| 304 | group *models.Group, |
| 305 | apiKey *models.APIKey, |
| 306 | startTime time.Time, |
| 307 | statusCode int, |
| 308 | finalError error, |
| 309 | isStream bool, |
| 310 | upstreamAddr string, |
| 311 | channelHandler channel.ChannelProxy, |
| 312 | bodyBytes []byte, |
| 313 | requestType string, |
| 314 | ) { |
| 315 | if ps.requestLogService == nil { |
| 316 | return |
| 317 | } |
| 318 | |
| 319 | var requestBodyToLog, userAgent string |
| 320 | |
| 321 | if group.EffectiveConfig.EnableRequestBodyLogging { |
| 322 | requestBodyToLog = utils.TruncateString(string(bodyBytes), 65000) |
| 323 | userAgent = c.Request.UserAgent() |
| 324 | } |
| 325 | |
| 326 | duration := time.Since(startTime).Milliseconds() |
| 327 | |
| 328 | logEntry := &models.RequestLog{ |
| 329 | GroupID: group.ID, |
| 330 | GroupName: group.Name, |
| 331 | IsSuccess: finalError == nil && statusCode < 400, |
| 332 | SourceIP: c.ClientIP(), |
| 333 | StatusCode: statusCode, |
| 334 | RequestPath: utils.TruncateString(c.Request.URL.String(), 500), |
| 335 | Duration: duration, |
| 336 | UserAgent: userAgent, |
| 337 | RequestType: requestType, |
| 338 | IsStream: isStream, |
| 339 | UpstreamAddr: utils.TruncateString(upstreamAddr, 500), |
| 340 | RequestBody: requestBodyToLog, |
| 341 | } |
| 342 | |
| 343 | // Set parent group |
| 344 | if originalGroup != nil && originalGroup.GroupType == "aggregate" && originalGroup.ID != group.ID { |
| 345 | logEntry.ParentGroupID = originalGroup.ID |
| 346 | logEntry.ParentGroupName = originalGroup.Name |
| 347 | } |
| 348 | |
| 349 | if channelHandler != nil && bodyBytes != nil { |
| 350 | logEntry.Model = channelHandler.ExtractModel(c, bodyBytes) |
| 351 | } |
| 352 | |
| 353 | if apiKey != nil { |
| 354 | // 加密密钥值用于日志存储 |
| 355 | encryptedKeyValue, err := ps.encryptionSvc.Encrypt(apiKey.KeyValue) |
| 356 | if err != nil { |
| 357 | logrus.WithError(err).Error("Failed to encrypt key value for logging") |
| 358 | logEntry.KeyValue = "failed-to-encryption" |
no test coverage detected