Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func RegisterMetrics(registry prometheus.Registerer) {
registry.MustRegister(relayRequestE2ESuccessCounter)
registry.MustRegister(relayRequestE2EFailedCounter)
registry.MustRegister(relayRequestE2EDurationObsever)
// token metrics
registry.MustRegister(inputTokensCounter)
registry.MustRegister(outputTokensCounter)
registry.MustRegister(cacheHitTokensCounter)
registry.MustRegister(inferenceTokensCounter)
}

var (
Expand Down Expand Up @@ -84,6 +89,34 @@ var (
},
[]string{"channel", "model", "group", "token_key", "token_name"},
)
// Token metrics
inputTokensCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: Namespace,
Name: "input_tokens_total",
Help: "Total number of input tokens processed",
}, []string{"channel", "model", "group", "user_id"})

outputTokensCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: Namespace,
Name: "output_tokens_total",
Help: "Total number of output tokens generated",
}, []string{"channel", "model", "group", "user_id"})

cacheHitTokensCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: Namespace,
Name: "cache_hit_tokens_total",
Help: "Total number of tokens served from cache",
}, []string{"channel", "model", "group", "user_id"})

inferenceTokensCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: Namespace,
Name: "inference_tokens_total",
Help: "Total number of tokens processed during inference",
}, []string{"channel", "model", "group", "user_id"})
)

func IncrementRelayRequestTotalCounter(channel, tag, baseURL, model, group string, add float64) {
Expand Down Expand Up @@ -121,3 +154,20 @@ func IncrementRelayRequestE2EFailedCounter(channel, model, group, code, tokenKey
func ObserveRelayRequestE2EDuration(channel, model, group, tokenKey, tokenName string, duration float64) {
relayRequestE2EDurationObsever.WithLabelValues(channel, model, group, tokenKey, tokenName).Observe(duration)
}

// Token metrics functions
func IncrementInputTokens(channel, model, group, userId string, add float64) {
inputTokensCounter.WithLabelValues(channel, model, group, userId).Add(add)
}

func IncrementOutputTokens(channel, model, group, userId string, add float64) {
outputTokensCounter.WithLabelValues(channel, model, group, userId).Add(add)
}

func IncrementCacheHitTokens(channel, model, group, userId string, add float64) {
cacheHitTokensCounter.WithLabelValues(channel, model, group, userId).Add(add)
}

func IncrementInferenceTokens(channel, model, group, userId string, add float64) {
inferenceTokensCounter.WithLabelValues(channel, model, group, userId).Add(add)
}
14 changes: 13 additions & 1 deletion relay/relay-text.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func TextHelper(c *gin.Context, relayInfo *relaycommon.RelayInfo, textRequest *d
c.Set("prompt_tokens", promptTokens)
}

// Record input tokens metric
metrics.IncrementInputTokens(strconv.Itoa(relayInfo.ChannelId), textRequest.Model, relayInfo.Group, strconv.Itoa(relayInfo.UserId), float64(promptTokens))

priceData, err := helper.ModelPriceHelper(c, relayInfo, promptTokens, int(textRequest.MaxTokens))
if err != nil {
funcErr = service.OpenAIErrorWrapperLocal(err, "model_price_error", http.StatusInternalServerError)
Expand Down Expand Up @@ -291,6 +294,7 @@ func TextHelper(c *gin.Context, relayInfo *relaycommon.RelayInfo, textRequest *d
} else {
postConsumeQuota(c, relayInfo, usage.(*dto.Usage), preConsumedQuota, userQuota, priceData, "")
}

return nil
}

Expand Down Expand Up @@ -405,7 +409,6 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
useTimeSeconds := time.Now().Unix() - relayInfo.StartTime.Unix()
promptTokens := usage.PromptTokens
cacheTokens := usage.PromptTokensDetails.CachedTokens
//

completionTokens := usage.CompletionTokens
thinkingTokens := usage.CompletionTokenDetails.ReasoningTokens
Expand Down Expand Up @@ -479,6 +482,15 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
logContent += ", " + extraContent
}

// Record token metrics
metrics.IncrementOutputTokens(strconv.Itoa(relayInfo.ChannelId), modelName, relayInfo.Group, strconv.Itoa(relayInfo.UserId), float64(completionTokens))

if cacheTokens > 0 {
metrics.IncrementCacheHitTokens(strconv.Itoa(relayInfo.ChannelId), modelName, relayInfo.Group, strconv.Itoa(relayInfo.UserId), float64(cacheTokens))
}

metrics.IncrementInferenceTokens(strconv.Itoa(relayInfo.ChannelId), modelName, relayInfo.Group, strconv.Itoa(relayInfo.UserId), float64(thinkingTokens))

other := service.GenerateTextOtherInfo(ctx, relayInfo, modelRatio, groupRatio, completionRatio, cacheTokens, cacheRatio, modelPrice)
model.RecordConsumeLog(ctx, relayInfo.UserId, relayInfo.ChannelId, promptTokens, completionTokens, thinkingTokens, logModel,
tokenName, quota, logContent, relayInfo.TokenId, userQuota, int(useTimeSeconds), relayInfo.IsStream, relayInfo.Group, other)
Expand Down