-
Notifications
You must be signed in to change notification settings - Fork 11
/
get_sli_triggered_event_handler.go
79 lines (63 loc) · 2.96 KB
/
get_sli_triggered_event_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package handler
import (
api "github.com/keptn/go-utils/pkg/api/utils"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
"github.com/keptn/go-utils/pkg/sdk"
)
type GetSliEventHandler struct {
}
func NewGetSliEventHandler() *GetSliEventHandler {
return &GetSliEventHandler{}
}
// Execute handles get-sli.triggered events if SLIProvider == keptn-service-template-go
// This function acts as an example showing how to handle get-sli events
// TODO: Adapt handler code to your needs
func (g *GetSliEventHandler) Execute(k sdk.IKeptn, event sdk.KeptnEvent) (interface{}, *sdk.Error) {
k.Logger().Infof("Handling get-sli.triggered Event: %s", event.ID)
sliTriggeredEvent := &keptnv2.GetSLITriggeredEventData{}
if err := keptnv2.Decode(event.Data, sliTriggeredEvent); err != nil {
return nil, &sdk.Error{Err: err, StatusType: keptnv2.StatusErrored, ResultType: keptnv2.ResultFailed, Message: "failed to decode sli.triggered event: " + err.Error()}
}
// Check if the event belongs to our SLI Provider
if sliTriggeredEvent.GetSLI.SLIProvider != "keptn-service-template-go" {
k.Logger().Infof("Not handling get-sli event as it is meant for %s", sliTriggeredEvent.GetSLI.SLIProvider)
return nil, nil
}
// Get SLI File from keptn-service-template-go subdirectory of the config repo - to add the file use:
sliFile := "keptn-service-template-go/sli.yaml"
resourceScope := *api.NewResourceScope().Project(sliTriggeredEvent.Project).Stage(sliTriggeredEvent.Stage).Service(sliTriggeredEvent.Service).Resource(sliFile)
sliConfigFileContent, err := k.GetResourceHandler().GetResource(resourceScope)
if err != nil {
k.Logger().Infof("Error while fetching SLI file: %e", err)
return nil, &sdk.Error{Err: err, StatusType: keptnv2.StatusErrored, ResultType: keptnv2.ResultFailed, Message: "error while fetching SLI file: " + err.Error()}
}
k.Logger().Infof("SLI config content: %s", sliConfigFileContent)
// TODO: Implement your functionality here
indicators := sliTriggeredEvent.GetSLI.Indicators
var sliResults []*keptnv2.SLIResult
for _, indicatorName := range indicators {
sliResult := &keptnv2.SLIResult{
Metric: indicatorName,
Value: 123.4, // TODO: Fetch the values from your monitoring tool here
}
sliResults = append(sliResults, sliResult)
}
finishedEventData := getSliFinishedEvent(keptnv2.ResultPass, keptnv2.StatusSucceeded, *sliTriggeredEvent, "", sliResults)
return finishedEventData, nil
}
func getSliFinishedEvent(result keptnv2.ResultType, status keptnv2.StatusType, sliTriggeredEvent keptnv2.GetSLITriggeredEventData, message string, sliResult []*keptnv2.SLIResult) keptnv2.GetSLIFinishedEventData {
return keptnv2.GetSLIFinishedEventData{
EventData: keptnv2.EventData{
Project: sliTriggeredEvent.Project,
Stage: sliTriggeredEvent.Stage,
Service: sliTriggeredEvent.Service,
Labels: sliTriggeredEvent.Labels,
Status: status,
Result: result,
Message: message,
},
GetSLI: keptnv2.GetSLIFinished{
IndicatorValues: sliResult,
},
}
}