forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetrics.go
More file actions
848 lines (748 loc) · 34.6 KB
/
metrics.go
File metadata and controls
848 lines (748 loc) · 34.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"context"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
compbasemetrics "k8s.io/component-base/metrics"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/metrics"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
metricsutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/metrics"
)
const (
InferenceObjectiveComponent = "inference_objective"
InferencePoolComponent = "inference_pool"
InferenceExtension = "inference_extension"
KVCacheUsagePercentKey = "KVCacheUsagePercent"
WaitingQueueSizeKey = "WaitingQueueSize"
MaxActiveModelsKey = "MaxActiveModels"
ActiveModelsKey = "ActiveModels"
WaitingModelsKey = "WaitingModels"
UpdateTimeKey = "UpdateTime"
)
var (
// Inference Objective Metrics
requestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_total",
Help: metricsutil.HelpMsgWithStability("Counter of inference objective requests broken out for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestErrCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_error_total",
Help: metricsutil.HelpMsgWithStability("Counter of inference objective requests errors broken out for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name", "error_code"},
)
requestTTFT = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_ttft_seconds",
Help: metricsutil.HelpMsgWithStability("Inference model TTFT distribution in seconds for each model and target model.", compbasemetrics.ALPHA),
Buckets: []float64{
0.005, 0.025, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.5, 2, 3,
4, 5, 6, 8, 10, 15, 20, 30, 45, 60, 120, 180, 240, 300, 360, 480, 600, 900, 1200, 1800, 2700, 3600,
},
},
[]string{"model_name", "target_model_name"},
)
requestTTFTGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_ttft_seconds_gauge",
Help: metricsutil.HelpMsgWithStability("Inference model TTFT gauge in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestPredictedTTFT = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_predicted_ttft_seconds",
Help: metricsutil.HelpMsgWithStability("Inference model Predicted TTFT distribution in seconds for each model and target model.", compbasemetrics.ALPHA),
Buckets: []float64{
0.005, 0.025, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.5, 2, 3,
4, 5, 6, 8, 10, 15, 20, 30, 45, 60, 120, 180, 240, 300, 360, 480, 600, 900, 1200, 1800, 2700, 3600,
},
},
[]string{"model_name", "target_model_name"},
)
requestPredictedTTFTGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_predicted_ttft_seconds_gauge",
Help: metricsutil.HelpMsgWithStability("Inference model Predicted TTFT gauge in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
// New metrics for TTFT prediction duration
requestTTFTPredictionDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_ttft_prediction_duration_seconds",
Help: metricsutil.HelpMsgWithStability("Duration taken to generate TTFT predictions in seconds for each model and target model.", compbasemetrics.ALPHA),
Buckets: []float64{
0.0001, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0,
},
},
[]string{"model_name", "target_model_name"},
)
requestTTFTPredictionDurationGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_ttft_prediction_duration_seconds_gauge",
Help: metricsutil.HelpMsgWithStability("Latest duration taken to generate TTFT predictions in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestTPOT = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_tpot_seconds",
Help: metricsutil.HelpMsgWithStability("Inference model TPOT distribution in seconds for each model and target model.", compbasemetrics.ALPHA),
Buckets: []float64{
0.0005, 0.00205, 0.005, 0.01, 0.02, 0.04, 0.06, 0.08, 0.1, 0.125, 0.15, 0.2, 0.3,
0.4, 0.5, 0.6, 0.8, 1, 1.5, 2, 3, 4.5, 6, 12, 18, 24, 30, 36, 48, 60, 90, 120, 180, 270, 360,
},
},
[]string{"model_name", "target_model_name"},
)
requestTPOTGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_tpot_seconds_gauge",
Help: metricsutil.HelpMsgWithStability("Inference model TPOT gauge in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestPredictedTPOT = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_predicted_tpot_seconds",
Help: metricsutil.HelpMsgWithStability("Inference model Predicted TPOT distribution in seconds for each model and target model.", compbasemetrics.ALPHA),
Buckets: []float64{
0.0005, 0.00205, 0.005, 0.01, 0.02, 0.04, 0.06, 0.08, 0.1, 0.125, 0.15, 0.2, 0.3,
0.4, 0.5, 0.6, 0.8, 1, 1.5, 2, 3, 4.5, 6, 12, 18, 24, 30, 36, 48, 60, 90, 120, 180, 270, 360,
},
},
[]string{"model_name", "target_model_name"},
)
requestPredictedTPOTGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_predicted_tpot_seconds_gauge",
Help: metricsutil.HelpMsgWithStability("Inference model Predicted TPOT gauge in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
// New metrics for TPOT prediction duration
requestTPOTPredictionDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_tpot_prediction_duration_seconds",
Help: metricsutil.HelpMsgWithStability("Duration taken to generate TPOT predictions in seconds for each model and target model.", compbasemetrics.ALPHA),
Buckets: []float64{
0.0001, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0,
},
},
[]string{"model_name", "target_model_name"},
)
requestTPOTPredictionDurationGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_tpot_prediction_duration_seconds_gauge",
Help: metricsutil.HelpMsgWithStability("Latest duration taken to generate TPOT predictions in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
// SLO Violation Metrics
requestTTFTSLOViolation = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_ttft_slo_violation",
Help: metricsutil.HelpMsgWithStability("Boolean indicator (0 or 1) of whether the last TTFT measurement violated the SLO threshold for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestTTFTSLOViolationCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_ttft_slo_violation_total",
Help: metricsutil.HelpMsgWithStability("Counter of TTFT SLO violations for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestTPOTSLOViolation = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_tpot_slo_violation",
Help: metricsutil.HelpMsgWithStability("Boolean indicator (0 or 1) of whether the last TPOT measurement violated the SLO threshold for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestTPOTSLOViolationCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_tpot_slo_violation_total",
Help: metricsutil.HelpMsgWithStability("Counter of TPOT SLO violations for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
// SLO threshold gauges (for dynamic threshold management)
requestTTFTSLOThreshold = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_ttft_slo_threshold_seconds",
Help: metricsutil.HelpMsgWithStability("Current TTFT SLO threshold in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestTPOTSLOThreshold = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_tpot_slo_threshold_seconds",
Help: metricsutil.HelpMsgWithStability("Current TPOT SLO threshold in seconds for each model and target model.", compbasemetrics.ALPHA),
},
[]string{"model_name", "target_model_name"},
)
requestLatencies = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_duration_seconds",
Help: metricsutil.HelpMsgWithStability("Inference objective response latency distribution in seconds for each model and target model.", compbasemetrics.ALPHA),
Buckets: []float64{
0.005, 0.025, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.5, 2, 3,
4, 5, 6, 8, 10, 15, 20, 30, 45, 60, 120, 180, 240, 300, 360, 480, 600, 900, 1200, 1800, 2700, 3600,
},
},
[]string{"model_name", "target_model_name"},
)
requestSizes = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "request_sizes",
Help: metricsutil.HelpMsgWithStability("Inference objective requests size distribution in bytes for each model and target model.", compbasemetrics.ALPHA),
// Use buckets ranging from 1000 bytes (1KB) to 10^9 bytes (1GB).
Buckets: []float64{
64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, // More fine-grained up to 64KB
131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, // Exponential up to 8MB
16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, // Exponential up to 1GB
},
},
[]string{"model_name", "target_model_name"},
)
responseSizes = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "response_sizes",
Help: metricsutil.HelpMsgWithStability("Inference objective responses size distribution in bytes for each model and target model.", compbasemetrics.ALPHA),
// Most models have a response token < 8192 tokens. Each token, in average, has 4 characters.
// 8192 * 4 = 32768.
Buckets: []float64{1, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32778, 65536},
},
[]string{"model_name", "target_model_name"},
)
inputTokens = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "input_tokens",
Help: metricsutil.HelpMsgWithStability("Inference objective input token count distribution for requests in each model.", compbasemetrics.ALPHA),
// Most models have a input context window less than 1 million tokens.
Buckets: []float64{1, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32778, 65536, 131072, 262144, 524288, 1048576},
},
[]string{"model_name", "target_model_name"},
)
outputTokens = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "output_tokens",
Help: metricsutil.HelpMsgWithStability("Inference objective output token count distribution for requests in each model.", compbasemetrics.ALPHA),
// Most models generates output less than 8192 tokens.
Buckets: []float64{1, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192},
},
[]string{"model_name", "target_model_name"},
)
promptCachedTokens = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "prompt_cached_tokens",
Help: metricsutil.HelpMsgWithStability("Inference objective prompt cached token count distribution for requests in each model.", compbasemetrics.ALPHA),
// Most models have a input context window less than 1 million tokens.
Buckets: []float64{1, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32778, 65536, 131072, 262144, 524288, 1048576},
},
[]string{"model_name", "target_model_name"},
)
runningRequests = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceObjectiveComponent,
Name: "running_requests",
Help: metricsutil.HelpMsgWithStability("Inference objective number of running requests in each model.", compbasemetrics.ALPHA),
},
[]string{"model_name"},
)
// NTPOT - Normalized Time Per Output Token
NormalizedTimePerOutputToken = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceObjectiveComponent,
Name: "normalized_time_per_output_token_seconds",
Help: metricsutil.HelpMsgWithStability("Inference objective latency divided by number of output tokens in seconds for each model and target model.", compbasemetrics.ALPHA),
// From few milliseconds per token to multiple seconds per token
Buckets: []float64{
0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0,
},
},
[]string{"model_name", "target_model_name"},
)
// Inference Pool Metrics
inferencePoolAvgKVCache = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferencePoolComponent,
Name: "average_kv_cache_utilization",
Help: metricsutil.HelpMsgWithStability("The average kv cache utilization for an inference server pool.", compbasemetrics.ALPHA),
},
[]string{"name"},
)
inferencePoolAvgQueueSize = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferencePoolComponent,
Name: "average_queue_size",
Help: metricsutil.HelpMsgWithStability("The average number of requests pending in the model server queue.", compbasemetrics.ALPHA),
},
[]string{"name"},
)
inferencePoolReadyPods = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferencePoolComponent,
Name: "ready_pods",
Help: metricsutil.HelpMsgWithStability("The number of ready pods in the inference server pool.", compbasemetrics.ALPHA),
},
[]string{"name"},
)
// Scheduler Metrics
SchedulerE2ELatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceExtension,
Name: "scheduler_e2e_duration_seconds",
Help: metricsutil.HelpMsgWithStability("End-to-end scheduling latency distribution in seconds.", compbasemetrics.ALPHA),
Buckets: []float64{
0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1,
},
// StabilityLevel: prometheus.ALPHA,
},
[]string{},
)
PluginProcessingLatencies = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceExtension,
Name: "plugin_duration_seconds",
Help: metricsutil.HelpMsgWithStability("Plugin processing latency distribution in seconds for each extension point, plugin type and plugin name.", compbasemetrics.ALPHA),
Buckets: []float64{
0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1,
},
},
[]string{"extension_point", "plugin_type", "plugin_name"},
)
// Prefix indexer Metrics
PrefixCacheSize = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceExtension,
Name: "prefix_indexer_size",
Help: metricsutil.HelpMsgWithStability("Size of the prefix indexer.", compbasemetrics.ALPHA),
},
[]string{},
)
PrefixCacheHitRatio = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceExtension,
Name: "prefix_indexer_hit_ratio",
Help: metricsutil.HelpMsgWithStability("Ratio of prefix length matched to total prefix length in the cache lookup.", compbasemetrics.ALPHA),
// Buckets from 0.0 to 1.0 in increments
Buckets: []float64{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0},
},
[]string{},
)
PrefixCacheHitLength = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceExtension,
Name: "prefix_indexer_hit_bytes",
Help: metricsutil.HelpMsgWithStability("Length of the prefix match in number of bytes in the cache lookup.", compbasemetrics.ALPHA),
Buckets: []float64{0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536},
},
[]string{},
)
// Info Metrics
InferenceExtensionInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceExtension,
Name: "info",
Help: metricsutil.HelpMsgWithStability("General information of the current build of Inference Extension.", compbasemetrics.ALPHA),
},
[]string{"commit", "build_ref"},
)
// Flow Control Metrics
flowControlRequestQueueDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: InferenceExtension,
Name: "flow_control_request_queue_duration_seconds",
Help: metricsutil.HelpMsgWithStability("Distribution of the total time requests spend in the EPP flow control layer, measured from the start of the EnqueueAndWait call until a final outcome is reached.", compbasemetrics.ALPHA),
Buckets: []float64{
0.0001, 0.0005, 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0, 60.0,
},
},
[]string{"fairness_id", "priority", "outcome"},
)
flowControlQueueSize = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Subsystem: InferenceExtension,
Name: "flow_control_queue_size",
Help: metricsutil.HelpMsgWithStability("Current number of requests being actively managed by the EPP flow control layer, from the start of the EnqueueAndWait call until a final outcome is reached.", compbasemetrics.ALPHA),
},
[]string{"fairness_id", "priority"},
)
)
var registerMetrics sync.Once
// Register all metrics.
func Register(customCollectors ...prometheus.Collector) {
registerMetrics.Do(func() {
metrics.Registry.MustRegister(requestTPOT)
metrics.Registry.MustRegister(requestTTFT)
metrics.Registry.MustRegister(requestTPOTGauge)
metrics.Registry.MustRegister(requestTTFTGauge)
metrics.Registry.MustRegister(requestPredictedTPOT)
metrics.Registry.MustRegister(requestPredictedTTFT)
metrics.Registry.MustRegister(requestPredictedTPOTGauge)
metrics.Registry.MustRegister(requestPredictedTTFTGauge)
// Register new prediction duration metrics
metrics.Registry.MustRegister(requestTPOTPredictionDuration)
metrics.Registry.MustRegister(requestTPOTPredictionDurationGauge)
metrics.Registry.MustRegister(requestTTFTPredictionDuration)
metrics.Registry.MustRegister(requestTTFTPredictionDurationGauge)
// Register SLO violation metrics
metrics.Registry.MustRegister(requestTTFTSLOViolation)
metrics.Registry.MustRegister(requestTTFTSLOViolationCounter)
metrics.Registry.MustRegister(requestTPOTSLOViolation)
metrics.Registry.MustRegister(requestTPOTSLOViolationCounter)
metrics.Registry.MustRegister(requestTTFTSLOThreshold)
metrics.Registry.MustRegister(requestTPOTSLOThreshold)
metrics.Registry.MustRegister(requestCounter)
metrics.Registry.MustRegister(requestErrCounter)
metrics.Registry.MustRegister(requestLatencies)
metrics.Registry.MustRegister(requestSizes)
metrics.Registry.MustRegister(responseSizes)
metrics.Registry.MustRegister(inputTokens)
metrics.Registry.MustRegister(outputTokens)
metrics.Registry.MustRegister(promptCachedTokens)
metrics.Registry.MustRegister(runningRequests)
metrics.Registry.MustRegister(NormalizedTimePerOutputToken)
metrics.Registry.MustRegister(inferencePoolAvgKVCache)
metrics.Registry.MustRegister(inferencePoolAvgQueueSize)
metrics.Registry.MustRegister(inferencePoolReadyPods)
metrics.Registry.MustRegister(SchedulerE2ELatency)
metrics.Registry.MustRegister(PluginProcessingLatencies)
metrics.Registry.MustRegister(InferenceExtensionInfo)
metrics.Registry.MustRegister(PrefixCacheSize)
metrics.Registry.MustRegister(PrefixCacheHitRatio)
metrics.Registry.MustRegister(PrefixCacheHitLength)
metrics.Registry.MustRegister(flowControlRequestQueueDuration)
metrics.Registry.MustRegister(flowControlQueueSize)
for _, collector := range customCollectors {
metrics.Registry.MustRegister(collector)
}
})
}
// Just for integration test
func Reset() {
requestCounter.Reset()
requestErrCounter.Reset()
requestLatencies.Reset()
requestSizes.Reset()
responseSizes.Reset()
inputTokens.Reset()
outputTokens.Reset()
promptCachedTokens.Reset()
runningRequests.Reset()
NormalizedTimePerOutputToken.Reset()
inferencePoolAvgKVCache.Reset()
inferencePoolAvgQueueSize.Reset()
inferencePoolReadyPods.Reset()
SchedulerE2ELatency.Reset()
PluginProcessingLatencies.Reset()
InferenceExtensionInfo.Reset()
PrefixCacheSize.Reset()
PrefixCacheHitRatio.Reset()
PrefixCacheHitLength.Reset()
flowControlRequestQueueDuration.Reset()
flowControlQueueSize.Reset()
requestTPOT.Reset()
requestTTFT.Reset()
requestTPOTGauge.Reset()
requestTTFTGauge.Reset()
requestPredictedTPOT.Reset()
requestPredictedTTFT.Reset()
requestPredictedTPOTGauge.Reset()
requestPredictedTTFTGauge.Reset()
// Reset new prediction duration metrics
requestTPOTPredictionDuration.Reset()
requestTPOTPredictionDurationGauge.Reset()
requestTTFTPredictionDuration.Reset()
requestTTFTPredictionDurationGauge.Reset()
// Reset SLO violation metrics
requestTTFTSLOViolation.Reset()
requestTTFTSLOViolationCounter.Reset()
requestTPOTSLOViolation.Reset()
requestTPOTSLOViolationCounter.Reset()
requestTTFTSLOThreshold.Reset()
requestTPOTSLOThreshold.Reset()
}
// RecordRequstCounter records the number of requests.
func RecordRequestCounter(modelName, targetModelName string) {
requestCounter.WithLabelValues(modelName, targetModelName).Inc()
}
// RecordRequestErrCounter records the number of error requests.
func RecordRequestErrCounter(modelName, targetModelName string, code string) {
if code != "" {
requestErrCounter.WithLabelValues(modelName, targetModelName, code).Inc()
}
}
// RecordRequestSizes records the request sizes.
func RecordRequestSizes(modelName, targetModelName string, reqSize int) {
requestSizes.WithLabelValues(modelName, targetModelName).Observe(float64(reqSize))
}
// RecordRequestLatencies records duration of request.
func RecordRequestLatencies(ctx context.Context, modelName, targetModelName string, received time.Time, complete time.Time) bool {
if !complete.After(received) {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "Request latency values are invalid",
"modelName", modelName, "targetModelName", targetModelName, "completeTime", complete, "receivedTime", received)
return false
}
elapsedSeconds := complete.Sub(received).Seconds()
requestLatencies.WithLabelValues(modelName, targetModelName).Observe(elapsedSeconds)
return true
}
func RecordRequestTPOT(ctx context.Context, modelName, targetModelName string, tpot float64) bool {
if tpot < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "TPOT value must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "tpot", tpot)
return false
}
requestTPOT.WithLabelValues(modelName, targetModelName).Observe(tpot)
requestTPOTGauge.WithLabelValues(modelName, targetModelName).Set(tpot)
return true
}
// RecordRequestTPOTWithSLO records TPOT and checks for SLO violation.
// If tpot exceeds the threshold, it records a violation (sets gauge to 1 and increments counter).
// If tpot is within limits, it sets gauge to 0.
func RecordRequestTPOTWithSLO(ctx context.Context, modelName, targetModelName string, tpot float64, sloThreshold float64) bool {
if tpot < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "TPOT value must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "tpot", tpot)
return false
}
// Check for SLO violation (tpot exceeds threshold)
if tpot > sloThreshold {
requestTPOTSLOViolation.WithLabelValues(modelName, targetModelName).Set(1)
requestTPOTSLOViolationCounter.WithLabelValues(modelName, targetModelName).Inc()
log.FromContext(ctx).V(logutil.DEFAULT).Info("TPOT SLO violation detected",
"modelName", modelName, "targetModelName", targetModelName, "tpot", tpot, "threshold", sloThreshold)
} else {
requestTPOTSLOViolation.WithLabelValues(modelName, targetModelName).Set(0)
}
return true
}
// TPOT records duration of request.
func RecordRequestPredictedTPOT(ctx context.Context, modelName, targetModelName string, predicted_tpot float64) bool {
if predicted_tpot < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "Predicted TPOT value must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "tpot", predicted_tpot)
return false
}
requestPredictedTPOT.WithLabelValues(modelName, targetModelName).Observe(predicted_tpot)
requestPredictedTPOTGauge.WithLabelValues(modelName, targetModelName).Set(predicted_tpot)
return true
}
// RecordRequestTPOTPredictionDuration records the duration taken to generate TPOT predictions.
func RecordRequestTPOTPredictionDuration(ctx context.Context, modelName, targetModelName string, duration float64) bool {
if duration < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "TPOT prediction duration must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "duration", duration)
return false
}
requestTPOTPredictionDuration.WithLabelValues(modelName, targetModelName).Observe(duration)
requestTPOTPredictionDurationGauge.WithLabelValues(modelName, targetModelName).Set(duration)
return true
}
// TTFT records duration of request.
func RecordRequestTTFT(ctx context.Context, modelName, targetModelName string, ttft float64) bool {
if ttft < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "TTFT value must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "ttft", ttft)
return false
}
requestTTFT.WithLabelValues(modelName, targetModelName).Observe(ttft)
requestTTFTGauge.WithLabelValues(modelName, targetModelName).Set(ttft)
return true
}
// RecordRequestTTFTWithSLO records TTFT and checks for SLO violation.
// If ttft exceeds the threshold, it records a violation (sets gauge to 1 and increments counter).
// If ttft is within limits, it sets gauge to 0.
func RecordRequestTTFTWithSLO(ctx context.Context, modelName, targetModelName string, ttft float64, sloThreshold float64) bool {
if ttft < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "TTFT value must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "ttft", ttft)
return false
}
// Check for SLO violation (ttft exceeds threshold)
if ttft > sloThreshold {
requestTTFTSLOViolation.WithLabelValues(modelName, targetModelName).Set(1)
requestTTFTSLOViolationCounter.WithLabelValues(modelName, targetModelName).Inc()
log.FromContext(ctx).V(logutil.DEFAULT).Info("TTFT SLO violation detected",
"modelName", modelName, "targetModelName", targetModelName, "ttft", ttft, "threshold", sloThreshold)
} else {
requestTTFTSLOViolation.WithLabelValues(modelName, targetModelName).Set(0)
}
return true
}
// TPOT records duration of request.
func RecordRequestPredictedTTFT(ctx context.Context, modelName, targetModelName string, predicted_ttft float64) bool {
if predicted_ttft < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "Predicted TTFT value must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "ttft", predicted_ttft)
return false
}
requestPredictedTTFT.WithLabelValues(modelName, targetModelName).Observe(predicted_ttft)
requestPredictedTTFTGauge.WithLabelValues(modelName, targetModelName).Set(predicted_ttft)
return true
}
// RecordRequestTTFTPredictionDuration records the duration taken to generate TTFT predictions.
func RecordRequestTTFTPredictionDuration(ctx context.Context, modelName, targetModelName string, duration float64) bool {
if duration < 0 {
log.FromContext(ctx).V(logutil.DEFAULT).Error(nil, "TTFT prediction duration must be non-negative",
"modelName", modelName, "targetModelName", targetModelName, "duration", duration)
return false
}
requestTTFTPredictionDuration.WithLabelValues(modelName, targetModelName).Observe(duration)
requestTTFTPredictionDurationGauge.WithLabelValues(modelName, targetModelName).Set(duration)
return true
}
// RecordResponseSizes records the response sizes.
func RecordResponseSizes(modelName, targetModelName string, size int) {
responseSizes.WithLabelValues(modelName, targetModelName).Observe(float64(size))
}
// RecordInputTokens records input tokens count.
func RecordInputTokens(modelName, targetModelName string, size int) {
if size > 0 {
inputTokens.WithLabelValues(modelName, targetModelName).Observe(float64(size))
}
}
// RecordOutputTokens records output tokens count.
func RecordOutputTokens(modelName, targetModelName string, size int) {
if size > 0 {
outputTokens.WithLabelValues(modelName, targetModelName).Observe(float64(size))
}
}
// RecordPromptCachedTokens records prompt cached tokens count.
func RecordPromptCachedTokens(modelName, targetModelName string, size int) {
promptCachedTokens.WithLabelValues(modelName, targetModelName).Observe(float64(size))
}
// RecordNormalizedTimePerOutputToken (NTPOT) records the normalized time per output token.
func RecordNormalizedTimePerOutputToken(ctx context.Context, modelName, targetModelName string, received time.Time, complete time.Time, outputTokenCount int) bool {
if !complete.After(received) {
log.FromContext(ctx).Error(nil, "Request latency values are invalid for NTPOT calculation",
"modelName", modelName, "targetModelName", targetModelName, "completeTime", complete, "receivedTime", received)
return false
}
if outputTokenCount <= 0 {
log.FromContext(ctx).Error(nil, "Output token count must be positive for NTPOT calculation",
"modelName", modelName, "targetModelName", targetModelName, "outputTokenCount", outputTokenCount)
return false
}
elapsedSeconds := complete.Sub(received).Seconds()
secondsPerToken := elapsedSeconds / float64(outputTokenCount)
NormalizedTimePerOutputToken.WithLabelValues(modelName, targetModelName).Observe(secondsPerToken)
return true
}
// IncRunningRequests increases the current running requests.
func IncRunningRequests(modelName string) {
if modelName != "" {
runningRequests.WithLabelValues(modelName).Inc()
}
}
// DecRunningRequests decreases the current running requests.
func DecRunningRequests(modelName string) {
if modelName != "" {
runningRequests.WithLabelValues(modelName).Dec()
}
}
func RecordInferencePoolAvgKVCache(name string, utilization float64) {
inferencePoolAvgKVCache.WithLabelValues(name).Set(utilization)
}
func RecordInferencePoolAvgQueueSize(name string, queueSize float64) {
inferencePoolAvgQueueSize.WithLabelValues(name).Set(queueSize)
}
func RecordInferencePoolReadyPods(name string, runningPods float64) {
inferencePoolReadyPods.WithLabelValues(name).Set(runningPods)
}
// RecordSchedulerE2ELatency records the end-to-end scheduling latency.
func RecordSchedulerE2ELatency(duration time.Duration) {
SchedulerE2ELatency.WithLabelValues().Observe(duration.Seconds())
}
// RecordPluginProcessingLatency records the processing latency for a plugin.
func RecordPluginProcessingLatency(extensionPoint, pluginType, pluginName string, duration time.Duration) {
PluginProcessingLatencies.WithLabelValues(extensionPoint, pluginType, pluginName).Observe(duration.Seconds())
}
// RecordPrefixCacheSize records the size of the prefix indexer in megabytes.
func RecordPrefixCacheSize(size int64) {
PrefixCacheSize.WithLabelValues().Set(float64(size))
}
// RecordPrefixCacheMatch records both the hit ratio and hit length for a prefix indexer match.
// matchedLength is the number of characters that matched, and totalLength is the total prefix length.
func RecordPrefixCacheMatch(matchedLength, totalLength int) {
// Record the hit length metric
PrefixCacheHitLength.WithLabelValues().Observe(float64(matchedLength))
// Record the hit ratio metric if totalLength is positive
if totalLength > 0 {
ratio := float64(matchedLength) / float64(totalLength)
PrefixCacheHitRatio.WithLabelValues().Observe(ratio)
}
}
func RecordInferenceExtensionInfo(commitSha, buildRef string) {
InferenceExtensionInfo.WithLabelValues(commitSha, buildRef).Set(1)
}
// RecordFlowControlRequestQueueDuration records the duration a request spent in the Flow Control layer.
func RecordFlowControlRequestQueueDuration(fairnessID, priority, outcome string, duration time.Duration) {
flowControlRequestQueueDuration.WithLabelValues(fairnessID, priority, outcome).Observe(duration.Seconds())
}
// IncFlowControlQueueSize increments the Flow Control queue size gauge.
func IncFlowControlQueueSize(fairnessID, priority string) {
flowControlQueueSize.WithLabelValues(fairnessID, priority).Inc()
}
// DecFlowControlQueueSize decrements the Flow Control queue size gauge.
func DecFlowControlQueueSize(fairnessID, priority string) {
flowControlQueueSize.WithLabelValues(fairnessID, priority).Dec()
}
// SetTTFTSLOThreshold sets the TTFT SLO threshold for a model.
// This allows dynamic threshold management and makes the threshold visible in metrics.
func SetTTFTSLOThreshold(modelName, targetModelName string, threshold float64) {
requestTTFTSLOThreshold.WithLabelValues(modelName, targetModelName).Set(threshold)
}
// SetTPOTSLOThreshold sets the TPOT SLO threshold for a model.
// This allows dynamic threshold management and makes the threshold visible in metrics.
func SetTPOTSLOThreshold(modelName, targetModelName string, threshold float64) {
requestTPOTSLOThreshold.WithLabelValues(modelName, targetModelName).Set(threshold)
}