Skip to content

Commit

Permalink
feat(gcp-stackdriver): handling value if no timeseries is return (#5345)
Browse files Browse the repository at this point in the history
* feat(gcp-stackdriver): handling value if no timeseries is return

Signed-off-by: VAN KELST Baptiste <baptistevk@gmail.com>

* fix bad space

Signed-off-by: VAN KELST Baptiste <baptistevk@gmail.com>

* update changelog

Signed-off-by: VAN KELST Baptiste <baptistevk@gmail.com>

* Update pkg/scalers/gcp_stackdriver_scaler.go

Co-authored-by: Jorge Turrado Ferrero <Jorge_turrado@hotmail.es>
Signed-off-by: lindmin <baptistevk@gmail.com>

---------

Signed-off-by: VAN KELST Baptiste <baptistevk@gmail.com>
Signed-off-by: lindmin <baptistevk@gmail.com>
Co-authored-by: Jorge Turrado Ferrero <Jorge_turrado@hotmail.es>
  • Loading branch information
lindmin and JorTurFer authored Jan 3, 2024
1 parent 9c23dae commit f242e8a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/scalers/gcp_cloud_tasks_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 6 additions & 2 deletions pkg/scalers/gcp_stackdriver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion pkg/scalers/gcp_stackdriver_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type stackdriverMetadata struct {
targetValue float64
activationTargetValue float64
metricName string
valueIfNull *float64

gcpAuthorization *gcpAuthorizationMetadata
aggregation *monitoringpb.Aggregation
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions pkg/scalers/gcp_stackdriver_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit f242e8a

Please sign in to comment.