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

feat(metrics): postConsumeQuota 中采集 token/配额/延迟指标

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

+ 29
- 0
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)
}
}
}

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