Skip to content

Commit d5bdba1

Browse files
committed
Add ignoreUnavailable and defaultValue for MetricsAPI scaler
1 parent ef7f715 commit d5bdba1

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

pkg/scalers/metrics_api_scaler.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type metricsAPIScaler struct {
3535
type metricsAPIScalerMetadata struct {
3636
targetValue float64
3737
activationTargetValue float64
38+
ignoreUnavailable bool
39+
defaultValue float64
3840
url string
3941
format APIFormat
4042
valueLocation string
@@ -156,6 +158,24 @@ func parseMetricsAPIMetadata(config *scalersconfig.ScalerConfig) (*metricsAPISca
156158
meta.activationTargetValue = activationTargetValue
157159
}
158160

161+
meta.ignoreUnavailable = false
162+
if val, ok := config.TriggerMetadata["ignoreUnavailable"]; ok {
163+
ignoreUnavailable, err := strconv.ParseBool(val)
164+
if err != nil {
165+
return nil, fmt.Errorf("targetValue parsing error %w", err)
166+
}
167+
meta.ignoreUnavailable = ignoreUnavailable
168+
}
169+
170+
meta.defaultValue = 0
171+
if val, ok := config.TriggerMetadata["defaultValue"]; ok {
172+
defaultValue, err := strconv.ParseFloat(val, 64)
173+
if err != nil {
174+
return nil, fmt.Errorf("defaultValue parsing error %w", err)
175+
}
176+
meta.defaultValue = defaultValue
177+
}
178+
159179
if val, ok := config.TriggerMetadata["url"]; ok {
160180
meta.url = val
161181
} else {
@@ -474,13 +494,18 @@ func (s *metricsAPIScaler) GetMetricSpecForScaling(context.Context) []v2.MetricS
474494
// GetMetricsAndActivity returns value for a supported metric and an error if there is a problem getting the metric
475495
func (s *metricsAPIScaler) GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
476496
val, err := s.getMetricValue(ctx)
497+
477498
if err != nil {
478-
return []external_metrics.ExternalMetricValue{}, false, fmt.Errorf("error requesting metrics endpoint: %w", err)
499+
if !s.metadata.ignoreUnavailable {
500+
return []external_metrics.ExternalMetricValue{}, false, fmt.Errorf("error requesting metrics endpoint: %w", err)
501+
} else {
502+
metric := GenerateMetricInMili(metricName, s.metadata.defaultValue)
503+
return []external_metrics.ExternalMetricValue{metric}, val > s.metadata.activationTargetValue, nil
504+
}
505+
} else {
506+
metric := GenerateMetricInMili(metricName, val)
507+
return []external_metrics.ExternalMetricValue{metric}, val > s.metadata.activationTargetValue, nil
479508
}
480-
481-
metric := GenerateMetricInMili(metricName, val)
482-
483-
return []external_metrics.ExternalMetricValue{metric}, val > s.metadata.activationTargetValue, nil
484509
}
485510

486511
func getMetricAPIServerRequest(ctx context.Context, meta *metricsAPIScalerMetadata) (*http.Request, error) {

tests/scalers/metrics_api/metrics_api_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ spec:
161161
metadata:
162162
targetValue: "5"
163163
activationTargetValue: "20"
164+
ignoreUnavailable: "true"
165+
defaultValue: "0"
164166
url: "{{.MetricsServerEndpoint}}"
165167
valueLocation: 'value'
166168
authMode: "basic"

0 commit comments

Comments
 (0)