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

Added custom time horizon in GCP scaler #5778

Merged
merged 5 commits into from
May 4, 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Here is an overview of all new **experimental** features:

### Improvements

- TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))
- **GCP Scaler**: Added custom time horizon in GCP scaler ([#5778](https://github.com/kedacore/keda/issues/5778))
Yaxhveer marked this conversation as resolved.
Show resolved Hide resolved

### Fixes

Expand Down
11 changes: 7 additions & 4 deletions pkg/scalers/gcp/gcp_stackdriver_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,13 @@ func getActualProjectID(s *StackDriverClient, projectID string) string {
// | align delta(3m)
// | every 3m
// | group_by [], count(value)
func (s StackDriverClient) BuildMQLQuery(projectID, resourceType, metric, resourceName, aggregation string) (string, error) {
th := defaultTimeHorizon
if aggregation != "" {
th = aggregationTimeHorizon
func (s StackDriverClient) BuildMQLQuery(projectID, resourceType, metric, resourceName, aggregation, timeHorizon string) (string, error) {
th := timeHorizon
if th == "" {
th = defaultTimeHorizon
if aggregation != "" {
th = aggregationTimeHorizon
}
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
}

pid := getActualProjectID(&s, projectID)
Expand Down
19 changes: 10 additions & 9 deletions pkg/scalers/gcp/gcp_stackdriver_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,55 @@ func TestBuildMQLQuery(t *testing.T) {
metric string
resourceName string
aggregation string
timeHorizon string

expected string
isError bool
}{
{
"topic with aggregation",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "count",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "count", "1m",
"fetch pubsub_topic | metric 'pubsub.googleapis.com/topic/x' | filter (resource.project_id == 'myproject' && resource.topic_id == 'mytopic')" +
" | within 5m | align delta(3m) | every 3m | group_by [], count(value)",
" | within 1m | align delta(3m) | every 3m | group_by [], count(value)",
false,
},
{
"topic without aggregation",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "", "",
"fetch pubsub_topic | metric 'pubsub.googleapis.com/topic/x' | filter (resource.project_id == 'myproject' && resource.topic_id == 'mytopic')" +
" | within 2m",
false,
},
{
"subscription with aggregation",
"subscription", "pubsub.googleapis.com/subscription/x", "mysubscription", "percentile99",
"subscription", "pubsub.googleapis.com/subscription/x", "mysubscription", "percentile99", "",
"fetch pubsub_subscription | metric 'pubsub.googleapis.com/subscription/x' | filter (resource.project_id == 'myproject' && resource.subscription_id == 'mysubscription')" +
" | within 5m | align delta(3m) | every 3m | group_by [], percentile(value, 99)",
false,
},
{
"subscription without aggregation",
"subscription", "pubsub.googleapis.com/subscription/x", "mysubscription", "",
"subscription", "pubsub.googleapis.com/subscription/x", "mysubscription", "", "4m",
"fetch pubsub_subscription | metric 'pubsub.googleapis.com/subscription/x' | filter (resource.project_id == 'myproject' && resource.subscription_id == 'mysubscription')" +
" | within 2m",
" | within 4m",
false,
},
{
"invalid percentile",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "percentile101",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "percentile101", "1m",
"invalid percentile value: 101",
true,
},
{
"unsupported aggregation function",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "max",
"topic", "pubsub.googleapis.com/topic/x", "mytopic", "max", "",
"unsupported aggregation function: max",
true,
},
} {
s := &StackDriverClient{}
t.Run(tc.name, func(t *testing.T) {
q, err := s.BuildMQLQuery("myproject", tc.resourceType, tc.metric, tc.resourceName, tc.aggregation)
q, err := s.BuildMQLQuery("myproject", tc.resourceType, tc.metric, tc.resourceName, tc.aggregation, tc.timeHorizon)
if tc.isError {
assert.Error(t, err)
assert.Equal(t, tc.expected, err.Error())
Expand Down
5 changes: 4 additions & 1 deletion pkg/scalers/gcp_pubsub_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type pubsubMetadata struct {
gcpAuthorization *gcp.AuthorizationMetadata
triggerIndex int
aggregation string
timeHorizon string
}

// NewPubSubScaler creates a new pubsubScaler
Expand Down Expand Up @@ -180,6 +181,8 @@ func parsePubSubMetadata(config *scalersconfig.ScalerConfig, logger logr.Logger)

meta.aggregation = config.TriggerMetadata["aggregation"]

meta.timeHorizon = config.TriggerMetadata["timeHorizon"]

err := parsePubSubResourceConfig(config, &meta)
if err != nil {
return nil, err
Expand Down Expand Up @@ -280,7 +283,7 @@ func (s *pubsubScaler) getMetrics(ctx context.Context, metricType string) (float
}
resourceID, projectID := getResourceData(s)
query, err := s.client.BuildMQLQuery(
projectID, s.metadata.resourceType, metricType, resourceID, s.metadata.aggregation,
projectID, s.metadata.resourceType, metricType, resourceID, s.metadata.aggregation, s.metadata.timeHorizon,
)
if err != nil {
return -1, err
Expand Down
Loading