Skip to content

Commit

Permalink
feat: add configurable fallback value when no metric value found
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Woodley <ben.woodley@y42.com>
  • Loading branch information
Caislear committed Jun 19, 2024
1 parent 48f02c7 commit 78765cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ Here is an overview of all new **experimental** features:
- TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))

### Improvements

- **GCP Pub/Sub**: Add optional valueIfNull to allow a default scaling value and prevent errors when GCP metric returns no value. ([#5896](https://github.com/kedacore/keda/issues/5896))
- **GCP Scalers**: Added custom time horizon in GCP scalers ([#5778](https://github.com/kedacore/keda/issues/5778))
- **GitHub Scaler**: Fixed pagination, fetching repository list ([#5738](https://github.com/kedacore/keda/issues/5738))
- **Kafka**: Fix logic to scale to zero on invalid offset even with earliest offsetResetPolicy ([#5689](https://github.com/kedacore/keda/issues/5689))


### Fixes

- **General**: Scalers are properly closed after being refreshed ([#5806](https://github.com/kedacore/keda/issues/5806))
Expand Down
7 changes: 5 additions & 2 deletions pkg/scalers/gcp/gcp_stackdriver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (s StackDriverClient) GetMetrics(
//
// MQL provides a more expressive query language than
// the current filtering options of GetMetrics
func (s StackDriverClient) QueryMetrics(ctx context.Context, projectID, query string) (float64, error) {
func (s StackDriverClient) QueryMetrics(ctx context.Context, projectID, query string, valueIfNull *float64) (float64, error) {
req := &monitoringpb.QueryTimeSeriesRequest{
Query: query,
PageSize: 1,
Expand All @@ -303,7 +303,10 @@ func (s StackDriverClient) QueryMetrics(ctx context.Context, projectID, query st
resp, err := it.Next()

if err == iterator.Done {
return value, fmt.Errorf("could not find stackdriver metric with query %s", req.Query)
if valueIfNull == nil {
return value, fmt.Errorf("could not find stackdriver metric with query %s", req.Query)
}
return *valueIfNull, nil
}

if err != nil {
Expand Down
11 changes: 10 additions & 1 deletion pkg/scalers/gcp_pubsub_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type pubsubMetadata struct {
triggerIndex int
aggregation string
timeHorizon string
valueIfNull *float64
}

// NewPubSubScaler creates a new pubsubScaler
Expand Down Expand Up @@ -177,6 +178,14 @@ func parsePubSubMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger)
}
meta.value = triggerValue
}

if val, ok := config.TriggerMetadata["valueIfNull"]; ok && val != "" {
valueIfNull, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("valueIfNull parsing error %w", err)
}
meta.valueIfNull = &valueIfNull
}
}

meta.aggregation = config.TriggerMetadata["aggregation"]
Expand Down Expand Up @@ -291,7 +300,7 @@ func (s *pubsubScaler) getMetrics(ctx context.Context, metricType string) (float

// Pubsub metrics are collected every 60 seconds so no need to aggregate them.
// See: https://cloud.google.com/monitoring/api/metrics_gcp#gcp-pubsub
return s.client.QueryMetrics(ctx, projectID, query)
return s.client.QueryMetrics(ctx, projectID, query, s.metadata.valueIfNull)
}

func getResourceData(s *pubsubScaler) (string, string) {
Expand Down

0 comments on commit 78765cc

Please sign in to comment.