|
| 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 26 | + |
| 27 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datalayer" |
| 28 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics" |
| 29 | + logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" |
| 30 | +) |
| 31 | + |
| 32 | +const debugPrintInterval = 5 * time.Second |
| 33 | + |
| 34 | +// StartMetricsLogger starts background goroutines for: |
| 35 | +// 1. Refreshing Prometheus metrics periodically |
| 36 | +// 2. Debug logging (if DEBUG level enabled) |
| 37 | +func StartMetricsLogger(ctx context.Context, datastore datalayer.PoolInfo, refreshInterval, stalenessThreshold time.Duration) { |
| 38 | + logger := log.FromContext(ctx) |
| 39 | + |
| 40 | + go runPrometheusRefresher(ctx, logger, datastore, refreshInterval, stalenessThreshold) |
| 41 | + |
| 42 | + if logger.V(logutil.DEBUG).Enabled() { |
| 43 | + go runDebugLogger(ctx, logger, datastore, stalenessThreshold) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func runPrometheusRefresher(ctx context.Context, logger logr.Logger, datastore datalayer.PoolInfo, interval, stalenessThreshold time.Duration) { |
| 48 | + ticker := time.NewTicker(interval) |
| 49 | + defer ticker.Stop() |
| 50 | + |
| 51 | + for { |
| 52 | + select { |
| 53 | + case <-ctx.Done(): |
| 54 | + logger.V(logutil.DEFAULT).Info("Shutting down prometheus metrics thread") |
| 55 | + return |
| 56 | + case <-ticker.C: |
| 57 | + refreshPrometheusMetrics(logger, datastore, stalenessThreshold) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func runDebugLogger(ctx context.Context, logger logr.Logger, datastore datalayer.PoolInfo, stalenessThreshold time.Duration) { |
| 63 | + ticker := time.NewTicker(debugPrintInterval) |
| 64 | + defer ticker.Stop() |
| 65 | + |
| 66 | + for { |
| 67 | + select { |
| 68 | + case <-ctx.Done(): |
| 69 | + logger.V(logutil.DEFAULT).Info("Shutting down metrics logger thread") |
| 70 | + return |
| 71 | + case <-ticker.C: |
| 72 | + printDebugMetrics(logger, datastore, stalenessThreshold) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func podsWithFreshMetrics(stalenessThreshold time.Duration) func(datalayer.Endpoint) bool { |
| 78 | + return func(ep datalayer.Endpoint) bool { |
| 79 | + if ep == nil { |
| 80 | + return false // Skip nil pods |
| 81 | + } |
| 82 | + return time.Since(ep.GetMetrics().UpdateTime) <= stalenessThreshold |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func podsWithStaleMetrics(stalenessThreshold time.Duration) func(datalayer.Endpoint) bool { |
| 87 | + return func(ep datalayer.Endpoint) bool { |
| 88 | + if ep == nil { |
| 89 | + return false // Skip nil pods |
| 90 | + } |
| 91 | + return time.Since(ep.GetMetrics().UpdateTime) > stalenessThreshold |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func printDebugMetrics(logger logr.Logger, datastore datalayer.PoolInfo, stalenessThreshold time.Duration) { |
| 96 | + freshPods := datastore.PodList(podsWithFreshMetrics(stalenessThreshold)) |
| 97 | + stalePods := datastore.PodList(podsWithStaleMetrics(stalenessThreshold)) |
| 98 | + |
| 99 | + message := fmt.Sprintf("Current Pods and metrics gathered. Fresh metrics: %+v, Stale metrics: %+v", |
| 100 | + freshPods, stalePods) |
| 101 | + logger.V(logutil.VERBOSE).Info(message) |
| 102 | +} |
| 103 | + |
| 104 | +func refreshPrometheusMetrics(logger logr.Logger, datastore datalayer.PoolInfo, stalenessThreshold time.Duration) { |
| 105 | + pool, err := datastore.PoolGet() |
| 106 | + if err != nil { |
| 107 | + logger.V(logutil.DEFAULT).Info("Pool is not initialized, skipping refreshing metrics") |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + podMetrics := datastore.PodList(podsWithFreshMetrics(stalenessThreshold)) |
| 112 | + logger.V(logutil.TRACE).Info("Refreshing Prometheus Metrics", "ReadyPods", len(podMetrics)) |
| 113 | + |
| 114 | + if len(podMetrics) == 0 { |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + totals := calculateTotals(podMetrics) |
| 119 | + podCount := len(podMetrics) |
| 120 | + |
| 121 | + metrics.RecordInferencePoolAvgKVCache(pool.Name, totals.kvCache/float64(podCount)) |
| 122 | + metrics.RecordInferencePoolAvgQueueSize(pool.Name, float64(totals.queueSize/podCount)) |
| 123 | + metrics.RecordInferencePoolReadyPods(pool.Name, float64(podCount)) |
| 124 | +} |
| 125 | + |
| 126 | +// totals holds aggregated metric values |
| 127 | +type totals struct { |
| 128 | + kvCache float64 |
| 129 | + queueSize int |
| 130 | +} |
| 131 | + |
| 132 | +func calculateTotals(endpoints []datalayer.Endpoint) totals { |
| 133 | + var result totals |
| 134 | + for _, pod := range endpoints { |
| 135 | + metrics := pod.GetMetrics() |
| 136 | + result.kvCache += metrics.KVCacheUsagePercent |
| 137 | + result.queueSize += metrics.WaitingQueueSize |
| 138 | + } |
| 139 | + return result |
| 140 | +} |
0 commit comments