|
| 1 | +package slack |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/apex/log" |
| 13 | +) |
| 14 | + |
| 15 | +// SetupLogHandler changes the log handler to send errors, fatals and warns to slack |
| 16 | +func SetupLogHandler(webhookURL string) { |
| 17 | + messageEnv := os.Getenv("SLACK_ENVIRONMENT") |
| 18 | + originalHandler := log.Log.(*log.Logger).Handler.HandleLog |
| 19 | + var handler log.HandlerFunc = func(entry *log.Entry) error { |
| 20 | + handleLogEntry(entry, webhookURL, messageEnv) |
| 21 | + return originalHandler(entry) |
| 22 | + } |
| 23 | + log.SetHandler(handler) |
| 24 | +} |
| 25 | + |
| 26 | +func handleLogEntry(entry *log.Entry, webhookURL string, messageEnv string) { |
| 27 | + // IMPORTANT: |
| 28 | + // Do not send error, fatal or warn logs in this method as it will cause a n+1 loop |
| 29 | + |
| 30 | + switch entry.Level { |
| 31 | + case log.WarnLevel, log.FatalLevel, log.ErrorLevel: |
| 32 | + // Continue |
| 33 | + default: |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + text := fmt.Sprintf("*RTCV %s %s: %s*", messageEnv, entry.Level, strings.TrimSpace(entry.Message)) |
| 38 | + |
| 39 | + if len(entry.Fields) != 0 { |
| 40 | + text += "\n\nFields:" |
| 41 | + for key, value := range entry.Fields { |
| 42 | + text += fmt.Sprintf("\n%s: %+v", key, value) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + type m = map[string]any |
| 47 | + body, err := json.Marshal(m{ |
| 48 | + "text": "", |
| 49 | + "blocks": []m{{ |
| 50 | + "type": "section", |
| 51 | + "text": m{"type": "mrkdwn", "text": text}, |
| 52 | + }}, |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + log.WithError(err).Info("Unable to marshal log entry meant for slack webhook") |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(body)) |
| 60 | + if err != nil { |
| 61 | + log.WithError(err).Info("Unable create post request for slack webhook") |
| 62 | + return |
| 63 | + } |
| 64 | + req.Header.Set("Content-Type", "application/json") |
| 65 | + resp, err := http.DefaultClient.Do(req) |
| 66 | + if err != nil { |
| 67 | + log.WithError(err).Info("Unable to post to slack webhook") |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + if resp.StatusCode >= 400 { |
| 72 | + respBody, err := ioutil.ReadAll(resp.Body) |
| 73 | + if err != nil { |
| 74 | + log.WithError(err).Info(fmt.Sprintf("slack webhook returned status code \"%s\" with a unreadable message", resp.Status)) |
| 75 | + } else { |
| 76 | + log.Info(fmt.Sprintf("slack webhook returned status code \"%s\" with message: %s", resp.Status, string(respBody))) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments