From 476277f219d6d48e3bc653d17fd9a0fb67450f65 Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Mon, 31 Oct 2016 19:19:32 -0500 Subject: [PATCH] Add alert interface --- .gitignore | 1 + chronograf.go | 13 +++++++++++-- kapacitor/alerts.go | 27 ++++++++++++++++++++------- kapacitor/alerts_test.go | 12 ++++++++---- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 7b86fcf60..1de95340d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ node_modules/ build/ chronograf.db npm-debug.log +.vscode diff --git a/chronograf.go b/chronograf.go index 52f409012..de1bedaa4 100644 --- a/chronograf.go +++ b/chronograf.go @@ -68,7 +68,7 @@ type Source struct { Name string `json:"name"` // Name is the user-defined name for the source Type string `json:"type,omitempty"` // Type specifies which kinds of source (enterprise vs oss) Username string `json:"username,omitempty"` // Username is the username to connect to the source - Password string `json:"password,omitempty"` // Password is in CLEARTEXT FIXME + Password string `json:"password,omitempty"` // Password is in CLEARTEXT // TODO: fixme URL string `json:"url"` // URL are the connections to the source Default bool `json:"default"` // Default specifies the default source for the application } @@ -87,13 +87,22 @@ type SourcesStore interface { Update(context.Context, Source) error } +// TickTemplate task to be used by kapacitor +type TickTemplate string + +// Alert generates tickscript templates for kapacitor +type Alert interface { + // Generate will create the tickscript to be used as a kapacitor template + Generate() (TickTemplate, error) +} + // Server represents a proxy connection to an HTTP server type Server struct { ID int // ID is the unique ID of the server SrcID int // SrcID of the data source Name string // Name is the user-defined name for the server Username string // Username is the username to connect to the server - Password string // Password is in CLEARTEXT FIXME + Password string // Password is in CLEARTEXT // TODO: FIXME URL string // URL are the connections to the server } diff --git a/kapacitor/alerts.go b/kapacitor/alerts.go index 16fc18e8d..0cca087c2 100644 --- a/kapacitor/alerts.go +++ b/kapacitor/alerts.go @@ -7,23 +7,36 @@ import ( "text/template" "time" + "github.com/influxdata/chronograf" "github.com/influxdata/kapacitor/pipeline" "github.com/influxdata/kapacitor/tick/ast" "github.com/influxdata/kapacitor/tick/stateful" ) +var _ chronograf.Alert = &Alert{} + // Alert defines alerting strings in template rendering type Alert struct { + Trigger string // Specifies the type of alert Service string // Alerting service Operator string // Operator for alert comparison Aggregate string // Statistic aggregate over window of data } -// TickTemplate task to be used by kapacitor -type TickTemplate string +func (a *Alert) Generate() (chronograf.TickTemplate, error) { + switch a.Trigger { + case "threshold": + return a.Threshold() + case "relative": + return a.Relative() + case "deadman": + return a.Deadman() + } + return "", fmt.Errorf("Unknown tigger mechanism %s", a.Trigger) +} // Threshold generates a tickscript template with an alert -func (a *Alert) Threshold() (TickTemplate, error) { +func (a *Alert) Threshold() (chronograf.TickTemplate, error) { if err := ValidateAlert(a); err != nil { return "", err } @@ -41,7 +54,7 @@ func (a *Alert) Threshold() (TickTemplate, error) { } // Relative creates a tickscript that alerts on relative changes over windows of data -func (a *Alert) Relative() (TickTemplate, error) { +func (a *Alert) Relative() (chronograf.TickTemplate, error) { if err := ValidateAlert(a); err != nil { return "", err } @@ -59,7 +72,7 @@ func (a *Alert) Relative() (TickTemplate, error) { } // Deadman creates a tickscript that alerts when no data has been received for a time. -func (a *Alert) Deadman() (TickTemplate, error) { +func (a *Alert) Deadman() (chronograf.TickTemplate, error) { if err := ValidateAlert(a); err != nil { return "", err } @@ -85,7 +98,7 @@ func ValidateAlert(alert *Alert) error { return validateTick(script) } -func formatTick(tickscript string) (TickTemplate, error) { +func formatTick(tickscript string) (chronograf.TickTemplate, error) { node, err := ast.Parse(tickscript) if err != nil { log.Fatalf("parse execution: %s", err) @@ -94,7 +107,7 @@ func formatTick(tickscript string) (TickTemplate, error) { output := new(bytes.Buffer) node.Format(output, "", true) - return TickTemplate(output.String()), nil + return chronograf.TickTemplate(output.String()), nil } func validateTick(script string) error { diff --git a/kapacitor/alerts_test.go b/kapacitor/alerts_test.go index 32ca6fffe..194c9032f 100644 --- a/kapacitor/alerts_test.go +++ b/kapacitor/alerts_test.go @@ -1,6 +1,10 @@ package tickscripts -import "testing" +import ( + "testing" + + "github.com/influxdata/chronograf" +) func TestValidateAlert(t *testing.T) { tests := []struct { @@ -65,7 +69,7 @@ func TestThreshold(t *testing.T) { tests := []struct { name string alert Alert - want TickTemplate + want chronograf.TickTemplate wantErr bool }{ { @@ -139,7 +143,7 @@ func TestRelative(t *testing.T) { tests := []struct { name string alert Alert - want TickTemplate + want chronograf.TickTemplate wantErr bool }{ { @@ -256,7 +260,7 @@ func TestDeadman(t *testing.T) { tests := []struct { name string alert Alert - want TickTemplate + want chronograf.TickTemplate wantErr bool }{ {