From e51a66c5f97688ecd128e28c6b52a318bc30d66e Mon Sep 17 00:00:00 2001 From: fengsilin Date: Wed, 20 May 2026 18:17:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(metrics):=20postConsumeQuota=20=E4=B8=AD?= =?UTF-8?q?=E9=87=87=E9=9B=86=20token/=E9=85=8D=E9=A2=9D/=E5=BB=B6?= =?UTF-8?q?=E8=BF=9F=E6=8C=87=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- relay/compatible_handler.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/relay/compatible_handler.go b/relay/compatible_handler.go index 17340a8..d0923a0 100644 --- a/relay/compatible_handler.go +++ b/relay/compatible_handler.go @@ -5,10 +5,12 @@ import ( "fmt" "io" "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" @@ -517,4 +519,31 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage Group: relayInfo.UsingGroup, Other: other, }) + + // Prometheus metrics 采集 + if metrics.IsEnabled() { + channelIdStr := strconv.Itoa(relayInfo.ChannelId) + modelName := relayInfo.OriginModelName + m := metrics.GetMetrics() + + if promptTokens > 0 { + m.TokensTotal.WithLabelValues(channelIdStr, modelName, "prompt").Add(float64(promptTokens)) + } + if completionTokens > 0 { + m.TokensTotal.WithLabelValues(channelIdStr, modelName, "completion").Add(float64(completionTokens)) + } + if cacheTokens > 0 { + m.TokensTotal.WithLabelValues(channelIdStr, modelName, "cache").Add(float64(cacheTokens)) + } + + m.QuotaConsumedTotal.WithLabelValues(channelIdStr, modelName, relayInfo.BillingSource).Add(float64(quota)) + + upstreamDuration := time.Since(relayInfo.StartTime).Seconds() + m.UpstreamDuration.WithLabelValues(channelIdStr, modelName).Observe(upstreamDuration) + + if relayInfo.IsStream && relayInfo.FirstResponseTime.After(relayInfo.StartTime) { + firstTokenDuration := relayInfo.FirstResponseTime.Sub(relayInfo.StartTime).Seconds() + m.FirstTokenDuration.WithLabelValues(channelIdStr, modelName).Observe(firstTokenDuration) + } + } }