airbrake.go 824 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package airbrake
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/airbrake/gobrake/v5"
  6. "github.com/imgproxy/imgproxy/v3/config"
  7. )
  8. var (
  9. notifier *gobrake.Notifier
  10. metaReplacer = strings.NewReplacer(" ", "-")
  11. )
  12. func Init() {
  13. if len(config.AirbrakeProjectKey) > 0 {
  14. notifier = gobrake.NewNotifierWithOptions(&gobrake.NotifierOptions{
  15. ProjectId: int64(config.AirbrakeProjectID),
  16. ProjectKey: config.AirbrakeProjectKey,
  17. Environment: config.AirbrakeEnv,
  18. })
  19. }
  20. }
  21. func Report(err error, req *http.Request, meta map[string]any) {
  22. if notifier == nil {
  23. return
  24. }
  25. notice := notifier.Notice(err, req, 2)
  26. for k, v := range meta {
  27. key := metaReplacer.Replace(strings.ToLower(k))
  28. notice.Context[key] = v
  29. }
  30. notifier.SendNoticeAsync(notice)
  31. }
  32. func Close() {
  33. if notifier != nil {
  34. notifier.Close()
  35. }
  36. }