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: Test severity #978

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,11 @@ func (c ExecCheck) GetEndpoint() string {
return *c.Script
}

func (c ExecCheck) GetTestFunction() Template {
func (c ExecCheck) GetTestTemplate() Template {
if c.Test.Expression == "" {
c.Test.Expression = "results.ExitCode == 0"
}
return c.Test
return c.Test.Template
}

type AwsConfigCheck struct {
Expand Down
30 changes: 24 additions & 6 deletions api/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ type Template struct {
Javascript string `yaml:"javascript,omitempty" json:"javascript,omitempty"`
}

type TestThreshold struct {
Info string `yaml:"info,omitempty" json:"info,omitempty"`
Low string `yaml:"low,omitempty" json:"low,omitempty"`
Medium string `yaml:"medium,omitempty" json:"medium,omitempty"`
High string `yaml:"high,omitempty" json:"high,omitempty"`
Critical string `yaml:"critical,omitempty" json:"critical,omitempty"`
}

func (t Template) IsEmpty() bool {
return t.Template == "" && t.JSONPath == "" && t.Expression == "" && t.Javascript == ""
}
Expand All @@ -227,22 +235,32 @@ type DisplayTemplate interface {

// +k8s:deepcopy-gen=false
type TestFunction interface {
GetTestFunction() Template
GetTestTemplate() Template
GetTestThreshold() *TestThreshold
}

// +k8s:deepcopy-gen=false
type Transformer interface {
GetTransformer() Template
}

type TestTemplate struct {
Template `yaml:",inline" json:",inline"`
*TestThreshold `yaml:"threshold,omitempty" json:"threshold,omitempty"`
}

type Templatable struct {
Test Template `yaml:"test,omitempty" json:"test,omitempty"`
Display Template `yaml:"display,omitempty" json:"display,omitempty"`
Transform Template `yaml:"transform,omitempty" json:"transform,omitempty"`
Test TestTemplate `yaml:"test,omitempty" json:"test,omitempty"`
Display Template `yaml:"display,omitempty" json:"display,omitempty"`
Transform Template `yaml:"transform,omitempty" json:"transform,omitempty"`
}

func (t Templatable) GetTestTemplate() Template {
return t.Test.Template
}

func (t Templatable) GetTestFunction() Template {
return t.Test
func (t Templatable) GetTestThreshold() *TestThreshold {
return t.Test.TestThreshold
}

func (t Templatable) GetDisplayTemplate() Template {
Expand Down
74 changes: 55 additions & 19 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion checks/runchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/models"
)

func RunChecks(ctx *context.Context) []*pkg.CheckResult {
Expand Down Expand Up @@ -104,10 +105,14 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult

switch v := r.Check.(type) {
case v1.TestFunction:
tpl := v.GetTestFunction()
data := map[string]any{"duration": r.Duration}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duration should be appended into the default check results object e.g. for a junit it might be:

test:
   threshold:
       high: duration > 10000 || failed / passed > 0.1
       warning: duration >20000 || failed / passed > 0.02
       info: duration >20000 || failed / passed > 0.01

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult {
if r.Duration == 0 && r.GetDuration() > 0 {
r.Duration = r.GetDuration()
}

Duration is added to the check result object at the top of the func

r.Severity = measureTestSeverity(ctx.New(data), v.GetTestThreshold())

tpl := v.GetTestTemplate()
if tpl.IsEmpty() {
break
}

message, err := template(ctx.New(r.Data), tpl)
if err != nil {
r.ErrorMessage(err)
Expand All @@ -122,3 +127,31 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult

return r
}

func measureTestSeverity(ctx *context.Context, threshold *v1.TestThreshold) models.Severity {
if threshold == nil {
return models.SeverityInfo
}

thresholds := []struct {
severity models.Severity
expr string
}{
{models.SeverityCritical, threshold.Critical},
{models.SeverityHigh, threshold.High},
{models.SeverityMedium, threshold.Medium},
{models.SeverityLow, threshold.Low},
{models.SeverityInfo, threshold.Info},
}

for _, t := range thresholds {
if res, err := template(ctx, v1.Template{Expression: t.expr}); err != nil {
logger.Errorf("failed to run expression for severity: %s", t.severity)
continue
} else if res == "true" {
return t.severity
}
}

return models.SeverityInfo
}
Loading