Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gcp-stackdriver): handling value if no timeseries is return #5345

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
}
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
15 changes: 14 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,18 @@ func parseStackdriverMetadata(config *ScalerConfig, logger logr.Logger) (*stackd
meta.activationTargetValue = activationTargetValue
}

if val, ok := config.TriggerMetadata["valueIfNull"]; ok {
if val == "" {
meta.valueIfNull = nil
} else {
valueIfNull, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("valueIfNull parsing error %w", err)
}
meta.valueIfNull = &valueIfNull
}
}
lindmin marked this conversation as resolved.
Show resolved Hide resolved

auth, err := getGCPAuthorization(config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -207,7 +220,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