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: introduce activationThreshold for New Relic Scaler #3427

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 18 additions & 8 deletions pkg/scalers/newrelic_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ type newrelicScaler struct {
}

type newrelicMetadata struct {
account int
region string
queryKey string
noDataError bool
nrql string
threshold float64
scalerIndex int
account int
region string
queryKey string
noDataError bool
nrql string
threshold float64
activationThreshold float64
scalerIndex int
}

func NewNewRelicScaler(config *ScalerConfig) (Scaler, error) {
Expand Down Expand Up @@ -116,6 +117,15 @@ func parseNewRelicMetadata(config *ScalerConfig, logger logr.Logger) (*newrelicM
return nil, fmt.Errorf("missing %s value", threshold)
}

meta.activationThreshold = 0
if val, ok := config.TriggerMetadata["activationThreshold"]; ok {
activationThreshold, err := strconv.ParseFloat(val, 64)
if err != nil {
return nil, fmt.Errorf("queryValue parsing error %s", err.Error())
}
meta.activationThreshold = activationThreshold
}

// If Query Return an Empty Data , shall we treat it as an error or not
// default is NO error is returned when query result is empty/no data
if val, ok := config.TriggerMetadata[noDataError]; ok {
Expand All @@ -137,7 +147,7 @@ func (s *newrelicScaler) IsActive(ctx context.Context) (bool, error) {
s.logger.Error(err, "error executing NRQL")
return false, err
}
return val > 0, nil
return val > s.metadata.activationThreshold, nil
}

func (s *newrelicScaler) Close(context.Context) error {
Expand Down
4 changes: 3 additions & 1 deletion pkg/scalers/newrelic_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var testNewRelicMetadata = []parseNewRelicMetadataTestData{
// all properly formed
{map[string]string{"account": "0", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false},
// all properly formed
{map[string]string{"account": "0", "region": "EU", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false},
{map[string]string{"account": "0", "region": "EU", "threshold": "100", "activationThreshold": "20", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, false},
// account passed via auth params
{map[string]string{"region": "EU", "threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{"account": "0"}, false},
// region passed via auth params
Expand All @@ -38,6 +38,8 @@ var testNewRelicMetadata = []parseNewRelicMetadataTestData{
{map[string]string{"threshold": "100", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true},
// malformed threshold
{map[string]string{"account": "0", "threshold": "one", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true},
// malformed activationThreshold
{map[string]string{"account": "0", "threshold": "100", "activationThreshold": "notanint", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true},
// missing threshold
{map[string]string{"account": "0", "queryKey": "somekey", "nrql": "SELECT average(cpuUsedCores) as result FROM K8sContainerSample WHERE containerName='coredns'"}, map[string]string{}, true},
// missing query
Expand Down
4 changes: 4 additions & 0 deletions tests/.env
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ OPENSTACK_PASSWORD=
OPENSTACK_PROJECT_ID=
OPENSTACK_USER_ID=
PREDICTKUBE_API_KEY=
NEWRELIC_REGION=
NEWRELIC_ACCOUNT_ID=
NEWRELIC_LICENSE=
NEWRELIC_API_KEY=
285 changes: 0 additions & 285 deletions tests/scalers/new-relic.test.ts

This file was deleted.

Loading