-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmetrics.go
More file actions
206 lines (178 loc) · 7.22 KB
/
metrics.go
File metadata and controls
206 lines (178 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package metrics
import (
"context"
"fmt"
"os"
llmdOptv1alpha1 "github.com/llm-d/llm-d-workload-variant-autoscaler/api/v1alpha1"
"github.com/llm-d/llm-d-workload-variant-autoscaler/internal/constants"
"github.com/prometheus/client_golang/prometheus"
)
// ControllerInstanceEnvVar is the environment variable name for controller instance label
const ControllerInstanceEnvVar = "CONTROLLER_INSTANCE"
var (
replicaScalingTotal *prometheus.CounterVec
desiredReplicas *prometheus.GaugeVec
currentReplicas *prometheus.GaugeVec
desiredRatio *prometheus.GaugeVec
optimizationDuration *prometheus.HistogramVec
modelsProcessedTotal prometheus.Counter
// controllerInstance stores the optional controller instance identifier.
// When set, it's added as a label to all emitted metrics.
controllerInstance string
)
// GetControllerInstance returns the configured controller instance label value
// Returns empty string if not configured
func GetControllerInstance() string {
return controllerInstance
}
// InitMetrics registers all custom metrics with the provided registry.
// This function should be called once during application startup from main().
// It reads CONTROLLER_INSTANCE from the environment to optionally add
// controller instance isolation labels to all emitted metrics.
func InitMetrics(registry prometheus.Registerer) error {
// Read controller instance from environment
controllerInstance = os.Getenv(ControllerInstanceEnvVar)
// Build label sets based on whether controller_instance is configured
baseLabels := []string{constants.LabelVariantName, constants.LabelNamespace, constants.LabelAcceleratorType}
scalingLabels := []string{constants.LabelVariantName, constants.LabelNamespace, constants.LabelDirection, constants.LabelReason}
if controllerInstance != "" {
baseLabels = append(baseLabels, constants.LabelControllerInstance)
scalingLabels = append(scalingLabels, constants.LabelControllerInstance)
}
replicaScalingTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: constants.WVAReplicaScalingTotal,
Help: "Total number of replica scaling operations",
},
scalingLabels,
)
desiredReplicas = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.WVADesiredReplicas,
Help: "Desired number of replicas for each variant",
},
baseLabels,
)
currentReplicas = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.WVACurrentReplicas,
Help: "Current number of replicas for each variant",
},
baseLabels,
)
desiredRatio = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: constants.WVADesiredRatio,
Help: "Ratio of the desired number of replicas and the current number of replicas for each variant",
},
baseLabels,
)
optimizationDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: constants.WVAOptimizationDurationSeconds,
Help: "Duration of optimization loop cycles in seconds",
Buckets: []float64{0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
},
[]string{constants.LabelStatus},
)
modelsProcessedTotal = prometheus.NewCounter(
prometheus.CounterOpts{
Name: constants.WVAModelsProcessedTotal,
Help: "Total number of models processed across optimization cycles",
},
)
// Register metrics with the registry
if err := registry.Register(replicaScalingTotal); err != nil {
return fmt.Errorf("failed to register replicaScalingTotal metric: %w", err)
}
if err := registry.Register(desiredReplicas); err != nil {
return fmt.Errorf("failed to register desiredReplicas metric: %w", err)
}
if err := registry.Register(currentReplicas); err != nil {
return fmt.Errorf("failed to register currentReplicas metric: %w", err)
}
if err := registry.Register(desiredRatio); err != nil {
return fmt.Errorf("failed to register desiredRatio metric: %w", err)
}
if err := registry.Register(optimizationDuration); err != nil {
return fmt.Errorf("failed to register optimizationDuration metric: %w", err)
}
if err := registry.Register(modelsProcessedTotal); err != nil {
return fmt.Errorf("failed to register modelsProcessedTotal metric: %w", err)
}
return nil
}
// InitMetricsAndEmitter registers metrics with Prometheus and creates a metrics emitter
// This is a convenience function that handles both registration and emitter creation
func InitMetricsAndEmitter(registry prometheus.Registerer) (*MetricsEmitter, error) {
if err := InitMetrics(registry); err != nil {
return nil, err
}
return NewMetricsEmitter(), nil
}
// MetricsEmitter handles emission of custom metrics
type MetricsEmitter struct{}
// NewMetricsEmitter creates a new metrics emitter
func NewMetricsEmitter() *MetricsEmitter {
return &MetricsEmitter{}
}
// EmitReplicaScalingMetrics emits metrics related to replica scaling
func (m *MetricsEmitter) EmitReplicaScalingMetrics(ctx context.Context, va *llmdOptv1alpha1.VariantAutoscaling, direction, reason string) error {
labels := prometheus.Labels{
constants.LabelVariantName: va.Name,
constants.LabelNamespace: va.Namespace,
constants.LabelDirection: direction,
constants.LabelReason: reason,
}
// Add controller_instance label if configured
if controllerInstance != "" {
labels[constants.LabelControllerInstance] = controllerInstance
}
// These operations are local and should never fail, but we handle errors for debugging
if replicaScalingTotal == nil {
return fmt.Errorf("replicaScalingTotal metric not initialized")
}
replicaScalingTotal.With(labels).Inc()
return nil
}
// ObserveOptimizationDuration records the duration of an optimization cycle with the given status.
// Status should be one of: "success", "error", "partial".
func (m *MetricsEmitter) ObserveOptimizationDuration(durationSeconds float64, status string) {
if optimizationDuration == nil {
return
}
optimizationDuration.With(prometheus.Labels{constants.LabelStatus: status}).Observe(durationSeconds)
}
// IncrModelsProcessed increments the models-processed counter by the given count.
func (m *MetricsEmitter) IncrModelsProcessed(count int) {
if modelsProcessedTotal == nil {
return
}
modelsProcessedTotal.Add(float64(count))
}
// EmitReplicaMetrics emits current and desired replica metrics
func (m *MetricsEmitter) EmitReplicaMetrics(ctx context.Context, va *llmdOptv1alpha1.VariantAutoscaling, current, desired int32, acceleratorType string) error {
baseLabels := prometheus.Labels{
constants.LabelVariantName: va.Name,
constants.LabelNamespace: va.Namespace,
constants.LabelAcceleratorType: acceleratorType,
}
// Add controller_instance label if configured
if controllerInstance != "" {
baseLabels[constants.LabelControllerInstance] = controllerInstance
}
// These operations are local and should never fail, but we handle errors for debugging
if currentReplicas == nil || desiredReplicas == nil || desiredRatio == nil {
return fmt.Errorf("replica metrics not initialized")
}
currentReplicas.With(baseLabels).Set(float64(current))
desiredReplicas.With(baseLabels).Set(float64(desired))
// Avoid division by 0 if current replicas is zero: set the ratio to the desired replicas
// Going 0 -> N is treated by using `desired_ratio = N`
if current == 0 {
desiredRatio.With(baseLabels).Set(float64(desired))
return nil
}
desiredRatio.With(baseLabels).Set(float64(desired) / float64(current))
return nil
}