From fa82039fc63aef4e1634678b2077c3b0982c6cdc Mon Sep 17 00:00:00 2001 From: realanna Date: Thu, 24 Aug 2023 10:10:46 +0200 Subject: [PATCH 1/8] feat(metrics-operator): introduce new provider interface function Signed-off-by: realanna --- .../controllers/common/providers/common.go | 7 ++++ .../common/providers/datadog/common_test.go | 2 +- .../common/providers/datadog/datadog.go | 12 ++++++- .../common/providers/datadog/datadog_test.go | 2 +- .../common/providers/dynatrace/common_test.go | 2 +- .../common/providers/dynatrace/dynatrace.go | 11 ++++++ .../providers/dynatrace/dynatrace_dql.go | 5 +++ .../providers/dynatrace/dynatrace_test.go | 2 +- .../common/providers/prometheus/prometheus.go | 34 +++++++++++++++++++ .../controllers/common/providers/provider.go | 1 + 10 files changed, 73 insertions(+), 5 deletions(-) diff --git a/metrics-operator/controllers/common/providers/common.go b/metrics-operator/controllers/common/providers/common.go index 12b47b57f6..701712feb4 100644 --- a/metrics-operator/controllers/common/providers/common.go +++ b/metrics-operator/controllers/common/providers/common.go @@ -4,3 +4,10 @@ const DynatraceProviderType = "dynatrace" const DynatraceDQLProviderType = "dql" const PrometheusProviderType = "prometheus" const DataDogProviderType = "datadog" + +var SupportedProviders = []string{ + DynatraceProviderType, + DynatraceDQLProviderType, + PrometheusProviderType, + DataDogProviderType, +} diff --git a/metrics-operator/controllers/common/providers/datadog/common_test.go b/metrics-operator/controllers/common/providers/datadog/common_test.go index 3804cfab50..3f50d0be55 100644 --- a/metrics-operator/controllers/common/providers/datadog/common_test.go +++ b/metrics-operator/controllers/common/providers/datadog/common_test.go @@ -2,13 +2,13 @@ package datadog import ( "context" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "strings" "testing" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" diff --git a/metrics-operator/controllers/common/providers/datadog/datadog.go b/metrics-operator/controllers/common/providers/datadog/datadog.go index 52287dfe1d..72c097e36d 100644 --- a/metrics-operator/controllers/common/providers/datadog/datadog.go +++ b/metrics-operator/controllers/common/providers/datadog/datadog.go @@ -26,6 +26,12 @@ type KeptnDataDogProvider struct { K8sClient client.Client } +func (d *KeptnDataDogProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { + ctx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + return d.query(ctx, query, *provider, spec.From.Unix(), spec.To.Unix()) +} + // EvaluateQuery fetches the SLI values from datadog provider func (d *KeptnDataDogProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) { ctx, cancel := context.WithTimeout(ctx, 20*time.Second) @@ -35,7 +41,11 @@ func (d *KeptnDataDogProvider) EvaluateQuery(ctx context.Context, metric metrics if err != nil { return "", nil, err } - qURL := provider.Spec.TargetServer + "/api/v1/query?from=" + strconv.Itoa(int(fromTime)) + "&to=" + strconv.Itoa(int(toTime)) + "&query=" + url.QueryEscape(metric.Spec.Query) + return d.query(ctx, metric.Spec.Query, provider, fromTime, toTime) +} + +func (d *KeptnDataDogProvider) query(ctx context.Context, query string, provider metricsapi.KeptnMetricsProvider, fromTime int64, toTime int64) (string, []byte, error) { + qURL := provider.Spec.TargetServer + "/api/v1/query?from=" + strconv.Itoa(int(fromTime)) + "&to=" + strconv.Itoa(int(toTime)) + "&query=" + url.QueryEscape(query) apiKeyVal, appKeyVal, err := getDDSecret(ctx, provider, d.K8sClient) if err != nil { return "", nil, err diff --git a/metrics-operator/controllers/common/providers/datadog/datadog_test.go b/metrics-operator/controllers/common/providers/datadog/datadog_test.go index 14b0b78b50..c669170cd4 100644 --- a/metrics-operator/controllers/common/providers/datadog/datadog_test.go +++ b/metrics-operator/controllers/common/providers/datadog/datadog_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "strings" @@ -13,7 +14,6 @@ import ( "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" diff --git a/metrics-operator/controllers/common/providers/dynatrace/common_test.go b/metrics-operator/controllers/common/providers/dynatrace/common_test.go index 04fa73f910..0ce2a6c3c0 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/common_test.go +++ b/metrics-operator/controllers/common/providers/dynatrace/common_test.go @@ -2,12 +2,12 @@ package dynatrace import ( "context" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "testing" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" ) diff --git a/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go b/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go index 886d031369..4056ebbed1 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go +++ b/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go @@ -38,6 +38,13 @@ type DynatraceData struct { Values []*float64 `json:"values"` } +func (d *KeptnDynatraceProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { + baseURL := d.normalizeAPIURL(provider.Spec.TargetServer) + escapedQ := urlEncodeQuery(query) + qURL := baseURL + "v2/metrics/query?metricSelector=" + escapedQ + "&from=" + spec.From.String() + "&to=" + spec.To.String() + return d.runQuery(ctx, qURL, *provider) +} + // EvaluateQuery fetches the SLI values from dynatrace provider func (d *KeptnDynatraceProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) { baseURL := d.normalizeAPIURL(provider.Spec.TargetServer) @@ -52,6 +59,10 @@ func (d *KeptnDynatraceProvider) EvaluateQuery(ctx context.Context, metric metri qURL = urlEncodeQuery(qURL) qURL = baseURL + "v2/metrics/query?" + qURL + return d.runQuery(ctx, qURL, provider) +} + +func (d *KeptnDynatraceProvider) runQuery(ctx context.Context, qURL string, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) { d.Log.Info("Running query: " + qURL) ctx, cancel := context.WithTimeout(ctx, 20*time.Second) defer cancel() diff --git a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go index f134a7d2ba..51e8270595 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go +++ b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go @@ -30,6 +30,11 @@ type keptnDynatraceDQLProvider struct { clock clock.Clock } +func (d *keptnDynatraceDQLProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { + //TODO implement me + panic("implement me") +} + type DynatraceDQLHandler struct { RequestToken string `json:"requestToken"` } diff --git a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go index 2477445cb6..030723b010 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go +++ b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go @@ -2,13 +2,13 @@ package dynatrace import ( "context" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "strings" "testing" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" diff --git a/metrics-operator/controllers/common/providers/prometheus/prometheus.go b/metrics-operator/controllers/common/providers/prometheus/prometheus.go index 6d1da44185..3e0bbccac1 100644 --- a/metrics-operator/controllers/common/providers/prometheus/prometheus.go +++ b/metrics-operator/controllers/common/providers/prometheus/prometheus.go @@ -23,6 +23,40 @@ type KeptnPrometheusProvider struct { HttpClient http.Client } +func (r *KeptnPrometheusProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { + ctx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + + client, err := promapi.NewClient(promapi.Config{Address: provider.Spec.TargetServer, Client: &r.HttpClient}) + if err != nil { + return "", nil, err + } + api := prometheus.NewAPI(client) + r.Log.Info(fmt.Sprintf( + "Running query: /api/v1/query_range?query=%s&start=%d&end=%d", + query, + spec.From.Unix(), spec.To.Unix(), + )) + queryRange := prometheus.Range{ + Start: spec.From.Time, + End: spec.To.Time, + } + result, warnings, err := api.QueryRange( + ctx, + query, + queryRange, + []prometheus.Option{}..., + ) + + if err != nil { + return "", nil, err + } + if len(warnings) != 0 { + r.Log.Info("Prometheus API returned warnings: " + warnings[0]) + } + return getResultForMatrix(result) +} + // EvaluateQuery fetches the SLI values from prometheus provider func (r *KeptnPrometheusProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) { ctx, cancel := context.WithTimeout(ctx, 20*time.Second) diff --git a/metrics-operator/controllers/common/providers/provider.go b/metrics-operator/controllers/common/providers/provider.go index 61cc818746..3117c63ca7 100644 --- a/metrics-operator/controllers/common/providers/provider.go +++ b/metrics-operator/controllers/common/providers/provider.go @@ -17,6 +17,7 @@ import ( // KeptnSLIProvider is the interface that describes the operations that an SLI provider must implement type KeptnSLIProvider interface { EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) + RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) } // NewProvider is a factory method that chooses the right implementation of KeptnSLIProvider From 8dfe8ec62535767926392f522c2c4297b6dcc9b6 Mon Sep 17 00:00:00 2001 From: realanna Date: Thu, 24 Aug 2023 10:11:06 +0200 Subject: [PATCH 2/8] feat(metrics-operator): introduce new provider interface function test Signed-off-by: realanna --- metrics-operator/controllers/common/providers/provider_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics-operator/controllers/common/providers/provider_test.go b/metrics-operator/controllers/common/providers/provider_test.go index f3869d424a..30fc208ae7 100644 --- a/metrics-operator/controllers/common/providers/provider_test.go +++ b/metrics-operator/controllers/common/providers/provider_test.go @@ -1,10 +1,10 @@ package providers import ( + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "testing" "github.com/go-logr/logr" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/datadog" "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/dynatrace" "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/prometheus" From 66452f7a9faf491177f8f8b7d8eb9932c378822a Mon Sep 17 00:00:00 2001 From: realanna Date: Thu, 24 Aug 2023 10:20:13 +0200 Subject: [PATCH 3/8] feat(metrics-operator): fix lint Signed-off-by: realanna --- metrics-operator/controllers/common/providers/provider_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics-operator/controllers/common/providers/provider_test.go b/metrics-operator/controllers/common/providers/provider_test.go index 30fc208ae7..f3869d424a 100644 --- a/metrics-operator/controllers/common/providers/provider_test.go +++ b/metrics-operator/controllers/common/providers/provider_test.go @@ -1,10 +1,10 @@ package providers import ( - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "testing" "github.com/go-logr/logr" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/datadog" "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/dynatrace" "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/prometheus" From 062263db1c5c204d8eeed507b6368cc61286ab56 Mon Sep 17 00:00:00 2001 From: realanna Date: Thu, 24 Aug 2023 10:28:39 +0200 Subject: [PATCH 4/8] feat(metrics-operator): prometheus test Signed-off-by: realanna --- .../providers/prometheus/prometheus_test.go | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go index a18cb74d64..241e41a3da 100644 --- a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go +++ b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go @@ -2,9 +2,11 @@ package prometheus import ( "context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "net/http" "net/http/httptest" "testing" + "time" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" "github.com/prometheus/common/model" @@ -314,3 +316,53 @@ func Test_resultsForMatrix(t *testing.T) { }) } } + +func TestRunAnalysis(t *testing.T) { + + svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, err := w.Write([]byte(promPayloadWithRangeAndStep)) + require.Nil(t, err) + })) + defer svr.Close() + + // Create a mock KeptnMetricsProvider + mockProvider := &metricsapi.KeptnMetricsProvider{ + Spec: metricsapi.KeptnMetricsProviderSpec{ + SecretKeyRef: v1.SecretKeySelector{ + LocalObjectReference: v1.LocalObjectReference{ + Name: "myapitoken", + }, + Key: "mykey", + }, + TargetServer: svr.URL, + }, + } + + // Create your KeptnPrometheusProvider instance + provider := KeptnPrometheusProvider{ + HttpClient: http.Client{}, + Log: ctrl.Log.WithName("testytest"), + } + + // Prepare the analysis spec + now := time.Now() + analysisSpec := metricsapi.AnalysisSpec{ + Timeframe: metricsapi.Timeframe{ + From: metav1.Time{ + Time: now.Add(-time.Hour), + }, + To: metav1.Time{ + Time: now, + }}, + } + + // Prepare the expected result + expectedResult := "1" + + // Call the function + result, _, err := provider.RunAnalysis(context.Background(), "your_query_string_here", analysisSpec, mockProvider) + + // Assertions + require.NoError(t, err) + require.Equal(t, expectedResult, result) +} From 1a13789d323762aa171e53328853d0e2d33ca6f5 Mon Sep 17 00:00:00 2001 From: realanna Date: Thu, 24 Aug 2023 10:53:33 +0200 Subject: [PATCH 5/8] feat(metrics-operator): lint Signed-off-by: realanna --- .../controllers/common/providers/datadog/datadog_test.go | 2 +- .../controllers/common/providers/dynatrace/common_test.go | 2 +- .../controllers/common/providers/dynatrace/dynatrace_test.go | 2 +- .../controllers/common/providers/prometheus/prometheus_test.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metrics-operator/controllers/common/providers/datadog/datadog_test.go b/metrics-operator/controllers/common/providers/datadog/datadog_test.go index c669170cd4..14b0b78b50 100644 --- a/metrics-operator/controllers/common/providers/datadog/datadog_test.go +++ b/metrics-operator/controllers/common/providers/datadog/datadog_test.go @@ -5,7 +5,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "strings" @@ -14,6 +13,7 @@ import ( "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" diff --git a/metrics-operator/controllers/common/providers/dynatrace/common_test.go b/metrics-operator/controllers/common/providers/dynatrace/common_test.go index 0ce2a6c3c0..04fa73f910 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/common_test.go +++ b/metrics-operator/controllers/common/providers/dynatrace/common_test.go @@ -2,12 +2,12 @@ package dynatrace import ( "context" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "testing" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" ) diff --git a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go index 030723b010..2477445cb6 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go +++ b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_test.go @@ -2,13 +2,13 @@ package dynatrace import ( "context" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "strings" "testing" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" diff --git a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go index 241e41a3da..b9aa99faa2 100644 --- a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go +++ b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go @@ -2,7 +2,6 @@ package prometheus import ( "context" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "net/http" "net/http/httptest" "testing" @@ -12,6 +11,7 @@ import ( "github.com/prometheus/common/model" "github.com/stretchr/testify/require" v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" ) From 2ab42216ef80366b44d15e693591feb842c6fb2f Mon Sep 17 00:00:00 2001 From: odubajDT Date: Thu, 24 Aug 2023 11:00:30 +0200 Subject: [PATCH 6/8] lint Signed-off-by: odubajDT --- .../controllers/common/providers/datadog/common_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics-operator/controllers/common/providers/datadog/common_test.go b/metrics-operator/controllers/common/providers/datadog/common_test.go index 3f50d0be55..3804cfab50 100644 --- a/metrics-operator/controllers/common/providers/datadog/common_test.go +++ b/metrics-operator/controllers/common/providers/datadog/common_test.go @@ -2,13 +2,13 @@ package datadog import ( "context" - "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "net/http" "net/http/httptest" "strings" "testing" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" + "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" From 830905f70e8f8191ee93dfa569ba2f86cb115178 Mon Sep 17 00:00:00 2001 From: realanna Date: Thu, 24 Aug 2023 12:58:57 +0200 Subject: [PATCH 7/8] feat(metrics-operator): rename to FetchAnalysisValue Signed-off-by: realanna --- .../controllers/common/providers/datadog/datadog.go | 2 +- .../controllers/common/providers/dynatrace/dynatrace.go | 2 +- .../controllers/common/providers/dynatrace/dynatrace_dql.go | 2 +- .../controllers/common/providers/prometheus/prometheus.go | 2 +- .../controllers/common/providers/prometheus/prometheus_test.go | 2 +- metrics-operator/controllers/common/providers/provider.go | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/metrics-operator/controllers/common/providers/datadog/datadog.go b/metrics-operator/controllers/common/providers/datadog/datadog.go index 72c097e36d..b230d4f8c3 100644 --- a/metrics-operator/controllers/common/providers/datadog/datadog.go +++ b/metrics-operator/controllers/common/providers/datadog/datadog.go @@ -26,7 +26,7 @@ type KeptnDataDogProvider struct { K8sClient client.Client } -func (d *KeptnDataDogProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { +func (d *KeptnDataDogProvider) FetchAnalysisValue(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { ctx, cancel := context.WithTimeout(ctx, 20*time.Second) defer cancel() return d.query(ctx, query, *provider, spec.From.Unix(), spec.To.Unix()) diff --git a/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go b/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go index 4056ebbed1..36a1d5d739 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go +++ b/metrics-operator/controllers/common/providers/dynatrace/dynatrace.go @@ -38,7 +38,7 @@ type DynatraceData struct { Values []*float64 `json:"values"` } -func (d *KeptnDynatraceProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { +func (d *KeptnDynatraceProvider) FetchAnalysisValue(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { baseURL := d.normalizeAPIURL(provider.Spec.TargetServer) escapedQ := urlEncodeQuery(query) qURL := baseURL + "v2/metrics/query?metricSelector=" + escapedQ + "&from=" + spec.From.String() + "&to=" + spec.To.String() diff --git a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go index 51e8270595..dfaed8eb44 100644 --- a/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go +++ b/metrics-operator/controllers/common/providers/dynatrace/dynatrace_dql.go @@ -30,7 +30,7 @@ type keptnDynatraceDQLProvider struct { clock clock.Clock } -func (d *keptnDynatraceDQLProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { +func (d *keptnDynatraceDQLProvider) FetchAnalysisValue(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { //TODO implement me panic("implement me") } diff --git a/metrics-operator/controllers/common/providers/prometheus/prometheus.go b/metrics-operator/controllers/common/providers/prometheus/prometheus.go index 3e0bbccac1..dbd6ca4a12 100644 --- a/metrics-operator/controllers/common/providers/prometheus/prometheus.go +++ b/metrics-operator/controllers/common/providers/prometheus/prometheus.go @@ -23,7 +23,7 @@ type KeptnPrometheusProvider struct { HttpClient http.Client } -func (r *KeptnPrometheusProvider) RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { +func (r *KeptnPrometheusProvider) FetchAnalysisValue(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) { ctx, cancel := context.WithTimeout(ctx, 20*time.Second) defer cancel() diff --git a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go index b9aa99faa2..e3847cb159 100644 --- a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go +++ b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go @@ -360,7 +360,7 @@ func TestRunAnalysis(t *testing.T) { expectedResult := "1" // Call the function - result, _, err := provider.RunAnalysis(context.Background(), "your_query_string_here", analysisSpec, mockProvider) + result, _, err := provider.FetchAnalysisValue(context.Background(), "your_query_string_here", analysisSpec, mockProvider) // Assertions require.NoError(t, err) diff --git a/metrics-operator/controllers/common/providers/provider.go b/metrics-operator/controllers/common/providers/provider.go index 3117c63ca7..779d75eb86 100644 --- a/metrics-operator/controllers/common/providers/provider.go +++ b/metrics-operator/controllers/common/providers/provider.go @@ -17,7 +17,7 @@ import ( // KeptnSLIProvider is the interface that describes the operations that an SLI provider must implement type KeptnSLIProvider interface { EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) - RunAnalysis(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) + FetchAnalysisValue(ctx context.Context, query string, spec metricsapi.AnalysisSpec, provider *metricsapi.KeptnMetricsProvider) (string, []byte, error) } // NewProvider is a factory method that chooses the right implementation of KeptnSLIProvider From dc3cd09e2166db30326ac6737697050300487b0c Mon Sep 17 00:00:00 2001 From: RealAnna <89971034+RealAnna@users.noreply.github.com> Date: Thu, 24 Aug 2023 13:08:46 +0200 Subject: [PATCH 8/8] Update metrics-operator/controllers/common/providers/prometheus/prometheus_test.go Co-authored-by: Florian Bacher Signed-off-by: RealAnna <89971034+RealAnna@users.noreply.github.com> --- .../controllers/common/providers/prometheus/prometheus_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go index e3847cb159..8c0558cdd4 100644 --- a/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go +++ b/metrics-operator/controllers/common/providers/prometheus/prometheus_test.go @@ -317,7 +317,7 @@ func Test_resultsForMatrix(t *testing.T) { } } -func TestRunAnalysis(t *testing.T) { +func TestFetchAnalysisValue(t *testing.T) { svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, err := w.Write([]byte(promPayloadWithRangeAndStep))