mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(alerting): skeleton commit for webhook
This commit is contained in:
@@ -12,6 +12,14 @@ type SendEmailCommand struct {
|
|||||||
Info string
|
Info string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SendWebhook struct {
|
||||||
|
Url string
|
||||||
|
AuthUser string
|
||||||
|
AuthPassword string
|
||||||
|
Body string
|
||||||
|
Method string
|
||||||
|
}
|
||||||
|
|
||||||
type SendResetPasswordEmailCommand struct {
|
type SendResetPasswordEmailCommand struct {
|
||||||
User *User
|
User *User
|
||||||
}
|
}
|
||||||
@@ -134,6 +134,7 @@ func (e *Engine) saveState(result *AlertResult) {
|
|||||||
query := &m.GetAlertByIdQuery{Id: result.AlertJob.Rule.Id}
|
query := &m.GetAlertByIdQuery{Id: result.AlertJob.Rule.Id}
|
||||||
bus.Dispatch(query)
|
bus.Dispatch(query)
|
||||||
|
|
||||||
|
e.notifier.Notify(result)
|
||||||
if query.Result.ShouldUpdateState(result.State) {
|
if query.Result.ShouldUpdateState(result.State) {
|
||||||
cmd := &m.UpdateAlertStateCommand{
|
cmd := &m.UpdateAlertStateCommand{
|
||||||
AlertId: result.AlertJob.Rule.Id,
|
AlertId: result.AlertJob.Rule.Id,
|
||||||
@@ -146,7 +147,7 @@ func (e *Engine) saveState(result *AlertResult) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
e.log.Debug("will notify! about", "new state", result.State)
|
e.log.Debug("will notify! about", "new state", result.State)
|
||||||
e.notifier.Notify(result)
|
|
||||||
} else {
|
} else {
|
||||||
e.log.Debug("state remains the same!")
|
e.log.Debug("state remains the same!")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,20 +51,36 @@ type EmailNotifier struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *EmailNotifier) Dispatch(alertResult *AlertResult) {
|
func (this *EmailNotifier) Dispatch(alertResult *AlertResult) {
|
||||||
//bus.dispath to notification package in grafana
|
/*
|
||||||
this.log.Info("Sending email")
|
this.log.Info("Sending email")
|
||||||
|
cmd := &m.SendEmailCommand{
|
||||||
|
Data: map[string]interface{}{},
|
||||||
|
To: []string{},
|
||||||
|
Info: "",
|
||||||
|
Massive: false,
|
||||||
|
Template: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
bus.Dispatch(cmd)
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebhookNotifier struct {
|
type WebhookNotifier struct {
|
||||||
Url string
|
Url string
|
||||||
|
Method string
|
||||||
AuthUser string
|
AuthUser string
|
||||||
AuthPassword string
|
AuthPassword string
|
||||||
log log.Logger
|
log log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *WebhookNotifier) Dispatch(alertResult *AlertResult) {
|
func (this *WebhookNotifier) Dispatch(alertResult *AlertResult) {
|
||||||
//bus.dispath to notification package in grafana
|
|
||||||
this.log.Info("Sending webhook")
|
this.log.Info("Sending webhook")
|
||||||
|
cmd := &m.SendWebhook{
|
||||||
|
Url: this.Url,
|
||||||
|
Method: this.Method,
|
||||||
|
}
|
||||||
|
|
||||||
|
bus.Dispatch(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
type NotificationDispatcher interface {
|
type NotificationDispatcher interface {
|
||||||
@@ -115,6 +131,7 @@ var createNotifier = func(notificationType string, settings *simplejson.Json) No
|
|||||||
|
|
||||||
return &WebhookNotifier{
|
return &WebhookNotifier{
|
||||||
Url: settings.Get("url").MustString(),
|
Url: settings.Get("url").MustString(),
|
||||||
|
Method: settings.Get("method").MustString(),
|
||||||
AuthUser: settings.Get("user").MustString(),
|
AuthUser: settings.Get("user").MustString(),
|
||||||
AuthPassword: settings.Get("password").MustString(),
|
AuthPassword: settings.Get("password").MustString(),
|
||||||
log: log.New("alerting.notification.webhook"),
|
log: log.New("alerting.notification.webhook"),
|
||||||
|
|||||||
@@ -23,11 +23,14 @@ var tmplWelcomeOnSignUp = "welcome_on_signup.html"
|
|||||||
|
|
||||||
func Init() error {
|
func Init() error {
|
||||||
initMailQueue()
|
initMailQueue()
|
||||||
|
initWebhookQueue()
|
||||||
|
|
||||||
bus.AddHandler("email", sendResetPasswordEmail)
|
bus.AddHandler("email", sendResetPasswordEmail)
|
||||||
bus.AddHandler("email", validateResetPasswordCode)
|
bus.AddHandler("email", validateResetPasswordCode)
|
||||||
bus.AddHandler("email", sendEmailCommandHandler)
|
bus.AddHandler("email", sendEmailCommandHandler)
|
||||||
|
|
||||||
|
bus.AddHandler("webhook", sendWebhook)
|
||||||
|
|
||||||
bus.AddEventListener(signUpStartedHandler)
|
bus.AddEventListener(signUpStartedHandler)
|
||||||
bus.AddEventListener(signUpCompletedHandler)
|
bus.AddEventListener(signUpCompletedHandler)
|
||||||
|
|
||||||
@@ -53,6 +56,18 @@ func Init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendWebhook(cmd *m.SendWebhook) error {
|
||||||
|
addToWebhookQueue(&Webhook{
|
||||||
|
Url: cmd.Url,
|
||||||
|
AuthUser: cmd.AuthUser,
|
||||||
|
AuthPassword: cmd.AuthPassword,
|
||||||
|
Method: cmd.Method,
|
||||||
|
Body: cmd.Body,
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func subjectTemplateFunc(obj map[string]interface{}, value string) string {
|
func subjectTemplateFunc(obj map[string]interface{}, value string) string {
|
||||||
obj["value"] = value
|
obj["value"] = value
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
62
pkg/services/notifications/webhook.go
Normal file
62
pkg/services/notifications/webhook.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package notifications
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Webhook struct {
|
||||||
|
Url string
|
||||||
|
AuthUser string
|
||||||
|
AuthPassword string
|
||||||
|
Body string
|
||||||
|
Method string
|
||||||
|
}
|
||||||
|
|
||||||
|
var webhookQueue chan *Webhook
|
||||||
|
var webhookLog log.Logger
|
||||||
|
|
||||||
|
func initWebhookQueue() {
|
||||||
|
webhookLog = log.New("notifications.webhook")
|
||||||
|
webhookQueue = make(chan *Webhook, 10)
|
||||||
|
go processWebhookQueue()
|
||||||
|
}
|
||||||
|
|
||||||
|
func processWebhookQueue() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case webhook := <-webhookQueue:
|
||||||
|
err := sendWebRequest(webhook)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
webhookLog.Error("Failed to send webrequest ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendWebRequest(webhook *Webhook) error {
|
||||||
|
webhookLog.Error("Sending stuff! ", "url", webhook.Url)
|
||||||
|
|
||||||
|
client := http.Client{Timeout: time.Duration(3 * time.Second)}
|
||||||
|
|
||||||
|
request, err := http.NewRequest(webhook.Method, webhook.Url, nil /*io.reader*/)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var addToWebhookQueue = func(msg *Webhook) {
|
||||||
|
webhookQueue <- msg
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user