Просмотр исходного кода

feat(metrics): 重试循环和错误处理中采集指标

master
fengsilin 3 недель назад
Родитель
Сommit
fe62509b03
1 измененных файлов: 17 добавлений и 0 удалений
  1. +17
    -0
      controller/relay.go

+ 17
- 0
controller/relay.go Просмотреть файл

@@ -6,10 +6,12 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"strconv"
"strings" "strings"
"time" "time"


"github.com/QuantumNous/new-api/common" "github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/common/metrics"
"github.com/QuantumNous/new-api/constant" "github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/dto" "github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/logger" "github.com/QuantumNous/new-api/logger"
@@ -87,6 +89,15 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) {
defer func() { defer func() {
if newAPIError != nil { if newAPIError != nil {
logger.LogError(c, fmt.Sprintf("relay error: %s", newAPIError.Error())) 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)) newAPIError.SetMessage(common.MessageWithRequestId(newAPIError.Error(), requestId))
switch relayFormat { switch relayFormat {
case types.RelayFormatOpenAIRealtime: case types.RelayFormatOpenAIRealtime:
@@ -231,6 +242,12 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) {
if !shouldRetry(c, newAPIError, common.RetryTimes-retryParam.GetRetry()) { if !shouldRetry(c, newAPIError, common.RetryTimes-retryParam.GetRetry()) {
break break
} }

// Prometheus: 重试计数
if metrics.IsEnabled() {
channelIdStr := strconv.Itoa(channel.Id)
metrics.GetMetrics().RequestRetriesTotal.WithLabelValues(channelIdStr, relayInfo.OriginModelName).Inc()
}
} }


useChannel := c.GetStringSlice("use_channel") useChannel := c.GetStringSlice("use_channel")


Загрузка…
Отмена
Сохранить