Skip to content

Commit 865d4b4

Browse files
committed
add consumer tag for token usage metric
1 parent 0c53ccf commit 865d4b4

File tree

1 file changed

+16
-8
lines changed
  • plugins/wasm-go/extensions/ai-statistics

1 file changed

+16
-8
lines changed

plugins/wasm-go/extensions/ai-statistics/main.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
RouteName = "route"
3737
ClusterName = "cluster"
3838
APIName = "api"
39+
ConsumerKey = "x-mse-consumer"
3940

4041
// Source Type
4142
FixedValue = "fixed_value"
@@ -81,8 +82,8 @@ type AIStatisticsConfig struct {
8182
shouldBufferStreamingBody bool
8283
}
8384

84-
func generateMetricName(route, cluster, model, metricName string) string {
85-
return fmt.Sprintf("route.%s.upstream.%s.model.%s.metric.%s", route, cluster, model, metricName)
85+
func generateMetricName(route, cluster, model, consumer, metricName string) string {
86+
return fmt.Sprintf("route.%s.upstream.%s.model.%s.consumer.%s.metric.%s", route, cluster, model, consumer, metricName)
8687
}
8788

8889
func getRouteName() (string, error) {
@@ -115,6 +116,9 @@ func getClusterName() (string, error) {
115116
}
116117

117118
func (config *AIStatisticsConfig) incrementCounter(metricName string, inc uint64) {
119+
if inc == 0 {
120+
return
121+
}
118122
counter, ok := config.counterMetrics[metricName]
119123
if !ok {
120124
counter = proxywasm.DefineCounterMetric(metricName)
@@ -158,6 +162,9 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, lo
158162
ctx.SetContext(ClusterName, cluster)
159163
ctx.SetUserAttribute(APIName, api)
160164
ctx.SetContext(StatisticsRequestStartTime, time.Now().UnixMilli())
165+
if consumer, _ := proxywasm.GetHttpRequestHeader(ConsumerKey); consumer != "" {
166+
ctx.SetContext(ConsumerKey, consumer)
167+
}
161168

162169
// Set user defined log & span attributes which type is fixed_value
163170
setAttributeBySource(ctx, config, FixedValue, nil, log)
@@ -388,6 +395,7 @@ func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper
388395
var ok bool
389396
var route, cluster, model string
390397
var inputToken, outputToken uint64
398+
consumer := ctx.GetStringContext(ConsumerKey, "none")
391399
route, ok = ctx.GetContext(RouteName).(string)
392400
if !ok {
393401
log.Warnf("RouteName typd assert failed, skip metric record")
@@ -421,8 +429,8 @@ func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper
421429
log.Warnf("inputToken and outputToken cannot equal to 0, skip metric record")
422430
return
423431
}
424-
config.incrementCounter(generateMetricName(route, cluster, model, InputToken), inputToken)
425-
config.incrementCounter(generateMetricName(route, cluster, model, OutputToken), outputToken)
432+
config.incrementCounter(generateMetricName(route, cluster, model, consumer, InputToken), inputToken)
433+
config.incrementCounter(generateMetricName(route, cluster, model, consumer, OutputToken), outputToken)
426434

427435
// Generate duration metrics
428436
var llmFirstTokenDuration, llmServiceDuration uint64
@@ -433,17 +441,17 @@ func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper
433441
log.Warnf("LLMFirstTokenDuration typd assert failed")
434442
return
435443
}
436-
config.incrementCounter(generateMetricName(route, cluster, model, LLMFirstTokenDuration), llmFirstTokenDuration)
437-
config.incrementCounter(generateMetricName(route, cluster, model, LLMStreamDurationCount), 1)
444+
config.incrementCounter(generateMetricName(route, cluster, model, consumer, LLMFirstTokenDuration), llmFirstTokenDuration)
445+
config.incrementCounter(generateMetricName(route, cluster, model, consumer, LLMStreamDurationCount), 1)
438446
}
439447
if ctx.GetUserAttribute(LLMServiceDuration) != nil {
440448
llmServiceDuration, ok = convertToUInt(ctx.GetUserAttribute(LLMServiceDuration))
441449
if !ok {
442450
log.Warnf("LLMServiceDuration typd assert failed")
443451
return
444452
}
445-
config.incrementCounter(generateMetricName(route, cluster, model, LLMServiceDuration), llmServiceDuration)
446-
config.incrementCounter(generateMetricName(route, cluster, model, LLMDurationCount), 1)
453+
config.incrementCounter(generateMetricName(route, cluster, model, consumer, LLMServiceDuration), llmServiceDuration)
454+
config.incrementCounter(generateMetricName(route, cluster, model, consumer, LLMDurationCount), 1)
447455
}
448456
}
449457

0 commit comments

Comments
 (0)