From f242e8aaa6ec732ba8ce9bad28a86987967a3185 Mon Sep 17 00:00:00 2001 From: lindmin Date: Wed, 3 Jan 2024 19:42:04 +0100 Subject: [PATCH] feat(gcp-stackdriver): handling value if no timeseries is return (#5345) * feat(gcp-stackdriver): handling value if no timeseries is return Signed-off-by: VAN KELST Baptiste * fix bad space Signed-off-by: VAN KELST Baptiste * update changelog Signed-off-by: VAN KELST Baptiste * Update pkg/scalers/gcp_stackdriver_scaler.go Co-authored-by: Jorge Turrado Ferrero Signed-off-by: lindmin --------- Signed-off-by: VAN KELST Baptiste Signed-off-by: lindmin Co-authored-by: Jorge Turrado Ferrero --- CHANGELOG.md | 1 + pkg/scalers/gcp_cloud_tasks_scaler.go | 2 +- pkg/scalers/gcp_stackdriver_client.go | 8 ++++++-- pkg/scalers/gcp_stackdriver_scaler.go | 11 ++++++++++- pkg/scalers/gcp_stackdriver_scaler_test.go | 4 ++++ 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 099b2f18014..a123f4d6304 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ Here is an overview of all new **experimental** features: - **General**: Support TriggerAuthentication properties from ConfigMap ([#4830](https://github.com/kedacore/keda/issues/4830)) - **General**: Use client-side round-robin load balancing for grpc calls ([#5224](https://github.com/kedacore/keda/issues/5224)) - **GCP pubsub scaler**: Support distribution-valued metrics and metrics from topics ([#5070](https://github.com/kedacore/keda/issues/5070)) +- **GCP stackdriver scaler**: Support valueIfNull parameter ([#5345](https://github.com/kedacore/keda/pull/5345)) - **Hashicorp Vault**: Add support to get secret that needs write operation (e.g. pki) ([#5067](https://github.com/kedacore/keda/issues/5067)) - **Hashicorp Vault**: Fix operator panic when spec.hashiCorpVault.credential.serviceAccount is not set ([#4964](https://github.com/kedacore/keda/issues/4964)) - **Hashicorp Vault**: Fix operator panic when using root token to authenticate to vault server ([#5192](https://github.com/kedacore/keda/issues/5192)) diff --git a/pkg/scalers/gcp_cloud_tasks_scaler.go b/pkg/scalers/gcp_cloud_tasks_scaler.go index ba2f7c42b22..dee2cc38798 100644 --- a/pkg/scalers/gcp_cloud_tasks_scaler.go +++ b/pkg/scalers/gcp_cloud_tasks_scaler.go @@ -179,5 +179,5 @@ func (s *gcpCloudTasksScaler) getMetrics(ctx context.Context, metricType string) // Cloud Tasks metrics are collected every 60 seconds so no need to aggregate them. // See: https://cloud.google.com/monitoring/api/metrics_gcp#gcp-cloudtasks - return s.client.GetMetrics(ctx, filter, s.metadata.projectID, nil) + return s.client.GetMetrics(ctx, filter, s.metadata.projectID, nil, nil) } diff --git a/pkg/scalers/gcp_stackdriver_client.go b/pkg/scalers/gcp_stackdriver_client.go index b6a0901f6d2..ca20f3ceb5c 100644 --- a/pkg/scalers/gcp_stackdriver_client.go +++ b/pkg/scalers/gcp_stackdriver_client.go @@ -213,7 +213,8 @@ func (s StackDriverClient) GetMetrics( ctx context.Context, filter string, projectID string, - aggregation *monitoringpb.Aggregation) (float64, error) { + aggregation *monitoringpb.Aggregation, + valueIfNull *float64) (float64, error) { // Set the start time to 1 minute ago startTime := time.Now().UTC().Add(time.Minute * -2) @@ -246,7 +247,10 @@ func (s StackDriverClient) GetMetrics( resp, err := it.Next() if err == iterator.Done { - return value, fmt.Errorf("could not find stackdriver metric with filter %s", filter) + if valueIfNull == nil { + return value, fmt.Errorf("could not find stackdriver metric with filter %s", filter) + } + return *valueIfNull, nil } if err != nil { diff --git a/pkg/scalers/gcp_stackdriver_scaler.go b/pkg/scalers/gcp_stackdriver_scaler.go index eb4b3a60bd2..1b63bd09cb5 100644 --- a/pkg/scalers/gcp_stackdriver_scaler.go +++ b/pkg/scalers/gcp_stackdriver_scaler.go @@ -30,6 +30,7 @@ type stackdriverMetadata struct { targetValue float64 activationTargetValue float64 metricName string + valueIfNull *float64 gcpAuthorization *gcpAuthorizationMetadata aggregation *monitoringpb.Aggregation @@ -109,6 +110,14 @@ func parseStackdriverMetadata(config *ScalerConfig, logger logr.Logger) (*stackd meta.activationTargetValue = activationTargetValue } + 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 + } + auth, err := getGCPAuthorization(config) if err != nil { return nil, err @@ -207,7 +216,7 @@ func (s *stackdriverScaler) GetMetricsAndActivity(ctx context.Context, metricNam // getMetrics gets metric type value from stackdriver api func (s *stackdriverScaler) getMetrics(ctx context.Context) (float64, error) { - val, err := s.client.GetMetrics(ctx, s.metadata.filter, s.metadata.projectID, s.metadata.aggregation) + val, err := s.client.GetMetrics(ctx, s.metadata.filter, s.metadata.projectID, s.metadata.aggregation, s.metadata.valueIfNull) if err == nil { s.logger.V(1).Info( fmt.Sprintf("Getting metrics for project %s, filter %s and aggregation %v. Result: %f", diff --git a/pkg/scalers/gcp_stackdriver_scaler_test.go b/pkg/scalers/gcp_stackdriver_scaler_test.go index 54d1ca08c2f..f20bd3e4250 100644 --- a/pkg/scalers/gcp_stackdriver_scaler_test.go +++ b/pkg/scalers/gcp_stackdriver_scaler_test.go @@ -55,6 +55,10 @@ var testStackdriverMetadata = []parseStackdriverMetadataTestData{ {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "alignmentPeriodSeconds": "a"}, true}, // properly formed float targetValue and activationTargetValue {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1"}, false}, + // properly formed float valueIfNull + {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1", "valueIfNull": "1.0"}, false}, + // With bad valueIfNull + {nil, map[string]string{"projectId": "myProject", "filter": sdFilter, "credentialsFromEnv": "SAMPLE_CREDS", "targetValue": "1.1", "activationTargetValue": "2.1", "valueIfNull": "toto"}, true}, } var gcpStackdriverMetricIdentifiers = []gcpStackdriverMetricIdentifier{