Update triggerType to be a tag in the output of alerts

pull/10616/head
Chris Goller 2016-11-03 14:21:17 -05:00
parent c5d9c342c6
commit 5424bc6e8c
7 changed files with 35 additions and 32 deletions

View File

@ -33,7 +33,7 @@ func Data(rule chronograf.AlertRule) (string, error) {
stream = fmt.Sprintf("%s\n.groupBy(groupby)\n", stream)
stream = stream + ".where(where_filter)\n"
// Only need aggregate functions for threshold and relative
if rule.Type != "deadman" {
if rule.Trigger != "deadman" {
for _, field := range rule.Query.Fields {
for _, fnc := range field.Funcs {
// Only need a window if we have an aggregate function

View File

@ -1,21 +1,17 @@
package kapacitor
import (
"fmt"
"github.com/influxdata/chronograf"
)
import "github.com/influxdata/chronograf"
// InfluxOut creates a kapacitor influxDBOut node to write alert data to Database, RP, Measurement.
func InfluxOut(rule chronograf.AlertRule) string {
return fmt.Sprintf(`
return `
trigger
|influxDBOut()
.create()
.database(output_db)
.retentionPolicy(output_rp)
.measurement(output_mt)
.tag('name', '%s')
.tag('type', '%s')
`, rule.Name, rule.Type)
.tag('name', name)
.tag('triggerType', triggerType)
`
}

View File

@ -16,8 +16,8 @@ func TestInfluxOut(t *testing.T) {
.database(output_db)
.retentionPolicy(output_rp)
.measurement(output_mt)
.tag('name', 'name')
.tag('type', 'deadman')
.tag('name', name)
.tag('triggerType', triggerType)
`,
},
}

View File

@ -36,7 +36,7 @@ func (a *Alert) Generate(rule chronograf.AlertRule) (chronograf.TICKScript, erro
return "", err
}
if err := validateTick(tick); err != nil {
return "", err
return tick, err
}
return tick, nil
}

View File

@ -1,6 +1,7 @@
package kapacitor
import (
"fmt"
"testing"
"github.com/influxdata/chronograf"
@ -53,8 +54,9 @@ func TestGenerate(t *testing.T) {
},
}
gen := Alert{}
_, err := gen.Generate(alert)
tick, err := gen.Generate(alert)
if err != nil {
fmt.Printf("%s", tick)
t.Errorf("Error generating alert: %v", err)
}
}
@ -154,6 +156,8 @@ var output_rp = 'autogen'
var output_mt = 'alerts'
var triggerType = 'threshold'
var crit = 90
var data = stream
@ -192,8 +196,8 @@ trigger
.database(output_db)
.retentionPolicy(output_rp)
.measurement(output_mt)
.tag('name', 'name')
.tag('type', 'stream')
.tag('name', name)
.tag('triggerType', triggerType)
`,
wantErr: false,
},
@ -306,6 +310,8 @@ var output_rp = 'autogen'
var output_mt = 'alerts'
var triggerType = 'relative'
var shift = -1m
var crit = 90
@ -358,8 +364,8 @@ trigger
.database(output_db)
.retentionPolicy(output_rp)
.measurement(output_mt)
.tag('name', 'name')
.tag('type', 'stream')
.tag('name', name)
.tag('triggerType', triggerType)
`,
wantErr: false,
},
@ -464,7 +470,7 @@ var messagefield = 'message'
var durationfield = 'duration'
var metric = 'metric'
var metric = 'usage_user'
var output_db = 'chronograf'
@ -472,6 +478,8 @@ var output_rp = 'autogen'
var output_mt = 'alerts'
var triggerType = 'deadman'
var threshold = 0.0
var data = stream
@ -481,12 +489,6 @@ var data = stream
.measurement(measurement)
.groupBy(groupby)
.where(where_filter)
|window()
.period(period)
.every(every)
.align()
|mean(field)
.as(metric)
var trigger = data
|deadman(threshold, every)
@ -507,8 +509,8 @@ trigger
.database(output_db)
.retentionPolicy(output_rp)
.measurement(output_mt)
.tag('name', 'name')
.tag('type', 'stream')
.tag('name', name)
.tag('triggerType', triggerType)
`,
wantErr: false,
},

View File

@ -13,6 +13,6 @@ func Trigger(rule chronograf.AlertRule) (string, error) {
case "threshold":
return execTemplate(ThresholdTrigger, rule)
default:
return "", fmt.Errorf("Unknown trigger type: %s", rule.Type)
return "", fmt.Errorf("Unknown trigger type: %s", rule.Trigger)
}
}

View File

@ -97,6 +97,7 @@ func commonVars(rule chronograf.AlertRule) (string, error) {
var output_db = '%s'
var output_rp = '%s'
var output_mt = '%s'
var triggerType = '%s'
`
return fmt.Sprintf(common,
rule.Query.Database,
@ -113,10 +114,11 @@ func commonVars(rule chronograf.AlertRule) (string, error) {
LevelField,
MessageField,
DurationField,
metric(rule.Query),
metric(rule),
Database,
RP,
Measurement,
rule.Trigger,
), nil
}
@ -136,9 +138,12 @@ func field(q chronograf.QueryConfig) (string, error) {
}
// metric will be metric unless there are no field aggregates. If no aggregates, then it is the field name.
func metric(q chronograf.QueryConfig) string {
for _, field := range q.Fields {
if field.Field != "" && len(field.Funcs) == 0 {
func metric(rule chronograf.AlertRule) string {
for _, field := range rule.Query.Fields {
// Deadman triggers do not need any aggregate functions
if field.Field != "" && rule.Trigger == "deadman" {
return field.Field
} else if field.Field != "" && len(field.Funcs) == 0 {
return field.Field
}
}