feat(notifications): make it possible to send test alert notifications
closes #5847
This commit is contained in:
@@ -46,6 +46,10 @@ func (n *RootNotifier) Notify(context *EvalContext) {
|
||||
n.log.Error("Failed to upload alert panel image", "error", err)
|
||||
}
|
||||
|
||||
n.sendNotifications(notifiers, context)
|
||||
}
|
||||
|
||||
func (n *RootNotifier) sendNotifications(notifiers []Notifier, context *EvalContext) {
|
||||
for _, notifier := range notifiers {
|
||||
n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
|
||||
go notifier.Notify(context)
|
||||
@@ -53,7 +57,6 @@ func (n *RootNotifier) Notify(context *EvalContext) {
|
||||
}
|
||||
|
||||
func (n *RootNotifier) uploadImage(context *EvalContext) error {
|
||||
|
||||
uploader, _ := imguploader.NewImageUploader()
|
||||
|
||||
imageUrl, err := context.GetImageUrl()
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
type NotificationTestCommand struct {
|
||||
Severity string
|
||||
Name string
|
||||
Type string
|
||||
Settings *simplejson.Json
|
||||
}
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("alerting", handleNotificationTestCommand)
|
||||
|
||||
}
|
||||
|
||||
func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
|
||||
notifier := NewRootNotifier()
|
||||
|
||||
model := &models.AlertNotification{
|
||||
Name: cmd.Name,
|
||||
Type: cmd.Type,
|
||||
Settings: cmd.Settings,
|
||||
}
|
||||
|
||||
notifiers, err := notifier.getNotifierFor(model)
|
||||
|
||||
if err != nil {
|
||||
log.Error2("Failed to create notifier", "error", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
severity := models.AlertSeverityType(cmd.Severity)
|
||||
notifier.sendNotifications([]Notifier{notifiers}, createTestEvalContext(severity))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func createTestEvalContext(severity models.AlertSeverityType) *EvalContext {
|
||||
state := models.AlertStateOK
|
||||
firing := false
|
||||
if severity == models.AlertSeverityCritical {
|
||||
state = models.AlertStateCritical
|
||||
firing = true
|
||||
}
|
||||
if severity == models.AlertSeverityWarning {
|
||||
state = models.AlertStateWarning
|
||||
firing = true
|
||||
}
|
||||
|
||||
testRule := &Rule{
|
||||
DashboardId: 1,
|
||||
PanelId: 1,
|
||||
Name: "Test notification",
|
||||
Message: "Someone is testing the alert notification within grafana.",
|
||||
State: state,
|
||||
Severity: severity,
|
||||
}
|
||||
|
||||
ctx := NewEvalContext(testRule)
|
||||
ctx.ImagePublicUrl = "http://grafana.org/assets/img/blog/mixed_styles.png"
|
||||
|
||||
ctx.IsTestRun = true
|
||||
ctx.Firing = firing
|
||||
ctx.Error = nil
|
||||
ctx.EvalMatches = evalMatchesBasedOnSeverity(severity)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func evalMatchesBasedOnSeverity(severity models.AlertSeverityType) []*EvalMatch {
|
||||
matches := make([]*EvalMatch, 0)
|
||||
if severity == models.AlertSeverityOK {
|
||||
return matches
|
||||
}
|
||||
|
||||
matches = append(matches, &EvalMatch{
|
||||
Metric: "High value",
|
||||
Value: 100,
|
||||
})
|
||||
|
||||
matches = append(matches, &EvalMatch{
|
||||
Metric: "Higher Value",
|
||||
Value: 200,
|
||||
})
|
||||
|
||||
return matches
|
||||
}
|
||||
Reference in New Issue
Block a user