bugsnag.go 818 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package bugsnag
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "net/http"
  6. "github.com/bugsnag/bugsnag-go/v2"
  7. "github.com/imgproxy/imgproxy/v3/config"
  8. )
  9. var enabled bool
  10. type logger struct{}
  11. func (l logger) Printf(format string, v ...interface{}) {
  12. slog.Debug(
  13. fmt.Sprintf(format, v...),
  14. "source", "bugsnag",
  15. )
  16. }
  17. func Init() {
  18. if len(config.BugsnagKey) > 0 {
  19. bugsnag.Configure(bugsnag.Configuration{
  20. APIKey: config.BugsnagKey,
  21. ReleaseStage: config.BugsnagStage,
  22. PanicHandler: func() {}, // Disable forking the process
  23. Logger: logger{},
  24. })
  25. enabled = true
  26. }
  27. }
  28. func Report(err error, req *http.Request, meta map[string]any) {
  29. if !enabled {
  30. return
  31. }
  32. extra := make(bugsnag.MetaData)
  33. for k, v := range meta {
  34. extra.Add("Processing Context", k, v)
  35. }
  36. bugsnag.Notify(err, req, extra)
  37. }