@@ -36,6 +36,7 @@ const (
36
36
RouteName = "route"
37
37
ClusterName = "cluster"
38
38
APIName = "api"
39
+ ConsumerKey = "x-mse-consumer"
39
40
40
41
// Source Type
41
42
FixedValue = "fixed_value"
@@ -81,8 +82,8 @@ type AIStatisticsConfig struct {
81
82
shouldBufferStreamingBody bool
82
83
}
83
84
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 )
86
87
}
87
88
88
89
func getRouteName () (string , error ) {
@@ -115,6 +116,9 @@ func getClusterName() (string, error) {
115
116
}
116
117
117
118
func (config * AIStatisticsConfig ) incrementCounter (metricName string , inc uint64 ) {
119
+ if inc == 0 {
120
+ return
121
+ }
118
122
counter , ok := config .counterMetrics [metricName ]
119
123
if ! ok {
120
124
counter = proxywasm .DefineCounterMetric (metricName )
@@ -158,6 +162,9 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config AIStatisticsConfig, lo
158
162
ctx .SetContext (ClusterName , cluster )
159
163
ctx .SetUserAttribute (APIName , api )
160
164
ctx .SetContext (StatisticsRequestStartTime , time .Now ().UnixMilli ())
165
+ if consumer , _ := proxywasm .GetHttpRequestHeader (ConsumerKey ); consumer != "" {
166
+ ctx .SetContext (ConsumerKey , consumer )
167
+ }
161
168
162
169
// Set user defined log & span attributes which type is fixed_value
163
170
setAttributeBySource (ctx , config , FixedValue , nil , log )
@@ -388,6 +395,7 @@ func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper
388
395
var ok bool
389
396
var route , cluster , model string
390
397
var inputToken , outputToken uint64
398
+ consumer := ctx .GetStringContext (ConsumerKey , "none" )
391
399
route , ok = ctx .GetContext (RouteName ).(string )
392
400
if ! ok {
393
401
log .Warnf ("RouteName typd assert failed, skip metric record" )
@@ -421,8 +429,8 @@ func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper
421
429
log .Warnf ("inputToken and outputToken cannot equal to 0, skip metric record" )
422
430
return
423
431
}
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 )
426
434
427
435
// Generate duration metrics
428
436
var llmFirstTokenDuration , llmServiceDuration uint64
@@ -433,17 +441,17 @@ func writeMetric(ctx wrapper.HttpContext, config AIStatisticsConfig, log wrapper
433
441
log .Warnf ("LLMFirstTokenDuration typd assert failed" )
434
442
return
435
443
}
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 )
438
446
}
439
447
if ctx .GetUserAttribute (LLMServiceDuration ) != nil {
440
448
llmServiceDuration , ok = convertToUInt (ctx .GetUserAttribute (LLMServiceDuration ))
441
449
if ! ok {
442
450
log .Warnf ("LLMServiceDuration typd assert failed" )
443
451
return
444
452
}
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 )
447
455
}
448
456
}
449
457
0 commit comments