forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics_spec.go
131 lines (112 loc) · 4.01 KB
/
metrics_spec.go
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
/*
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"
"fmt"
"strings"
"sigs.k8s.io/controller-runtime/pkg/log"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)
// MetricSpec represents a single metric's specification.
type MetricSpec struct {
MetricName string
Labels map[string]string // Label name -> Label value
}
// MetricMapping holds named MetricSpecs.
type MetricMapping struct {
TotalQueuedRequests *MetricSpec
KVCacheUtilization *MetricSpec
LoraRequestInfo *MetricSpec
}
// stringToMetricSpec converts a string to a MetricSpec.
// Example inputs:
//
// "metric_name"
// "metric_name{label1=value1}"
// "metric_name{label1=value1,label2=value2}"
func stringToMetricSpec(specStr string) (*MetricSpec, error) {
if specStr == "" {
return nil, nil // Allow empty strings to represent nil MetricSpecs
}
specStr = strings.TrimSpace(specStr)
metricName := specStr
labels := make(map[string]string)
// Check for labels enclosed in curly braces
start := strings.Index(specStr, "{")
end := strings.Index(specStr, "}")
if start != -1 || end != -1 { // If *either* brace is present...
if start == -1 || end == -1 || end <= start+1 { // ...check that *both* are present and correctly placed.
return nil, fmt.Errorf("invalid metric spec string: %q, missing or malformed label block", specStr)
}
metricName = strings.TrimSpace(specStr[:start])
labelStr := specStr[start+1 : end]
// Split into individual label pairs
labelPairs := strings.Split(labelStr, ",")
for _, pair := range labelPairs {
pair = strings.TrimSpace(pair)
parts := strings.Split(pair, "=")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid label pair: %q in metric spec: %q", pair, specStr)
}
labelName := strings.TrimSpace(parts[0])
labelValue := strings.TrimSpace(parts[1])
if labelName == "" || labelValue == "" {
return nil, fmt.Errorf("empty label name or value in pair: %q in metric spec: %q", pair, specStr)
}
labels[labelName] = labelValue
}
// Check for extra characters after labels
if end != len(specStr)-1 {
return nil, fmt.Errorf("invalid characters after label section in: %q", specStr)
}
}
if metricName == "" { // Metric name cannot be empty
return nil, fmt.Errorf("empty metric name in spec: %q", specStr)
}
return &MetricSpec{
MetricName: metricName,
Labels: labels,
}, nil
}
// NewMetricMapping creates a MetricMapping from string values.
func NewMetricMapping(ctx context.Context, queuedStr, kvUsageStr, loraReqInfoStr string) (*MetricMapping, error) {
queuedSpec, err := stringToMetricSpec(queuedStr)
if err != nil {
return nil, fmt.Errorf("error parsing WaitingRequests: %w", err)
}
kvUsageSpec, err := stringToMetricSpec(kvUsageStr)
if err != nil {
return nil, fmt.Errorf("error parsing KVCacheUsage: %w", err)
}
loraReqInfoSpec, err := stringToMetricSpec(loraReqInfoStr)
if err != nil {
return nil, fmt.Errorf("error parsing loraReqInfoStr: %w", err)
}
mapping := &MetricMapping{
TotalQueuedRequests: queuedSpec,
KVCacheUtilization: kvUsageSpec,
LoraRequestInfo: loraReqInfoSpec,
}
logger := log.FromContext(ctx)
if mapping.TotalQueuedRequests == nil {
logger.V(logutil.TRACE).Info("Not scraping metric: TotalQueuedRequests")
}
if mapping.KVCacheUtilization == nil {
logger.V(logutil.TRACE).Info("Not scraping metric: KVCacheUtilization")
}
if mapping.LoraRequestInfo == nil {
logger.V(logutil.TRACE).Info("Not scraping metric: LoraRequestInfo")
}
return mapping, nil
}