|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/feast-dev/feast/go/internal/feast/onlineserving" |
| 10 | + "github.com/feast-dev/feast/go/protos/feast/serving" |
| 11 | +) |
| 12 | + |
| 13 | +// extractFeatureView extracts the feature view name from a full feature name. |
| 14 | +// Feature names follow the format: feature_view__feature_name |
| 15 | +// Example: "hotel_fv__price" -> "hotel_fv" |
| 16 | +func extractFeatureView(featureName string) string { |
| 17 | + parts := strings.SplitN(featureName, "__", 2) |
| 18 | + if len(parts) == 2 { |
| 19 | + return parts[0] |
| 20 | + } |
| 21 | + return "unknown" |
| 22 | +} |
| 23 | + |
| 24 | +type LookupMetricsAggregator struct { |
| 25 | + notFound map[string]int64 |
| 26 | + nullOrExpired map[string]int64 |
| 27 | + project string |
| 28 | + onlineStore string |
| 29 | + client StatsdClient |
| 30 | + sampleRate float64 |
| 31 | +} |
| 32 | + |
| 33 | +func NewLookupMetricsAggregator( |
| 34 | + project, onlineStore string, |
| 35 | + client StatsdClient, |
| 36 | +) *LookupMetricsAggregator { |
| 37 | + if client == nil { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + // Read sampling rate from environment (default: 1.0 = no sampling) |
| 42 | + sampleRate := 1.0 |
| 43 | + if rateStr := os.Getenv("FEAST_METRICS_SAMPLE_RATE"); rateStr != "" { |
| 44 | + if rate, err := strconv.ParseFloat(rateStr, 64); err == nil { |
| 45 | + if rate > 0 && rate <= 1.0 { |
| 46 | + sampleRate = rate |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return &LookupMetricsAggregator{ |
| 52 | + notFound: make(map[string]int64), |
| 53 | + nullOrExpired: make(map[string]int64), |
| 54 | + project: project, |
| 55 | + onlineStore: onlineStore, |
| 56 | + client: client, |
| 57 | + sampleRate: sampleRate, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func (m *LookupMetricsAggregator) Record(featureID string, status serving.FieldStatus) { |
| 62 | + if m == nil { |
| 63 | + return |
| 64 | + } |
| 65 | + switch status { |
| 66 | + case serving.FieldStatus_NOT_FOUND: |
| 67 | + m.notFound[featureID]++ |
| 68 | + case serving.FieldStatus_NULL_VALUE, serving.FieldStatus_OUTSIDE_MAX_AGE: |
| 69 | + m.nullOrExpired[featureID]++ |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func (m *LookupMetricsAggregator) RecordFromFeatureVectors(vectors []*onlineserving.FeatureVector) { |
| 74 | + if m == nil { |
| 75 | + return |
| 76 | + } |
| 77 | + for _, vector := range vectors { |
| 78 | + for _, status := range vector.Statuses { |
| 79 | + m.Record(vector.Name, status) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (m *LookupMetricsAggregator) RecordFromRangeFeatureVectors(vectors []*onlineserving.RangeFeatureVector) { |
| 85 | + if m == nil { |
| 86 | + return |
| 87 | + } |
| 88 | + for _, vector := range vectors { |
| 89 | + for _, entityStatuses := range vector.RangeStatuses { |
| 90 | + for _, status := range entityStatuses { |
| 91 | + m.Record(vector.Name, status) |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func (m *LookupMetricsAggregator) Emit() { |
| 98 | + if m == nil || m.client == nil { |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + // Probabilistic sampling: skip this request's metrics based on sample_rate |
| 103 | + if m.sampleRate < 1.0 && rand.Float64() > m.sampleRate { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + // Calculate multiplier to preserve statistical accuracy |
| 108 | + // If sampleRate=0.1, we only emit 10% of the time, so multiply counts by 10 |
| 109 | + multiplier := 1.0 / m.sampleRate |
| 110 | + |
| 111 | + baseTags := []string{ |
| 112 | + "project:" + m.project, |
| 113 | + "online_store_type:" + m.onlineStore, |
| 114 | + } |
| 115 | + |
| 116 | + for featureID, count := range m.notFound { |
| 117 | + if count == 0 { |
| 118 | + continue |
| 119 | + } |
| 120 | + // Adjust count to preserve accuracy when sampling |
| 121 | + adjustedCount := int64(float64(count) * multiplier) |
| 122 | + tags := make([]string, len(baseTags)+2) |
| 123 | + copy(tags, baseTags) |
| 124 | + tags[len(baseTags)] = "feature:" + featureID |
| 125 | + tags[len(baseTags)+1] = "feature_view:" + extractFeatureView(featureID) |
| 126 | + m.client.Count("mlpfs.featureserver.feature_lookup_not_found", adjustedCount, tags, 1.0) |
| 127 | + } |
| 128 | + |
| 129 | + for featureID, count := range m.nullOrExpired { |
| 130 | + if count == 0 { |
| 131 | + continue |
| 132 | + } |
| 133 | + adjustedCount := int64(float64(count) * multiplier) |
| 134 | + tags := make([]string, len(baseTags)+2) |
| 135 | + copy(tags, baseTags) |
| 136 | + tags[len(baseTags)] = "feature:" + featureID |
| 137 | + tags[len(baseTags)+1] = "feature_view:" + extractFeatureView(featureID) |
| 138 | + m.client.Count("mlpfs.featureserver.feature_lookup_null_or_expired", adjustedCount, tags, 1.0) |
| 139 | + } |
| 140 | +} |
0 commit comments