| @@ -5,10 +5,12 @@ import ( | |||||
| "fmt" | "fmt" | ||||
| "io" | "io" | ||||
| "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" | ||||
| @@ -517,4 +519,31 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usage | |||||
| Group: relayInfo.UsingGroup, | Group: relayInfo.UsingGroup, | ||||
| Other: other, | 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) | |||||
| } | |||||
| } | |||||
| } | } | ||||