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

Make Alerta event property configurable #461

Merged
merged 2 commits into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ For example, let's say we want to store all data that triggered an alert in Infl
- [#426](https://github.com/influxdata/kapacitor/issues/426): Add `skip-format` query parameter to the `GET /task` endpoint so that returned TICKscript content is left unmodified from the user input.
- [#388](https://github.com/influxdata/kapacitor/issues/388): The duration of an alert is now tracked and exposed as part of the alert data as well as can be set as a field via `.durationField('duration')`.
- [#486](https://github.com/influxdata/kapacitor/pull/486): Default config file location.
- [#461](https://github.com/influxdata/kapacitor/pull/461): Make Alerta `event` property configurable.

### Bugfixes

Expand Down
16 changes: 15 additions & 1 deletion alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, l *log.Logger) (an *
if err != nil {
return nil, err
}
evtmpl, err := text.New("event").Parse(alerta.Event)
if err != nil {
return nil, err
}
etmpl, err := text.New("environment").Parse(alerta.Environment)
if err != nil {
return nil, err
Expand All @@ -247,6 +251,7 @@ func newAlertNode(et *ExecutingTask, n *pipeline.AlertNode, l *log.Logger) (an *
ai := alertaHandler{
AlertaHandler: alerta,
resourceTmpl: rtmpl,
eventTmpl: evtmpl,
environmentTmpl: etmpl,
groupTmpl: gtmpl,
valueTmpl: vtmpl,
Expand Down Expand Up @@ -873,6 +878,7 @@ type alertaHandler struct {
*pipeline.AlertaHandler

resourceTmpl *text.Template
eventTmpl *text.Template
environmentTmpl *text.Template
valueTmpl *text.Template
groupTmpl *text.Template
Expand Down Expand Up @@ -907,6 +913,14 @@ func (a *AlertNode) handleAlerta(alerta alertaHandler, ad *AlertData) {
resource := buf.String()
buf.Reset()

err = alerta.eventTmpl.Execute(&buf, ad.info)
Copy link
Contributor

Choose a reason for hiding this comment

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

Right here you can create a new type like:

type eventData struct {
      idInfo
      ID string
}
data := eventData{
     idInfo: ad.info.messageInfo.idInfo,
     ID: ad.ID,
}
err = alerta.eventTmpl.Execute(&buf, data)

This way on the ID Info data can be used in addition to the ID itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Updated.

if err != nil {
a.logger.Printf("E! failed to evaluate Alerta Event template %s", alerta.Event)
return
}
event := buf.String()
buf.Reset()

err = alerta.environmentTmpl.Execute(&buf, ad.info)
if err != nil {
a.logger.Printf("E! failed to evaluate Alerta Environment template %s", alerta.Environment)
Expand Down Expand Up @@ -938,7 +952,7 @@ func (a *AlertNode) handleAlerta(alerta alertaHandler, ad *AlertData) {
err = a.et.tm.AlertaService.Alert(
alerta.Token,
resource,
ad.ID,
event,
environment,
severity,
group,
Expand Down
14 changes: 9 additions & 5 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,9 @@ func TestStream_AlertAlerta(t *testing.T) {
if exp := "cpu"; pd.Resource != exp {
t.Errorf("unexpected resource got %s exp %s", pd.Resource, exp)
}
if exp := "serverA"; pd.Event != exp {
t.Errorf("unexpected event got %s exp %s", pd.Event, exp)
}
if exp := "production"; pd.Environment != exp {
t.Errorf("unexpected environment got %s exp %s", pd.Environment, exp)
}
Expand All @@ -2726,9 +2729,12 @@ func TestStream_AlertAlerta(t *testing.T) {
if exp := "/alert?api-key=anothertesttoken"; r.URL.String() != exp {
t.Errorf("unexpected url got %s exp %s", r.URL.String(), exp)
}
if exp := "resource: cpu"; pd.Resource != exp {
if exp := "resource: serverA"; pd.Resource != exp {
t.Errorf("unexpected resource got %s exp %s", pd.Resource, exp)
}
if exp := "event: TestStream_Alert"; pd.Event != exp {
t.Errorf("unexpected event got %s exp %s", pd.Event, exp)
}
if exp := "serverA"; pd.Environment != exp {
t.Errorf("unexpected environment got %s exp %s", pd.Environment, exp)
}
Expand All @@ -2745,9 +2751,6 @@ func TestStream_AlertAlerta(t *testing.T) {
t.Errorf("unexpected origin got %s exp %s", pd.Origin, exp)
}
}
if exp := "serverA"; pd.Event != exp {
t.Errorf("unexpected event got %s exp %s", pd.Event, exp)
}
if exp := "kapacitor/cpu/serverA is CRITICAL @1971-01-01 00:00:10 +0000 UTC"; pd.Text != exp {
t.Errorf("unexpected text got %s exp %s", pd.Text, exp)
}
Expand Down Expand Up @@ -2775,7 +2778,8 @@ stream
.environment('production')
.alerta()
.token('anothertesttoken')
.resource('resource: {{ .Name }}')
.resource('resource: {{ index .Tags "host" }}')
.event('event: {{ .TaskName }}')
.environment('{{ index .Tags "host" }}')
.origin('override')
.group('{{ .ID }}')
Expand Down
7 changes: 7 additions & 0 deletions pipeline/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,13 +751,15 @@ func (a *AlertNode) Alerta() *AlertaHandler {
alerta := &AlertaHandler{
AlertNode: a,
Resource: defaultAlertaResource,
Event: defaultAlertaEvent,
Group: defaultAlertaGroup,
}
a.AlertaHandlers = append(a.AlertaHandlers, alerta)
return alerta
}

const defaultAlertaResource = "{{ .Name }}"
const defaultAlertaEvent = "{{ .ID }}"
const defaultAlertaGroup = "{{ .Group }}"

// tick:embedded:AlertNode.Alerta
Expand All @@ -773,6 +775,11 @@ type AlertaHandler struct {
// Default: {{ .Name }}
Resource string

// Alerta event.
// Can be a template and has access to the same data as the AlertNode.Details property.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update this comment since it uses on the Info struct and not the Details?

// Default: {{ .ID }}
Event string

// Alerta environment.
// Can be a template and has access to the same data as the AlertNode.Details property.
// Defaut is set from the configuration.
Expand Down