From fe62509b03d2b4ac563fd564ec69d9d45902c10d Mon Sep 17 00:00:00 2001 From: fengsilin Date: Wed, 20 May 2026 18:20:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(metrics):=20=E9=87=8D=E8=AF=95=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E4=B8=AD?= =?UTF-8?q?=E9=87=87=E9=9B=86=E6=8C=87=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/relay.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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")