Skip to content

Commit

Permalink
feat: experiment on fetching SLOs from datadog.
Browse files Browse the repository at this point in the history
Signed-off-by: Sudipto Baral <[email protected]>
  • Loading branch information
sudiptob2 committed Feb 27, 2023
1 parent ed563da commit 2e8ed67
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
23 changes: 21 additions & 2 deletions operator/controllers/common/providers/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package datadog

import (
"context"
"encoding/json"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/go-logr/logr"
klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2"
"net/http"
"time"
)

type KeptnDataDogProvider struct {
Expand All @@ -13,6 +17,21 @@ type KeptnDataDogProvider struct {
}

func (k KeptnDataDogProvider) EvaluateQuery(ctx context.Context, objective klcv1alpha2.Objective, provider klcv1alpha2.KeptnEvaluationProvider) (string, []byte, error) {
//TODO implement me
panic("implement me")

ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

k.Log.Info("Running query: " + provider.Spec.TargetServer + "/api/v1/slo")

cfg := datadog.NewConfiguration()
client := datadog.NewAPIClient(cfg)
api := datadogV1.NewServiceLevelObjectivesApi(client)
resp, _, err := api.ListSLOs(ctx, *datadogV1.NewListSLOsOptionalParameters())

if err != nil {
return "", nil, err
}

responseContent, _ := json.MarshalIndent(resp, "", " ")
return string(responseContent), responseContent, nil
}
57 changes: 57 additions & 0 deletions operator/controllers/common/providers/datadog/datadog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package datadog

import (
"context"
"fmt"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"net/http"
"os"
"testing"

klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
)

func Test_datadog(t *testing.T) {

t.Run("datadog-test", func(t *testing.T) {

kpp := KeptnDataDogProvider{
HttpClient: http.Client{},
Log: ctrl.Log.WithName("testytest"),
}
obj := klcv1alpha2.Objective{
Query: "garbage",
}
p := klcv1alpha2.KeptnEvaluationProvider{
Spec: klcv1alpha2.KeptnEvaluationProviderSpec{
SecretKeyRef: v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "myapitoken",
},
Key: "mykey",
},
TargetServer: "http://mockserver", // TODO: pass mock server url
},
}
ctx := context.WithValue(
context.Background(),
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: "DD_CLIENT_API_KEY",
},
"appKeyAuth": {
Key: "DD_CLIENT_APP_KEY",
},
},
)
r, _, _ := kpp.EvaluateQuery(ctx, obj, p)
fmt.Fprintf(os.Stdout, r)
require.Equal(t, "0", "1")

})

}

0 comments on commit 2e8ed67

Please sign in to comment.