diff --git a/controller/relay.go b/controller/relay.go index 07b1a8a..e495c9e 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -6,10 +6,12 @@ import ( "io" "log" "net/http" + "strconv" "strings" "time" "github.com/QuantumNous/new-api/common" + "github.com/QuantumNous/new-api/common/metrics" "github.com/QuantumNous/new-api/constant" "github.com/QuantumNous/new-api/dto" "github.com/QuantumNous/new-api/logger" @@ -87,6 +89,15 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) { defer func() { if newAPIError != nil { logger.LogError(c, fmt.Sprintf("relay error: %s", newAPIError.Error())) + + // Prometheus: 错误计数 + if metrics.IsEnabled() { + channelId := strconv.Itoa(c.GetInt("channel_id")) + modelName := c.GetString("original_model") + errorType, errorCode := metrics.ClassifyError(newAPIError.StatusCode, newAPIError.Error()) + metrics.GetMetrics().RequestErrorsTotal.WithLabelValues(channelId, modelName, errorType, errorCode).Inc() + } + newAPIError.SetMessage(common.MessageWithRequestId(newAPIError.Error(), requestId)) switch relayFormat { case types.RelayFormatOpenAIRealtime: @@ -231,6 +242,12 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) { if !shouldRetry(c, newAPIError, common.RetryTimes-retryParam.GetRetry()) { break } + + // Prometheus: 重试计数 + if metrics.IsEnabled() { + channelIdStr := strconv.Itoa(channel.Id) + metrics.GetMetrics().RequestRetriesTotal.WithLabelValues(channelIdStr, relayInfo.OriginModelName).Inc() + } } useChannel := c.GetStringSlice("use_channel")