sentry.go 762 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package sentry
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/getsentry/sentry-go"
  6. "github.com/imgproxy/imgproxy/v3/config"
  7. )
  8. var (
  9. enabled bool
  10. timeout = 5 * time.Second
  11. )
  12. func Init() {
  13. if len(config.SentryDSN) > 0 {
  14. sentry.Init(sentry.ClientOptions{
  15. Dsn: config.SentryDSN,
  16. Release: config.SentryRelease,
  17. Environment: config.SentryEnvironment,
  18. })
  19. enabled = true
  20. }
  21. }
  22. func Report(err error, req *http.Request, meta map[string]any) {
  23. if !enabled {
  24. return
  25. }
  26. hub := sentry.CurrentHub().Clone()
  27. hub.Scope().SetRequest(req)
  28. hub.Scope().SetLevel(sentry.LevelError)
  29. if meta != nil {
  30. hub.Scope().SetContext("Processing context", meta)
  31. }
  32. eventID := hub.CaptureException(err)
  33. if eventID != nil {
  34. hub.Flush(timeout)
  35. }
  36. }