package prometheus import ( "fmt" "net/http" libHTTP "dev.sum7.eu/genofire/golang-lib/http" "github.com/bdlm/log" xmpp "github.com/mattn/go-xmpp" "github.com/prometheus/alertmanager/notify" "dev.sum7.eu/genofire/hook2xmpp/runtime" ) const hookType = "prometheus" func init() { runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) { log.WithField("type", hookType).Info("loaded") return func(w http.ResponseWriter, r *http.Request) { logger := log.WithField("type", hookType) var request notify.WebhookMessage if err := libHTTP.Read(r, &request); err != nil { logger.Errorf("no readable payload: %s", err) http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError) return } contentTag := request.Status if len(request.Alerts.Firing) > 0 { contentTag += ":" + len(request.Alerts.Firing) } content := fmt.Sprintf("[%s] %s", contentTag, strings.Join(request.CommonAnnotations.Values()," ")) logger = logger.WithField("body", content) ok := false for _, hook := range hooks { if request.Payload.VCSURL != hook.Secret { continue } logger.Infof("run hook") runtime.Notify(client, hook, content) ok = true } if !ok { logger.Warnf("no hook found") http.Error(w, fmt.Sprintf("no configuration for %s for url: %s", hookType, request.ExternalURL), http.StatusNotFound) } } } }