Selaa lähdekoodia

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

master
fengsilin 3 viikkoa sitten
vanhempi
commit
fe62509b03
1 muutettua tiedostoa jossa 17 lisäystä ja 0 poistoa
  1. +17
    -0
      controller/relay.go

+ 17
- 0
controller/relay.go Näytä tiedosto

@@ -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")


Ladataan…
Peruuta
Tallenna