From f59da4419faa16f2d71cc505ddac93f2d5566446 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Ferrero Date: Mon, 4 Oct 2021 10:13:04 +0200 Subject: [PATCH] Add support to provide the metric name in Azure Log Analytics Scaler (#2106) Signed-off-by: Jorge Turrado --- CHANGELOG.md | 1 + pkg/scalers/azure_log_analytics_scaler.go | 10 +++++++- .../azure_log_analytics_scaler_test.go | 24 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d278c433fc6..c82ebb2ec3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pkg/scalers/azure_log_analytics_scaler.go b/pkg/scalers/azure_log_analytics_scaler.go index ba0df660b5e..f122006bc90 100644 --- a/pkg/scalers/azure_log_analytics_scaler.go +++ b/pkg/scalers/azure_log_analytics_scaler.go @@ -64,6 +64,7 @@ type azureLogAnalyticsMetadata struct { podIdentity string query string threshold int64 + metricName string // Custom metric name for trigger } type sessionCache struct { @@ -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 } @@ -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, diff --git a/pkg/scalers/azure_log_analytics_scaler_test.go b/pkg/scalers/azure_log_analytics_scaler_test.go index 328eac3affb..864b9de19cc 100644 --- a/pkg/scalers/azure_log_analytics_scaler_test.go +++ b/pkg/scalers/azure_log_analytics_scaler_test.go @@ -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) + } + } +}