Skip to content

Commit

Permalink
Add support for setting the alert message as a field (influxdata#878)
Browse files Browse the repository at this point in the history
* add message field for alerts

* CHANGELOG.md

* add batch tests
  • Loading branch information
Nathaniel Cook authored Sep 8, 2016
1 parent 0c07b2f commit 65c9e19
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Features

- [#873](https://github.com/influxdata/kapacitor/pull/873): Add TCP alert handler
- [#869](https://github.com/influxdata/kapacitor/issues/869): Add ability to set alert message as a field

### Bugfixes

Expand Down
13 changes: 10 additions & 3 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,14 @@ func (a *AlertNode) runAlert([]byte) error {
p.Tags[a.a.IdTag] = ad.ID
}
}
if a.a.LevelField != "" || a.a.IdField != "" || a.a.DurationField != "" {
if a.a.LevelField != "" || a.a.IdField != "" || a.a.DurationField != "" || a.a.MessageField != "" {
p.Fields = p.Fields.Copy()
if a.a.LevelField != "" {
p.Fields[a.a.LevelField] = l.String()
}
if a.a.MessageField != "" {
p.Fields[a.a.MessageField] = ad.Message
}
if a.a.IdField != "" {
p.Fields[a.a.IdField] = ad.ID
}
Expand Down Expand Up @@ -541,7 +544,8 @@ func (a *AlertNode) runAlert([]byte) error {
a.a.LevelField != "" ||
a.a.IdTag != "" ||
a.a.IdField != "" ||
a.a.DurationField != "" {
a.a.DurationField != "" ||
a.a.MessageField != "" {
for i := range b.Points {
if a.a.LevelTag != "" || a.a.IdTag != "" {
b.Points[i].Tags = b.Points[i].Tags.Copy()
Expand All @@ -552,11 +556,14 @@ func (a *AlertNode) runAlert([]byte) error {
b.Points[i].Tags[a.a.IdTag] = ad.ID
}
}
if a.a.LevelField != "" || a.a.IdField != "" || a.a.DurationField != "" {
if a.a.LevelField != "" || a.a.IdField != "" || a.a.DurationField != "" || a.a.MessageField != "" {
b.Points[i].Fields = b.Points[i].Fields.Copy()
if a.a.LevelField != "" {
b.Points[i].Fields[a.a.LevelField] = l.String()
}
if a.a.MessageField != "" {
b.Points[i].Fields[a.a.MessageField] = ad.Message
}
if a.a.IdField != "" {
b.Points[i].Fields[a.a.IdField] = ad.ID
}
Expand Down
58 changes: 58 additions & 0 deletions integrations/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,64 @@ batch
testBatcherWithOutput(t, "TestBatch_SimpleMR", script, 30*time.Second, er, false)
}

func TestBatch_AlertMessage(t *testing.T) {

var script = `
batch
|query('''
SELECT mean("value")
FROM "telegraf"."default".cpu_usage_idle
WHERE "host" = 'serverA' AND "cpu" != 'cpu-total'
''')
.period(10s)
.every(10s)
.groupBy(time(2s), 'cpu')
|alert()
.crit(lambda:"mean" > 95)
.messageField('msg')
|httpOut('TestBatch_SimpleMR')
`

er := kapacitor.Result{
Series: imodels.Rows{
{
Name: "cpu_usage_idle",
Tags: map[string]string{"cpu": "cpu1"},
Columns: []string{"time", "mean", "msg"},
Values: [][]interface{}{
{
time.Date(1971, 1, 1, 0, 0, 20, 0, time.UTC),
96.49999999996908,
"cpu_usage_idle:cpu=cpu1 is CRITICAL",
},
{
time.Date(1971, 1, 1, 0, 0, 22, 0, time.UTC),
93.46464646468584,
"cpu_usage_idle:cpu=cpu1 is CRITICAL",
},
{
time.Date(1971, 1, 1, 0, 0, 24, 0, time.UTC),
95.00950095007724,
"cpu_usage_idle:cpu=cpu1 is CRITICAL",
},
{
time.Date(1971, 1, 1, 0, 0, 26, 0, time.UTC),
92.99999999998636,
"cpu_usage_idle:cpu=cpu1 is CRITICAL",
},
{
time.Date(1971, 1, 1, 0, 0, 28, 0, time.UTC),
90.99999999998545,
"cpu_usage_idle:cpu=cpu1 is CRITICAL",
},
},
},
},
}

testBatcherWithOutput(t, "TestBatch_SimpleMR", script, 30*time.Second, er, false)
}

func TestBatch_AlertStateChangesOnly(t *testing.T) {
requestCount := int32(0)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
4 changes: 3 additions & 1 deletion integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,7 @@ stream
.idField('id')
.idTag('id')
.levelField('level')
.messageField('msg')
.levelTag('level')
.info(lambda: "count" > infoThreshold)
.warn(lambda: "count" > warnThreshold)
Expand All @@ -4549,12 +4550,13 @@ stream
{
Name: "cpu",
Tags: map[string]string{"host": "serverA", "level": "CRITICAL", "id": "kapacitor/cpu/serverA"},
Columns: []string{"time", "count", "id", "level"},
Columns: []string{"time", "count", "id", "level", "msg"},
Values: [][]interface{}{[]interface{}{
time.Date(1971, 1, 1, 0, 0, 10, 0, time.UTC),
10.0,
"kapacitor/cpu/serverA",
"CRITICAL",
"kapacitor/cpu/serverA is CRITICAL",
}},
},
},
Expand Down
3 changes: 3 additions & 0 deletions pipeline/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ type AlertNode struct {
// Optional field key to add to the data, containing the alert level as a string.
LevelField string

// Optional field key to add to the data, containing the alert message.
MessageField string

// Optional field key to add the alert duration to the data.
// The duration is always in units of nanoseconds.
DurationField string
Expand Down

0 comments on commit 65c9e19

Please sign in to comment.