Merge pull request #354 from prometheus/feature/send-prom-metadata

Transfer alerting rule and Prometheus URL to alertmanager.
This commit is contained in:
juliusv 2013-08-12 03:10:17 -07:00
commit 483f2be3b3
3 changed files with 25 additions and 6 deletions

View file

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

View file

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

View file

@ -18,8 +18,10 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
"net"
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
"os"
"code.google.com/p/gorest" "code.google.com/p/gorest"
@ -64,7 +66,7 @@ func (w WebService) ServeForever() error {
exp.HandleFunc("/graph", graphHandler) exp.HandleFunc("/graph", graphHandler)
exp.Handle("/api/", compressionHandler{handler: gorest.Handle()}) exp.Handle("/api/", compressionHandler{handler: gorest.Handle()})
exp.Handle("/metrics.json", prometheus.DefaultHandler) exp.Handle("/metrics", prometheus.DefaultHandler)
if *useLocalAssets { if *useLocalAssets {
exp.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static")))) exp.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
} else { } else {
@ -137,3 +139,15 @@ func executeTemplate(w http.ResponseWriter, name string, data interface{}) {
log.Printf("Error executing template: %s", err) 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)
}