-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented datadog provider with httpclient instead of dd-go-c…
…lient library due to its limitation Signed-off-by: Sudipto Baral <[email protected]>
- Loading branch information
Showing
6 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
metrics-operator/controllers/common/providers/datadog/common.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package datadog | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha2" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var ErrSecretKeyRefNotDefined = errors.New("the SecretKeyRef property with the DataDog API Key is missing") | ||
|
||
func getDDSecret(ctx context.Context, provider metricsapi.KeptnMetricsProvider, k8sClient client.Client) (string, error) { | ||
if !provider.HasSecretDefined() { | ||
return "", ErrSecretKeyRefNotDefined | ||
} | ||
ddCredsSecret := &corev1.Secret{} | ||
if err := k8sClient.Get(ctx, types.NamespacedName{Name: provider.Spec.SecretKeyRef.Name, Namespace: provider.Namespace}, ddCredsSecret); err != nil { | ||
return "", err | ||
} | ||
fmt.Println(provider.Spec.SecretKeyRef.Key) | ||
|
||
apiKey := ddCredsSecret.Data[provider.Spec.SecretKeyRef.Key] | ||
if len(apiKey) == 0 { | ||
return "", fmt.Errorf("secret contains invalid key %s", provider.Spec.SecretKeyRef.Key) | ||
} | ||
return string(apiKey), nil | ||
} |
80 changes: 80 additions & 0 deletions
80
metrics-operator/controllers/common/providers/datadog/datadog.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package datadog | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" | ||
"github.com/go-logr/logr" | ||
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha2" | ||
"io" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
|
||
//nolint:gci | ||
"net/http" //nolint:gci | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type KeptnDataDogProvider struct { | ||
Log logr.Logger | ||
HttpClient http.Client | ||
K8sClient client.Client | ||
} | ||
|
||
// 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) | ||
defer cancel() | ||
|
||
// Assumed default metric duration as 1 day(s) | ||
// Think a better way to handle this | ||
fromTime := time.Now().AddDate(0, 0, -1).Unix() | ||
toTime := time.Now().Unix() | ||
qURL := provider.Spec.TargetServer + "/api/v1/query?from=" + strconv.Itoa(int(fromTime)) + "&to=" + strconv.Itoa(int(toTime)) + "&query=" + url.QueryEscape(metric.Spec.Query) | ||
req, err := http.NewRequestWithContext(ctx, "GET", qURL, nil) | ||
if err != nil { | ||
d.Log.Error(err, "Error while creating request") | ||
return "", nil, err | ||
} | ||
|
||
apiKey, err := getDDSecret(ctx, provider, d.K8sClient) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
appKey := "appkey" // TODO: retrieve this from kubernetes secret | ||
|
||
req.Header.Set("Accept", "application/json") | ||
req.Header.Set("Dd-Api-Key", apiKey) | ||
req.Header.Set("Dd-Application-Key", appKey) | ||
|
||
res, err := d.HttpClient.Do(req) | ||
if err != nil { | ||
d.Log.Error(err, "Error while creating request") | ||
return "", nil, err | ||
} | ||
defer func() { | ||
err := res.Body.Close() | ||
if err != nil { | ||
d.Log.Error(err, "Could not close request body") | ||
} | ||
}() | ||
|
||
b, _ := io.ReadAll(res.Body) | ||
result := datadogV1.MetricsQueryResponse{} | ||
err = json.Unmarshal(b, &result) | ||
if err != nil { | ||
d.Log.Error(err, "Error while parsing response") | ||
return "", nil, err | ||
} | ||
|
||
if len(result.Series) == 0 { | ||
d.Log.Info("No values in query result") | ||
return "", nil, fmt.Errorf("no values in query result") | ||
} | ||
|
||
points := (result.Series)[0].Pointlist | ||
value := strconv.FormatFloat(*points[len(points)-1][1], 'g', 5, 64) | ||
return value, b, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters