Skip to content

Commit

Permalink
fix: add support for setting timeout through env variable
Browse files Browse the repository at this point in the history
- bump image tag to 0.2.0
- add sample sli and slo
- use podtatohead for test events
  • Loading branch information
vadasambar committed Mar 2, 2022
1 parent f73609e commit 79649bd
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 17 deletions.
25 changes: 20 additions & 5 deletions eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@ import (
"github.com/vadasambar/datadog-service/pkg/utils"
)

const sliFile = "datadog/sli.yaml"
const apiSleep = 30
const (
sliFile = "datadog/sli.yaml"
defaultSleepBeforeAPIInSeconds = 30
)

// We have to put a min of 30s of sleep for the datadog API to reflect the data correctly
// More info: https://github.com/keptn-sandbox/datadog-service/issues/8
var sleepBeforeAPIInSeconds int

func init() {
var err error
sleepBeforeAPIInSeconds, err = strconv.Atoi(strings.TrimSpace(os.Getenv("SLEEP_BEFORE_API_IN_SECONDS")))
if err != nil || sleepBeforeAPIInSeconds < defaultSleepBeforeAPIInSeconds {
logger.Infof("defaulting SLEEP_BEFORE_API_IN_SECONDS to 30s because it was set to '%v' which is less than the min allowed value of 30s", sleepBeforeAPIInSeconds)
sleepBeforeAPIInSeconds = defaultSleepBeforeAPIInSeconds
}
}

// HandleGetSliTriggeredEvent handles get-sli.triggered events if SLIProvider == datadog
func HandleGetSliTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.GetSLITriggeredEventData) error {
Expand All @@ -29,7 +44,7 @@ func HandleGetSliTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevent

// Step 1 - Do we need to do something?
// Lets make sure we are only processing an event that really belongs to our SLI Provider
if data.GetSLI.SLIProvider != "datadog-service" {
if data.GetSLI.SLIProvider != "datadog" {
logger.Infof("Not handling get-sli event as it is meant for %s", data.GetSLI.SLIProvider)
return nil
}
Expand Down Expand Up @@ -108,8 +123,8 @@ func HandleGetSliTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevent
// Pulling the data from Datadog api immediately gives incorrect data in api response
// we have to wait for some time for the correct data to be reflected in the api response
// TODO: Find a better way around the sleep time for datadog api
logger.Debugf("waiting for %vs so that the metrics data is reflected correctly in the api", apiSleep)
time.Sleep(time.Second * apiSleep)
logger.Debugf("waiting for %vs so that the metrics data is reflected correctly in the api", sleepBeforeAPIInSeconds)
time.Sleep(time.Second * time.Duration(sleepBeforeAPIInSeconds))

query := replaceQueryParameters(data, sliConfig[indicatorName], start, end)
logger.Debugf("actual query sent to datadog: %v, from: %v, to: %v", query, start.Unix(), end.Unix())
Expand Down
4 changes: 2 additions & 2 deletions helm/templates/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
type: Opaque
data:
DD_API_KEY: {{ required "A valid DD_API_KEY is required to connect to the Datadog API" .Values.datadogservice.ddApikey | b64enc | quote }}
DD_APP_KEY: {{ required "A valid DD_API_KEY is required to connect to the Datadog API" .Values.datadogservice.ddAppKey | b64enc | quote }}
DD_SITE: {{ required "A valid DD_API_KEY is required to connect to the Datadog API" .Values.datadogservice.ddSite | b64enc | quote }}
DD_APP_KEY: {{ required "A valid DD_APP_KEY is required to connect to the Datadog API" .Values.datadogservice.ddAppKey | b64enc | quote }}
DD_SITE: {{ required "A valid DD_SITE is required to connect to the Datadog API" .Values.datadogservice.ddSite | b64enc | quote }}

{{- end -}}
2 changes: 1 addition & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ datadogservice:
image:
repository: ghcr.io/keptn-sandbox/datadog-service # Container Image Name
pullPolicy: IfNotPresent # Kubernetes Image Pull Policy
tag: 0.1.0 # Container Tag
tag: 0.2.0 # Container Tag
service:
enabled: true # Creates a Kubernetes Service for the datadog-service

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (

var keptnOptions = keptn.KeptnOpts{}

const envVarLogLevel = "LOG_LEVEL"
const (
envVarLogLevel = "LOG_LEVEL"
)

type envConfig struct {
// Port on which to listen for cloudevents
Expand Down
9 changes: 9 additions & 0 deletions resources/sli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
spec_version: '1.0'
indicators:
# http_response_time_seconds_main_page_sum: sum(rate(http_server_request_duration_seconds_sum{method="GET",route="/",status_code="200",job="$SERVICE-$PROJECT-$STAGE"}[$DURATION_SECONDS])/rate(http_server_request_duration_seconds_count{method="GET",route="/",status_code="200",job="$SERVICE-$PROJECT-$STAGE"}[$DURATION_SECONDS]))
# failing_request: promhttp_metric_handler_requests_total{code!="200",job="$SERVICE-$PROJECT-$STAGE"}
# http_requests_total_sucess: http_requests_total{status="success"}
# go_routines: go_goroutines{job="$SERVICE-$PROJECT-$STAGE"}
# request_throughput: sum(rate(http_requests_total{status="success"}[2m]))
system_load: avg:system.load.1{*}.rollup(avg, $DURATION)
23 changes: 23 additions & 0 deletions resources/slo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
spec_version: '0.1.0'
comparison:
compare_with: "single_result"
include_result_with_score: "pass"
aggregate_function: avg
objectives:
- sli: system_load
displayName: "System Load"
pass:
- criteria:
- "<0.6"
warning:
- criteria:
- ">=0.6"
- "<0.7"
fail:
- criteria:
- ">0.7"

total_score:
pass: "100%"
warning: "50%"
6 changes: 3 additions & 3 deletions test-events/evaluation.triggered.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
},
"labels": null,
"message": "",
"project": "sockshop",
"project": "podtatohead",
"result": "",
"service": "carts",
"stage": "staging",
"service": "helloservice",
"stage": "hardening",
"status": "",
"test": {
"end": "",
Expand Down
9 changes: 4 additions & 5 deletions test-events/get-sli.triggered.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
"customFilters": [],
"end": "2021-01-15T15:09:45.000Z",
"indicators": [
"response_time_p95",
"some_other_metric"
"system_load"
],
"sliProvider": "datadog-service",
"start": "2021-01-15T15:04:45.000Z"
},
"labels": null,
"message": "",
"project": "sockshop",
"project": "podtatohead",
"result": "",
"service": "carts",
"stage": "staging",
"service": "helloservice",
"stage": "hardening",
"status": ""
},
"id": "409539ae-c0b9-436e-abc6-c257292e28ff",
Expand Down

0 comments on commit 79649bd

Please sign in to comment.