Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bytes"
- "context"
- "encoding/json"
- "log"
- "net/http"
- "os"
- "os/signal"
- "syscall"
- "time"
- "github.com/gorilla/mux"
- "gopkg.in/natefinch/lumberjack.v2"
- )
- type SignozAlertResPayload struct {
- Channel string `json:"channel"`
- Attachments []struct {
- Title string `json:"title"`
- Text string `json:"text"`
- Fallback string `json:"fallback"`
- CallbackID string `json:"callback_id"`
- Footer string `json:"footer"`
- MrkdwnIn []string `json:"mrkdwn_in"`
- } `json:"attachments"`
- }
- func handler(w http.ResponseWriter, r *http.Request) {
- webhook_url := os.Getenv("SQUADCAST_WEBHOOK_URL")
- decoder := json.NewDecoder(r.Body)
- var sp SignozAlertResPayload
- err := decoder.Decode(&sp)
- if err != nil {
- panic(err)
- }
- for _, alert := range sp.Attachments {
- createIncident(webhook_url, alert.Title, alert.Text)
- }
- }
- func main() {
- r := mux.NewRouter()
- r.HandleFunc("/", handler)
- srv := &http.Server{
- Handler: r,
- Addr: ":80",
- ReadTimeout: 10 * time.Second,
- WriteTimeout: 10 * time.Second,
- }
- // Configure Logging
- LOG_FILE_LOCATION := os.Getenv("LOG_FILE_LOCATION")
- if LOG_FILE_LOCATION != "" {
- log.SetOutput(&lumberjack.Logger{
- Filename: LOG_FILE_LOCATION,
- MaxSize: 500, // megabytes
- MaxBackups: 3,
- MaxAge: 28, //days
- Compress: true, // disabled by default
- })
- }
- // Start Server
- go func() {
- log.Println("Starting Server..")
- if err := srv.ListenAndServe(); err != nil {
- log.Fatal(err)
- }
- }()
- // Graceful Shutdown
- waitForShutdown(srv)
- }
- func waitForShutdown(srv *http.Server) {
- interruptChan := make(chan os.Signal, 1)
- signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
- // Block until we receive our signal.
- <-interruptChan
- // Create a deadline to wait for.
- ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
- defer cancel()
- srv.Shutdown(ctx)
- log.Println("Shutting down")
- os.Exit(0)
- }
- // createIncident creates the incident in Squadcast with Signoz payload
- func createIncident(webhook_url, title, description string) {
- postBody, _ := json.Marshal(map[string]string{
- "message": title,
- "description": description,
- })
- resp, err := http.Post(webhook_url, "application/json",
- bytes.NewBuffer(postBody))
- if err != nil {
- log.Fatalf("An Error Occured %v", err)
- }
- defer resp.Body.Close()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement