sentry.go 660 B

123456789101112131415161718192021222324252627282930313233343536373839
  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) {
  23. if enabled {
  24. hub := sentry.CurrentHub().Clone()
  25. hub.Scope().SetRequest(req)
  26. hub.Scope().SetLevel(sentry.LevelError)
  27. eventID := hub.CaptureException(err)
  28. if eventID != nil {
  29. hub.Flush(timeout)
  30. }
  31. }
  32. }