Skip to content

Commit

Permalink
images: Add metalk8s-alert-logger
Browse files Browse the repository at this point in the history
This is a simple HTTP server, listening
on a port (default to 19094), waiting for
HTTP post request from alertmanager.
It then logs the content of these requests
to stdout.

Refs: #3180
  • Loading branch information
alexandre-allard committed Apr 6, 2021
1 parent 217bd5a commit d1185a8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions images/metalk8s-alert-logger/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module metalk8s-alert-logger

go 1.16

require github.com/prometheus/alertmanager @@ALERTMANAGER_VERSION@@
49 changes: 49 additions & 0 deletions images/metalk8s-alert-logger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"encoding/json"
"flag"
"log"
"net/http"

"github.com/prometheus/alertmanager/template"
)

func main() {
address := flag.String("address", ":19094", "address and port of service")
flag.Parse()

log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))

http.HandleFunc("/", logAlert)
http.HandleFunc("/ready", serverIsRunning)
http.HandleFunc("/health", serverIsRunning)
if err := http.ListenAndServe(*address, nil); err != nil {
log.Fatalf("Failed to start HTTP server: %v", err)
}
}

func serverIsRunning(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}

func logAlert(w http.ResponseWriter, r *http.Request) {
var alerts template.Data

if err := json.NewDecoder(r.Body).Decode(&alerts); err != nil {
log.Printf("Unable to parse HTTP body: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

for _, alert := range alerts.Alerts {
encoded_alert, err := json.Marshal(alert)
if err != nil {
log.Error(err)
} else {
log.Println(string(encoded_alert))
}
}

w.WriteHeader(http.StatusNoContent)
}

0 comments on commit d1185a8

Please sign in to comment.