Skip to content

Commit

Permalink
feat: implemented avg for getting single value from the metric.
Browse files Browse the repository at this point in the history
Signed-off-by: Sudipto Baral <[email protected]>
  • Loading branch information
sudiptob2 committed Mar 9, 2023
1 parent b04da55 commit a30c132
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
24 changes: 23 additions & 1 deletion metrics-operator/controllers/common/providers/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

0 comments on commit a30c132

Please sign in to comment.