|
| 1 | +/* |
| 2 | +Copyright 2026 Feast Community. |
| 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 provides a Prometheus info gauge that records the store |
| 18 | +// types configured for each FeatureStore CR (online store, offline store, |
| 19 | +// registry). These operator-level metrics are distinct from the Feast |
| 20 | +// feature-server application metrics (feast_feature_server_*) and are useful |
| 21 | +// for usage telemetry and assessing the impact of removing store type support. |
| 22 | +package metrics |
| 23 | + |
| 24 | +import ( |
| 25 | + feastdevv1 "github.com/feast-dev/feast/infra/feast-operator/api/v1" |
| 26 | + "github.com/prometheus/client_golang/prometheus" |
| 27 | + ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 28 | +) |
| 29 | + |
| 30 | +const typeNone = "none" |
| 31 | + |
| 32 | +// FeatureStoreMetrics holds the Prometheus GaugeVec for feast-operator |
| 33 | +// installation telemetry. |
| 34 | +type FeatureStoreMetrics struct { |
| 35 | + FeatureStoreInfo *prometheus.GaugeVec |
| 36 | +} |
| 37 | + |
| 38 | +// NewFeatureStoreMetrics creates a new FeatureStoreMetrics with the GaugeVec |
| 39 | +// initialised but not yet registered. Call Register() before starting the manager. |
| 40 | +func NewFeatureStoreMetrics() *FeatureStoreMetrics { |
| 41 | + return &FeatureStoreMetrics{ |
| 42 | + FeatureStoreInfo: prometheus.NewGaugeVec( |
| 43 | + prometheus.GaugeOpts{ |
| 44 | + Name: "feast_operator_feature_store_info", |
| 45 | + Help: "Information about a deployed FeatureStore. " + |
| 46 | + "Value is always 1. Labels carry the configured store types: " + |
| 47 | + "'online_store_type', 'offline_store_type', and 'registry_type' " + |
| 48 | + "are set to the persistence type (e.g. redis, snowflake.offline, local) " + |
| 49 | + "or 'none' when that component is not configured.", |
| 50 | + }, |
| 51 | + []string{"namespace", "name", "online_store_type", "offline_store_type", "registry_type"}, |
| 52 | + ), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Register registers the metric with the controller-runtime metrics registry |
| 57 | +// so it is exposed on the manager's /metrics endpoint. |
| 58 | +func (m *FeatureStoreMetrics) Register() { |
| 59 | + ctrlmetrics.Registry.MustRegister(m.FeatureStoreInfo) |
| 60 | +} |
| 61 | + |
| 62 | +// RecordFeatureStore updates the gauge for the given FeatureStore using the |
| 63 | +// applied configuration stored in status.Applied (which has operator defaults |
| 64 | +// applied). The previous label set for this FeatureStore is deleted first so |
| 65 | +// that store type changes are reflected cleanly on the next scrape. |
| 66 | +func (m *FeatureStoreMetrics) RecordFeatureStore(fs *feastdevv1.FeatureStore) { |
| 67 | + svcs := fs.Status.Applied.Services |
| 68 | + m.FeatureStoreInfo.DeletePartialMatch(prometheus.Labels{ |
| 69 | + "namespace": fs.Namespace, |
| 70 | + "name": fs.Name, |
| 71 | + }) |
| 72 | + m.FeatureStoreInfo.WithLabelValues( |
| 73 | + fs.Namespace, |
| 74 | + fs.Name, |
| 75 | + onlineStoreType(svcs), |
| 76 | + offlineStoreType(svcs), |
| 77 | + registryType(svcs), |
| 78 | + ).Set(1) |
| 79 | +} |
| 80 | + |
| 81 | +// DeleteFeatureStore removes the metric label set for the given FeatureStore. |
| 82 | +// Safe to call when the CR has already been deleted from the API server. |
| 83 | +func (m *FeatureStoreMetrics) DeleteFeatureStore(namespace, name string) { |
| 84 | + m.FeatureStoreInfo.DeletePartialMatch(prometheus.Labels{ |
| 85 | + "namespace": namespace, |
| 86 | + "name": name, |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +// onlineStoreType returns the online store persistence type or "none". |
| 91 | +func onlineStoreType(svcs *feastdevv1.FeatureStoreServices) string { |
| 92 | + if svcs == nil || svcs.OnlineStore == nil { |
| 93 | + return typeNone |
| 94 | + } |
| 95 | + if p := svcs.OnlineStore.Persistence; p != nil && p.DBPersistence != nil { |
| 96 | + return p.DBPersistence.Type |
| 97 | + } |
| 98 | + return "file" |
| 99 | +} |
| 100 | + |
| 101 | +// offlineStoreType returns the offline store persistence type or "none". |
| 102 | +func offlineStoreType(svcs *feastdevv1.FeatureStoreServices) string { |
| 103 | + if svcs == nil || svcs.OfflineStore == nil { |
| 104 | + return typeNone |
| 105 | + } |
| 106 | + if p := svcs.OfflineStore.Persistence; p != nil { |
| 107 | + if p.DBPersistence != nil { |
| 108 | + return p.DBPersistence.Type |
| 109 | + } |
| 110 | + if p.FilePersistence != nil && p.FilePersistence.Type != "" { |
| 111 | + return p.FilePersistence.Type |
| 112 | + } |
| 113 | + } |
| 114 | + return "file" |
| 115 | +} |
| 116 | + |
| 117 | +// registryType returns "local", "remote", "remote_feastref", or "none". |
| 118 | +func registryType(svcs *feastdevv1.FeatureStoreServices) string { |
| 119 | + if svcs == nil || svcs.Registry == nil { |
| 120 | + return typeNone |
| 121 | + } |
| 122 | + switch { |
| 123 | + case svcs.Registry.Local != nil: |
| 124 | + return "local" |
| 125 | + case svcs.Registry.Remote != nil: |
| 126 | + if svcs.Registry.Remote.FeastRef != nil { |
| 127 | + return "remote_feastref" |
| 128 | + } |
| 129 | + return "remote" |
| 130 | + default: |
| 131 | + return typeNone |
| 132 | + } |
| 133 | +} |
0 commit comments