Created
February 4, 2023 06:59
-
-
Save hindungWang/9d06b0cfa7286d29b4f985720ee10a41 to your computer and use it in GitHub Desktop.
alert-manager-receiver-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"io" | |
"log" | |
"net/http" | |
"github.com/k0kubun/pp/v3" | |
"github.com/prometheus/alertmanager/notify/webhook" | |
) | |
func main() { | |
addr := flag.String("addr", ":8080", "address to listen for webhook") | |
flag.Parse() | |
http.HandleFunc("/healthz", healthzHandler) | |
http.HandleFunc("/alerts", alertsHandler) | |
log.Fatal(http.ListenAndServe(*addr, nil)) | |
} | |
func healthzHandler(w http.ResponseWriter, r *http.Request) { | |
io.WriteString(w, "ok\n") | |
} | |
func alertsHandler(w http.ResponseWriter, r *http.Request) { | |
switch r.Method { | |
case http.MethodPost: | |
postHandler(w, r) | |
default: | |
http.Error(w, "unsupported HTTP method", 400) | |
} | |
} | |
func postHandler(w http.ResponseWriter, r *http.Request) { | |
dec := json.NewDecoder(r.Body) | |
defer r.Body.Close() | |
var m webhook.Message | |
if err := dec.Decode(&m); err != nil { | |
log.Printf("error decoding message: %v", err) | |
http.Error(w, "invalid request body", 400) | |
return | |
} | |
pp.Println(m) | |
w.WriteHeader(http.StatusOK) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment