From a30c13251fdaa903661329b9378ab2cb9a30e67e Mon Sep 17 00:00:00 2001 From: Sudipto Baral Date: Thu, 9 Mar 2023 17:11:30 +0600 Subject: [PATCH] feat: implemented avg for getting single value from the metric. Signed-off-by: Sudipto Baral --- .../common/providers/datadog/datadog.go | 24 +++++++++++++- .../common/providers/datadog/datadog_test.go | 31 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/metrics-operator/controllers/common/providers/datadog/datadog.go b/metrics-operator/controllers/common/providers/datadog/datadog.go index 13c142cdf1..6080a48028 100644 --- a/metrics-operator/controllers/common/providers/datadog/datadog.go +++ b/metrics-operator/controllers/common/providers/datadog/datadog.go @@ -74,6 +74,28 @@ func (d *KeptnDataDogProvider) EvaluateQuery(ctx context.Context, metric metrics } points := (result.Series)[0].Pointlist - value := strconv.FormatFloat(*points[len(points)-1][1], 'g', 5, 64) + if len(points) == 0 { + d.Log.Info("No metric points in query result") + return "", nil, fmt.Errorf("no metric points in query result") + } + + r := d.getSingleValue(points) + value := strconv.FormatFloat(r, 'g', 5, 64) return value, b, nil } + +func (d *KeptnDataDogProvider) getSingleValue(points [][]*float64) float64 { + var sum float64 = 0 + var count uint64 = 0 + for _, point := range points { + if point[1] != nil { + sum += *point[1] + count++ + } + } + if count < 1 { + // cannot dive by zero + return 0 + } + return sum / float64(count) +} diff --git a/metrics-operator/controllers/common/providers/datadog/datadog_test.go b/metrics-operator/controllers/common/providers/datadog/datadog_test.go index 485b2b2f79..ca39650e53 100644 --- a/metrics-operator/controllers/common/providers/datadog/datadog_test.go +++ b/metrics-operator/controllers/common/providers/datadog/datadog_test.go @@ -2,12 +2,14 @@ package datadog import ( "context" + "encoding/json" "fmt" "net/http" "net/http/httptest" "strings" "testing" + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha2" "github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake" "github.com/stretchr/testify/require" @@ -69,7 +71,7 @@ func TestEvaluateQuery_HappyPath(t *testing.T) { r, raw, e := kdd.EvaluateQuery(context.TODO(), metric, p) require.Nil(t, e) require.Equal(t, []byte(ddPayload), raw) - require.Equal(t, fmt.Sprintf("%.3f", 84.782), r) + require.Equal(t, fmt.Sprintf("%.3f", 89.116), r) } func TestEvaluateQuery_WrongPayloadHandling(t *testing.T) { svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -285,3 +287,30 @@ func TestEvaluateQuery_EmptyPayload(t *testing.T) { require.True(t, strings.Contains(e.Error(), "no values in query result")) } +func TestGetSingleValue_EmptyPoints(t *testing.T) { + fakeClient := fake.NewClient() + kdd := KeptnDataDogProvider{ + HttpClient: http.Client{}, + Log: ctrl.Log.WithName("testytest"), + K8sClient: fakeClient, + } + var points [][]*float64 + value := kdd.getSingleValue(points) + + require.Zero(t, value) +} +func TestGetSingleValue_HappyPath(t *testing.T) { + fakeClient := fake.NewClient() + kdd := KeptnDataDogProvider{ + HttpClient: http.Client{}, + Log: ctrl.Log.WithName("testytest"), + K8sClient: fakeClient, + } + result := datadogV1.MetricsQueryResponse{} + _ = json.Unmarshal([]byte(ddPayload), &result) + points := (result.Series)[0].Pointlist + value := kdd.getSingleValue(points) + + require.NotZero(t, value) + require.Equal(t, 89.11554133097331, value) +}