|
| 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 | + "time" |
| 22 | + |
| 23 | + "github.com/go-logr/logr" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/api/v1alpha2" |
| 26 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics" |
| 27 | + logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // Note currently the EPP treats stale metrics same as fresh. |
| 32 | + // TODO: https://github.com/kubernetes-sigs/gateway-api-inference-extension/issues/336 |
| 33 | + metricsValidityPeriod = 5 * time.Second |
| 34 | +) |
| 35 | + |
| 36 | +type Datastore interface { |
| 37 | + PoolGet() (*v1alpha2.InferencePool, error) |
| 38 | + // PodMetrics operations |
| 39 | + // PodGetAll returns all pods and metrics, including fresh and stale. |
| 40 | + PodGetAll() []PodMetrics |
| 41 | + PodList(func(PodMetrics) bool) []PodMetrics |
| 42 | +} |
| 43 | + |
| 44 | +// StartMetricsLogger starts goroutines to 1) Print metrics debug logs if the DEBUG log level is |
| 45 | +// enabled; 2) flushes Prometheus metrics about the backend servers. |
| 46 | +func StartMetricsLogger(ctx context.Context, datastore Datastore, refreshPrometheusMetricsInterval time.Duration) { |
| 47 | + logger := log.FromContext(ctx) |
| 48 | + |
| 49 | + // Periodically flush prometheus metrics for inference pool |
| 50 | + go func() { |
| 51 | + for { |
| 52 | + select { |
| 53 | + case <-ctx.Done(): |
| 54 | + logger.V(logutil.DEFAULT).Info("Shutting down prometheus metrics thread") |
| 55 | + return |
| 56 | + default: |
| 57 | + time.Sleep(refreshPrometheusMetricsInterval) |
| 58 | + flushPrometheusMetricsOnce(logger, datastore) |
| 59 | + } |
| 60 | + } |
| 61 | + }() |
| 62 | + |
| 63 | + // Periodically print out the pods and metrics for DEBUGGING. |
| 64 | + if logger := logger.V(logutil.DEBUG); logger.Enabled() { |
| 65 | + go func() { |
| 66 | + for { |
| 67 | + select { |
| 68 | + case <-ctx.Done(): |
| 69 | + logger.V(logutil.DEFAULT).Info("Shutting down metrics logger thread") |
| 70 | + return |
| 71 | + default: |
| 72 | + time.Sleep(5 * time.Second) |
| 73 | + podsWithFreshMetrics := datastore.PodList(func(pm PodMetrics) bool { |
| 74 | + return time.Since(pm.GetMetrics().UpdateTime) <= metricsValidityPeriod |
| 75 | + }) |
| 76 | + podsWithStaleMetrics := datastore.PodList(func(pm PodMetrics) bool { |
| 77 | + return time.Since(pm.GetMetrics().UpdateTime) > metricsValidityPeriod |
| 78 | + }) |
| 79 | + logger.Info("Current Pods and metrics gathered", "fresh metrics", podsWithFreshMetrics, "stale metrics", podsWithStaleMetrics) |
| 80 | + } |
| 81 | + } |
| 82 | + }() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func flushPrometheusMetricsOnce(logger logr.Logger, datastore Datastore) { |
| 87 | + pool, err := datastore.PoolGet() |
| 88 | + if err != nil { |
| 89 | + // No inference pool or not initialize. |
| 90 | + logger.V(logutil.VERBOSE).Info("pool is not initialized, skipping flushing metrics") |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + var kvCacheTotal float64 |
| 95 | + var queueTotal int |
| 96 | + |
| 97 | + podMetrics := datastore.PodGetAll() |
| 98 | + logger.V(logutil.VERBOSE).Info("Flushing Prometheus Metrics", "ReadyPods", len(podMetrics)) |
| 99 | + if len(podMetrics) == 0 { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + for _, pod := range podMetrics { |
| 104 | + kvCacheTotal += pod.GetMetrics().KVCacheUsagePercent |
| 105 | + queueTotal += pod.GetMetrics().WaitingQueueSize |
| 106 | + } |
| 107 | + |
| 108 | + podTotalCount := len(podMetrics) |
| 109 | + metrics.RecordInferencePoolAvgKVCache(pool.Name, kvCacheTotal/float64(podTotalCount)) |
| 110 | + metrics.RecordInferencePoolAvgQueueSize(pool.Name, float64(queueTotal/podTotalCount)) |
| 111 | +} |
0 commit comments