|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "strconv" |
| 24 | + |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + MetricsDataSourceType = "metrics-data-source" |
| 30 | + MetricsExtractorType = "model-server-protocol-metrics" |
| 31 | +) |
| 32 | + |
| 33 | +// Configuration parameters for metrics data source and extractor. |
| 34 | +type ( |
| 35 | + // Data source configuration parameters |
| 36 | + metricsDatasourceParams struct { |
| 37 | + // Scheme defines the protocol scheme used in metrics retrieval (e.g., "http"). |
| 38 | + Scheme string // `json:"scheme"` |
| 39 | + // Path defines the URL path used in metrics retrieval (e.g., "/metrics"). |
| 40 | + Path string // `json:"path"` |
| 41 | + // InsecureSkipVerify defines whether model server certificate should be verified or not. |
| 42 | + InsecureSkipVerify bool // `json:"insecureSkipVerify"` |
| 43 | + } |
| 44 | + |
| 45 | + // Extractor configuration parameters |
| 46 | + modelServerExtractorParams struct { |
| 47 | + // QueueRequestsSpec defines the metric specification string for retrieving queued request count. |
| 48 | + QueueRequestsSpec string // `json:"queuedRequestsSpec"` |
| 49 | + // RunningRequestsSpec defines the metric specification string for retrieving running requests count. |
| 50 | + RunningRequestsSpec string // `json:"runningRequestsSpec"` |
| 51 | + // KVUsage defines the metric specification string for retrieving KV cache usage. |
| 52 | + KVUsageSpec string // `json:"kvUsageSpec"` |
| 53 | + // LoRASpec defines the metric specification string for retrieving LoRA availability. |
| 54 | + LoRASpec string // `json:"loraSpec"` |
| 55 | + // CacheInfoSpec defines the metrics specification string for retrieving KV cache configuration. |
| 56 | + CacheInfoSpec string // `json:"cacheInfoSpec"` |
| 57 | + } |
| 58 | +) |
| 59 | + |
| 60 | +// MetricsDataSourceFactory is a factory function used to instantiate data layer's |
| 61 | +// metrics data source plugins specified in a configuration. |
| 62 | +func MetricsDataSourceFactory(name string, parameters json.RawMessage, handle plugins.Handle) (plugins.Plugin, error) { |
| 63 | + cfg, err := defaultDataSourceConfigParams() |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + if parameters != nil { // overlay the defaults with configured values |
| 69 | + if err := json.Unmarshal(parameters, cfg); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + ds := NewMetricsDataSource(cfg.Scheme, cfg.Path, cfg.InsecureSkipVerify) |
| 75 | + ds.typedName.Name = name |
| 76 | + return ds, nil |
| 77 | +} |
| 78 | + |
| 79 | +// ModelServerExtractorFactory is a factory function used to instantiate data layer's metrics |
| 80 | +// Extractor plugins specified in a configuration. |
| 81 | +func ModelServerExtractorFactory(name string, parameters json.RawMessage, handle plugins.Handle) (plugins.Plugin, error) { |
| 82 | + cfg, err := defaultExtractorConfigParams() |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + if parameters != nil { // overlay the defaults with configured values |
| 88 | + if err := json.Unmarshal(parameters, cfg); err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + extractor, err := NewModelServerExtractor(cfg.QueueRequestsSpec, cfg.RunningRequestsSpec, cfg.KVUsageSpec, |
| 94 | + cfg.LoRASpec, cfg.CacheInfoSpec) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + extractor.typedName.Name = name |
| 99 | + return extractor, nil |
| 100 | +} |
| 101 | + |
| 102 | +// Names of CLI flags in main |
| 103 | +// |
| 104 | +// TODO: |
| 105 | +// |
| 106 | +// 1. Consider having a cli package with all flag names and constants? |
| 107 | +// Can't use values from runserver as this creates an import cycle with datalayer. |
| 108 | +// Given that relevant issues/PRs have been closed so may be able to remove the cycle? |
| 109 | +// Comment from runserver package (regarding TestPodMetricsClient *backendmetrics.FakePodMetricsClient) |
| 110 | +// This should only be used in tests. We won't need this once we do not inject metrics in the tests. |
| 111 | +// TODO:(https://github.com/kubernetes-sigs/gateway-api-inference-extension/issues/432) Cleanup |
| 112 | +// |
| 113 | +// 2. Deprecation notice on these flags being moved to the configuration file |
| 114 | +const ( |
| 115 | + totalQueuedRequestsMetricSpecFlag = "total-queued-requests-metric" |
| 116 | + totalRunningRequestsMetricSpecFlag = "total-running-requests-metric" |
| 117 | + kvCacheUsagePercentageMetricSpecFlags = "kv-cache-usage-percentage-metric" |
| 118 | + loraInfoMetricSpecFlag = "lora-info-metric" |
| 119 | + cacheInfoMetricSpecFlag = "cache-info-metric" |
| 120 | + modelServerMetricsPathFlag = "model-server-metrics-path" |
| 121 | + modelServerMetricsSchemeFlag = "model-server-metrics-scheme" |
| 122 | + modelServerMetricsInsecureSkipVerifyFlag = "model-server-metrics-https-insecure-skip-verify" |
| 123 | +) |
| 124 | + |
| 125 | +// return the default configuration state. The defaults are populated from |
| 126 | +// existing command line flags. |
| 127 | +func defaultDataSourceConfigParams() (*metricsDatasourceParams, error) { |
| 128 | + var err error |
| 129 | + cfg := &metricsDatasourceParams{} |
| 130 | + |
| 131 | + if cfg.Scheme, err = fromStringFlag(modelServerMetricsSchemeFlag); err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + if cfg.Path, err = fromStringFlag(modelServerMetricsPathFlag); err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + if cfg.InsecureSkipVerify, err = fromBoolFlag(modelServerMetricsInsecureSkipVerifyFlag); err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + return cfg, nil |
| 141 | +} |
| 142 | + |
| 143 | +func defaultExtractorConfigParams() (*modelServerExtractorParams, error) { |
| 144 | + var err error |
| 145 | + cfg := &modelServerExtractorParams{} |
| 146 | + |
| 147 | + if cfg.QueueRequestsSpec, err = fromStringFlag(totalQueuedRequestsMetricSpecFlag); err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + if cfg.RunningRequestsSpec, err = fromStringFlag(totalRunningRequestsMetricSpecFlag); err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + if cfg.KVUsageSpec, err = fromStringFlag(kvCacheUsagePercentageMetricSpecFlags); err != nil { |
| 154 | + return nil, err |
| 155 | + } |
| 156 | + if cfg.LoRASpec, err = fromStringFlag(loraInfoMetricSpecFlag); err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + if cfg.CacheInfoSpec, err = fromStringFlag(cacheInfoMetricSpecFlag); err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + |
| 163 | + return cfg, nil |
| 164 | +} |
| 165 | + |
| 166 | +func fromStringFlag(name string) (string, error) { |
| 167 | + f := flag.Lookup(name) |
| 168 | + if f == nil { |
| 169 | + return "", fmt.Errorf("flag not found: %s", name) |
| 170 | + } |
| 171 | + return f.Value.String(), nil |
| 172 | +} |
| 173 | + |
| 174 | +func fromBoolFlag(name string) (bool, error) { |
| 175 | + f := flag.Lookup(name) |
| 176 | + if f == nil { |
| 177 | + return false, fmt.Errorf("flag not found: %s", name) |
| 178 | + } |
| 179 | + b, err := strconv.ParseBool(f.Value.String()) |
| 180 | + if err != nil { |
| 181 | + return false, fmt.Errorf("invalid bool flag %q: %w", name, err) |
| 182 | + } |
| 183 | + return b, nil |
| 184 | +} |
0 commit comments