Skip to content

Commit

Permalink
Add support to provide the metric name in Azure Log Analytics Scaler (#…
Browse files Browse the repository at this point in the history
…2106)

Signed-off-by: Jorge Turrado <jorge.turrado@docplanner.com>
  • Loading branch information
Jorge Turrado Ferrero authored Oct 4, 2021
1 parent 02aa93a commit f59da44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Escape `queueName` and `vhostName` in RabbitMQ Scaler before use them in query string (bug fix) ([#2055](https://github.com/kedacore/keda/pull/2055))
- TriggerAuthentication/Vault: add support for HashiCorp Vault namespace (Vault Enterprise) ([#2085](https://github.com/kedacore/keda/pull/2085))
- Add custom http timeout in RabbitMQ Scaler ([#2086](https://github.com/kedacore/keda/pull/2086))
- Add support to provide the metric name in Azure Log Analytics Scaler ([#2106](https://github.com/kedacore/keda/pull/2106))

### Breaking Changes

Expand Down
10 changes: 9 additions & 1 deletion pkg/scalers/azure_log_analytics_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type azureLogAnalyticsMetadata struct {
podIdentity string
query string
threshold int64
metricName string // Custom metric name for trigger
}

type sessionCache struct {
Expand Down Expand Up @@ -177,6 +178,13 @@ func parseAzureLogAnalyticsMetadata(config *ScalerConfig) (*azureLogAnalyticsMet
}
meta.threshold = threshold

// Resolve metricName
if val, ok := config.TriggerMetadata["metricName"]; ok {
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-log-analytics", val))
} else {
meta.metricName = kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-log-analytics", meta.workspaceID))
}

return &meta, nil
}

Expand Down Expand Up @@ -214,7 +222,7 @@ func (s *azureLogAnalyticsScaler) GetMetricSpecForScaling() []v2beta2.MetricSpec

externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: kedautil.NormalizeString(fmt.Sprintf("%s-%s", "azure-log-analytics", s.metadata.workspaceID)),
Name: s.metadata.metricName,
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
Expand Down
24 changes: 24 additions & 0 deletions pkg/scalers/azure_log_analytics_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,27 @@ func TestLogAnalyticsGetMetricSpecForScaling(t *testing.T) {
}
}
}

type parseMetadataMetricNameTestData struct {
metadata map[string]string
metricName string
}

var testParseMetadataMetricName = []parseMetadataMetricNameTestData{
// WorkspaceID
{map[string]string{"tenantIdFromEnv": "d248da64-0e1e-4f79-b8c6-72ab7aa055eb", "clientIdFromEnv": "41826dd4-9e0a-4357-a5bd-a88ad771ea7d", "clientSecretFromEnv": "U6DtAX5r6RPZxd~l12Ri3X8J9urt5Q-xs", "workspaceIdFromEnv": "074dd9f8-c368-4220-9400-acb6e80fc325", "query": query, "threshold": "1900000000"}, "azure-log-analytics-074dd9f8-c368-4220-9400-acb6e80fc325"},
// Custom Name
{map[string]string{"metricName": "testName", "tenantIdFromEnv": "d248da64-0e1e-4f79-b8c6-72ab7aa055eb", "clientIdFromEnv": "41826dd4-9e0a-4357-a5bd-a88ad771ea7d", "clientSecretFromEnv": "U6DtAX5r6RPZxd~l12Ri3X8J9urt5Q-xs", "workspaceIdFromEnv": "074dd9f8-c368-4220-9400-acb6e80fc325", "query": query, "threshold": "1900000000"}, "azure-log-analytics-testName"},
}

func TestLogAnalyticsParseMetadataMetricName(t *testing.T) {
for _, testData := range testParseMetadataMetricName {
meta, err := parseAzureLogAnalyticsMetadata(&ScalerConfig{ResolvedEnv: sampleLogAnalyticsResolvedEnv, TriggerMetadata: testData.metadata, AuthParams: nil, PodIdentity: ""})
if err != nil {
t.Error("Expected success but got error", err)
}
if meta.metricName != testData.metricName {
t.Errorf("Expected %s but got %s", testData.metricName, meta.metricName)
}
}
}

0 comments on commit f59da44

Please sign in to comment.