Advertisement
Guest User

signoz

a guest
Mar 12th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bytes"
  5. "context"
  6. "encoding/json"
  7. "log"
  8. "net/http"
  9. "os"
  10. "os/signal"
  11. "syscall"
  12. "time"
  13.  
  14. "github.com/gorilla/mux"
  15. "gopkg.in/natefinch/lumberjack.v2"
  16. )
  17.  
  18. type SignozAlertResPayload struct {
  19. Channel string `json:"channel"`
  20. Attachments []struct {
  21. Title string `json:"title"`
  22. Text string `json:"text"`
  23. Fallback string `json:"fallback"`
  24. CallbackID string `json:"callback_id"`
  25. Footer string `json:"footer"`
  26. MrkdwnIn []string `json:"mrkdwn_in"`
  27. } `json:"attachments"`
  28. }
  29.  
  30. func handler(w http.ResponseWriter, r *http.Request) {
  31.  
  32. webhook_url := os.Getenv("SQUADCAST_WEBHOOK_URL")
  33. decoder := json.NewDecoder(r.Body)
  34. var sp SignozAlertResPayload
  35. err := decoder.Decode(&sp)
  36. if err != nil {
  37. panic(err)
  38. }
  39. for _, alert := range sp.Attachments {
  40. createIncident(webhook_url, alert.Title, alert.Text)
  41. }
  42.  
  43. }
  44.  
  45. func main() {
  46.  
  47. r := mux.NewRouter()
  48. r.HandleFunc("/", handler)
  49.  
  50. srv := &http.Server{
  51. Handler: r,
  52. Addr: ":80",
  53. ReadTimeout: 10 * time.Second,
  54. WriteTimeout: 10 * time.Second,
  55. }
  56.  
  57. // Configure Logging
  58. LOG_FILE_LOCATION := os.Getenv("LOG_FILE_LOCATION")
  59. if LOG_FILE_LOCATION != "" {
  60. log.SetOutput(&lumberjack.Logger{
  61. Filename: LOG_FILE_LOCATION,
  62. MaxSize: 500, // megabytes
  63. MaxBackups: 3,
  64. MaxAge: 28, //days
  65. Compress: true, // disabled by default
  66. })
  67. }
  68.  
  69. // Start Server
  70. go func() {
  71. log.Println("Starting Server..")
  72. if err := srv.ListenAndServe(); err != nil {
  73. log.Fatal(err)
  74. }
  75. }()
  76.  
  77. // Graceful Shutdown
  78. waitForShutdown(srv)
  79. }
  80.  
  81. func waitForShutdown(srv *http.Server) {
  82. interruptChan := make(chan os.Signal, 1)
  83. signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
  84.  
  85. // Block until we receive our signal.
  86. <-interruptChan
  87.  
  88. // Create a deadline to wait for.
  89. ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
  90. defer cancel()
  91. srv.Shutdown(ctx)
  92.  
  93. log.Println("Shutting down")
  94. os.Exit(0)
  95. }
  96.  
  97. // createIncident creates the incident in Squadcast with Signoz payload
  98. func createIncident(webhook_url, title, description string) {
  99. postBody, _ := json.Marshal(map[string]string{
  100. "message": title,
  101. "description": description,
  102. })
  103.  
  104. resp, err := http.Post(webhook_url, "application/json",
  105. bytes.NewBuffer(postBody))
  106.  
  107. if err != nil {
  108. log.Fatalf("An Error Occured %v", err)
  109. }
  110. defer resp.Body.Close()
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement