Skip to content

Commit

Permalink
feat: GCP Pub/Sub scaler add configurable fallback value when no metr…
Browse files Browse the repository at this point in the history
…ic value found (#5897)

Signed-off-by: Benjamin Woodley <bwoodley012@gmail.com>
  • Loading branch information
Caislear authored Jul 25, 2024
1 parent 857c09f commit 4a14698
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Here is an overview of all new **experimental** features:
### Improvements

- **Cassandra Scaler**: Add TLS support for cassandra scaler ([#5802](https://github.com/kedacore/keda/issues/5802))
- **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))
- **IBM MQ Scaler**: Add TLS support for IBM MQ scaler ([#5974](https://github.com/kedacore/keda/issues/5974))
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 @@ -179,6 +180,14 @@ func parsePubSubMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger)
}
}

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"]

meta.timeHorizon = config.TriggerMetadata["timeHorizon"]
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 4a14698

Please sign in to comment.