Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: datadog evaluation provider support #869

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions operator/controllers/common/providers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DynatraceProviderName = "dynatrace"
const DynatraceDQLProviderName = "dql"
const PrometheusProviderName = "prometheus"
const KeptnMetricProviderName = "keptn-metric"
const DataDogProviderName = "datadog"

func GetDefaultMetricProvider(namespace string) *klcv1alpha2.KeptnEvaluationProvider {
return &klcv1alpha2.KeptnEvaluationProvider{
Expand Down
64 changes: 64 additions & 0 deletions operator/controllers/common/providers/datadog/datadog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package datadog

import (
"context"
"fmt"
"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"
"strconv"
"time"
)

type KeptnDataDogProvider struct {
Log logr.Logger
HttpClient http.Client
}

func (k KeptnDataDogProvider) EvaluateQuery(ctx context.Context, objective klcv1alpha2.Objective, provider klcv1alpha2.KeptnEvaluationProvider) (string, []byte, error) {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

// TODO: get DD_API_KEY and DD_APP_KEY from kubernetes secret
// TODO: patch the context with the keys
// Ref: https://github.com/DataDog/datadog-api-client-go#getting-started

ctx = context.WithValue(
ctx,
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: "DD_KEY",
},
"appKeyAuth": {
Key: "DD_APP_KEY",
},
},
)
fromTime := time.Now().AddDate(0, 0, -1)
queryTime := time.Now()

configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
api := datadogV1.NewMetricsApi(apiClient)

resp, _, err := api.QueryMetrics(
ctx,
fromTime.Unix(),
queryTime.Unix(),
objective.Query,
)
if err != nil {
k.Log.Error(err, "Error while creating request")
return "", nil, err
}
if len(resp.Series) == 0 {
k.Log.Info("No values in query result")
return "", nil, fmt.Errorf("no values in query result")
}
points := (resp.Series)[0].Pointlist
value := strconv.FormatFloat(*points[len(points)-1][1], 'g', 5, 64)
return value, []byte(value), 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")

})

}
82 changes: 82 additions & 0 deletions operator/controllers/common/providers/datadog/example_resp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"data": [
{
"created_at": 1677413713,
"creator": {
"email": "[email protected]",
"handle": "[email protected]",
"name": "sudipto baral"
},
"description": "string",
"id": "691bd5bdd78b5b848a03efc882f6bccb",
"modified_at": 1677491641,
"monitor_tags": [

],
"name": "TEST_SLO_1",
"query": {
"denominator": "sum:httpservice.hits{!code:3xx}.as_count()",
"numerator": "sum:httpservice.hits{code:2xx}.as_count()"
},
"tags": [
"app:core",
"env:prod"
],
"target_threshold": 97,
"thresholds": [
{
"target": 97,
"target_display": "97.",
"timeframe": "7d",
"warning": 98,
"warning_display": "98."
}
],
"timeframe": "7d",
"type": "metric",
"warning_threshold": 98
},
{
"created_at": 1677491606,
"creator": {
"email": "[email protected]",
"handle": "[email protected]",
"name": "sudipto baral"
},
"description": "string",
"id": "9b1f168123175d6b86a8a6dfb8193f6f",
"modified_at": 1677491606,
"monitor_tags": [

],
"name": "TEST_SLO_2",
"query": {
"denominator": "sum:httpservice.hits{!code:3xx}.as_count()",
"numerator": "sum:httpservice.hits{code:2xx}.as_count()"
},
"tags": [
"app:core",
"env:prod"
],
"target_threshold": 97,
"thresholds": [
{
"target": 97,
"target_display": "97.",
"timeframe": "7d",
"warning": 98,
"warning_display": "98."
}
],
"timeframe": "7d",
"type": "metric",
"warning_threshold": 98
}
],
"metadata": {
"page": {
"total_count": 2,
"total_filtered_count": 2
}
}
}
6 changes: 6 additions & 0 deletions operator/controllers/common/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/go-logr/logr"
klcv1alpha2 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha2"
"github.com/keptn/lifecycle-toolkit/operator/controllers/common/providers/datadog"
"github.com/keptn/lifecycle-toolkit/operator/controllers/common/providers/dynatrace"
"github.com/keptn/lifecycle-toolkit/operator/controllers/common/providers/keptnmetric"
"github.com/keptn/lifecycle-toolkit/operator/controllers/common/providers/prometheus"
Expand Down Expand Up @@ -43,6 +44,11 @@ func NewProvider(provider string, log logr.Logger, k8sClient client.Client) (Kep
Log: log,
K8sClient: k8sClient,
}, nil
case DataDogProviderName:
return &datadog.KeptnDataDogProvider{
Log: log,
HttpClient: http.Client{},
}, nil
default:
return nil, fmt.Errorf("provider %s not supported", provider)
}
Expand Down
2 changes: 2 additions & 0 deletions operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/keptn/lifecycle-toolkit/operator
go 1.19

require (
github.com/DataDog/datadog-api-client-go/v2 v2.9.0
github.com/benbjohnson/clock v1.3.0
github.com/go-logr/logr v1.2.3
github.com/gorilla/mux v1.8.0
Expand Down Expand Up @@ -48,6 +49,7 @@ require (

require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/DataDog/zstd v1.5.0 // indirect
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-api-client-go/v2 v2.9.0 h1:1Cz3mqj95iqnQPykEovq2p52rrU26XvLC2Fz6hPE+TU=
github.com/DataDog/datadog-api-client-go/v2 v2.9.0/go.mod h1:sHt3EuVMN8PSYJu065qwp3pZxCwR3RZP4sJnYwj/ZQY=
github.com/DataDog/zstd v1.5.0 h1:+K/VEwIAaPcHiMtQvpLD4lqW7f0Gk3xdYZmI1hD+CXo=
github.com/DataDog/zstd v1.5.0/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down