Transfer alerting rule and Prometheus URL to alertmanager.

This commit is contained in:
Julius Volz 2013-08-09 18:09:44 +02:00
parent c03712dc58
commit ecf0ee8f39
3 changed files with 25 additions and 6 deletions

View File

@ -202,8 +202,8 @@ func main() {
}
go ruleManager.Run()
// Queue depth will need to be exposed
notificationHandler := notification.NewNotificationHandler(*alertmanagerUrl, notifications)
prometheusUrl := web.MustBuildServerUrl()
notificationHandler := notification.NewNotificationHandler(*alertmanagerUrl, prometheusUrl, notifications)
go notificationHandler.Run()
flags := map[string]string{}

View File

@ -42,6 +42,8 @@ var (
type NotificationHandler struct {
// The URL of the alert manager to send notifications to.
alertmanagerUrl string
// The URL of this Prometheus instance to include in notifications.
prometheusUrl string
// Buffer of notifications that have not yet been sent.
pendingNotifications <-chan rules.NotificationReqs
// HTTP client with custom timeout settings.
@ -49,11 +51,12 @@ type NotificationHandler struct {
}
// Construct a new NotificationHandler.
func NewNotificationHandler(alertmanagerUrl string, notificationReqs <-chan rules.NotificationReqs) *NotificationHandler {
func NewNotificationHandler(alertmanagerUrl string, prometheusUrl string, notificationReqs <-chan rules.NotificationReqs) *NotificationHandler {
return &NotificationHandler{
alertmanagerUrl: alertmanagerUrl,
pendingNotifications: notificationReqs,
httpClient: utility.NewDeadlineClient(*deadline),
prometheusUrl: prometheusUrl,
}
}
@ -68,8 +71,10 @@ func (n *NotificationHandler) sendNotifications(reqs rules.NotificationReqs) err
rules.AlertNameLabel: clientmodel.LabelValue(req.Rule.Name()),
}),
"Payload": map[string]interface{}{
"Value": req.ActiveAlert.Value,
"ActiveSince": req.ActiveAlert.ActiveSince,
"Value": req.ActiveAlert.Value,
"ActiveSince": req.ActiveAlert.ActiveSince,
"GeneratorUrl": n.prometheusUrl,
"AlertingRule": req.Rule.String(),
},
})
}

View File

@ -18,8 +18,10 @@ import (
"fmt"
"html/template"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
"code.google.com/p/gorest"
@ -64,7 +66,7 @@ func (w WebService) ServeForever() error {
exp.HandleFunc("/graph", graphHandler)
exp.Handle("/api/", compressionHandler{handler: gorest.Handle()})
exp.Handle("/metrics.json", prometheus.DefaultHandler)
exp.Handle("/metrics", prometheus.DefaultHandler)
if *useLocalAssets {
exp.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
} else {
@ -137,3 +139,15 @@ func executeTemplate(w http.ResponseWriter, name string, data interface{}) {
log.Printf("Error executing template: %s", err)
}
}
func MustBuildServerUrl() string {
_, port, err := net.SplitHostPort(*listenAddress)
if err != nil {
panic(err)
}
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
return fmt.Sprintf("http://%s:%s", hostname, port)
}