Added custom messages for uptime alerts
This commit is contained in:
@@ -30,13 +30,16 @@ func (d *Dispatcher) OnTransition(check *config.Check, from, to checks.State, sn
|
||||
if to == checks.StateUnknown {
|
||||
return
|
||||
}
|
||||
msg := Render(d.selfID, check, from, to, snap)
|
||||
alerts := d.cluster.EffectiveAlertsFor(check)
|
||||
if len(alerts) == 0 && len(check.AlertIDs) > 0 {
|
||||
d.logger.Printf("alerts: check %q references alerts but none resolved", check.Name)
|
||||
}
|
||||
for i := range alerts {
|
||||
alert := alerts[i]
|
||||
msg, err := RenderFor(&alert, d.selfID, check, from, to, snap)
|
||||
if err != nil {
|
||||
d.logger.Printf("alerts: %q template: %v — falling back to default", alert.Name, err)
|
||||
}
|
||||
if err := d.dispatchOne(&alert, msg); err != nil {
|
||||
d.logger.Printf("alerts: %q via %s: %v", alert.Name, alert.Type, err)
|
||||
}
|
||||
@@ -44,15 +47,32 @@ func (d *Dispatcher) OnTransition(check *config.Check, from, to checks.State, sn
|
||||
}
|
||||
|
||||
// Test sends a one-shot test message to the named alert. Returns an
|
||||
// error so the CLI can surface failures interactively.
|
||||
// error so the CLI can surface failures interactively. If the alert
|
||||
// carries custom templates they are exercised against a synthetic
|
||||
// "homepage going DOWN" transition so the operator can confirm the
|
||||
// template renders before a real outage.
|
||||
func (d *Dispatcher) Test(alertID string) error {
|
||||
alert := d.cluster.FindAlert(alertID)
|
||||
if alert == nil {
|
||||
return fmt.Errorf("alert %q not found", alertID)
|
||||
}
|
||||
msg := Message{
|
||||
Subject: "[quptime] test alert",
|
||||
Body: fmt.Sprintf("This is a test of alert %q from node %s.\nIf you see this, the alert channel is wired correctly.\n", alert.Name, d.selfID),
|
||||
if alert.SubjectTemplate == "" && alert.BodyTemplate == "" {
|
||||
msg := Message{
|
||||
Subject: "[quptime] test alert",
|
||||
Body: fmt.Sprintf("This is a test of alert %q from node %s.\nIf you see this, the alert channel is wired correctly.\n", alert.Name, d.selfID),
|
||||
}
|
||||
return d.dispatchOne(alert, msg)
|
||||
}
|
||||
sample := &config.Check{
|
||||
ID: "test-check",
|
||||
Name: "test-check",
|
||||
Type: config.CheckHTTP,
|
||||
Target: "https://example.com",
|
||||
}
|
||||
snap := checks.Snapshot{Reports: 3, OKCount: 0, NotOK: 3, Detail: "synthetic test failure"}
|
||||
msg, err := RenderFor(alert, d.selfID, sample, checks.StateUp, checks.StateDown, snap)
|
||||
if err != nil {
|
||||
return fmt.Errorf("render template: %w", err)
|
||||
}
|
||||
return d.dispatchOne(alert, msg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user