errorreport.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package errorreport
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/imgproxy/imgproxy/v3/errorreport/airbrake"
  6. "github.com/imgproxy/imgproxy/v3/errorreport/bugsnag"
  7. "github.com/imgproxy/imgproxy/v3/errorreport/honeybadger"
  8. "github.com/imgproxy/imgproxy/v3/errorreport/sentry"
  9. )
  10. type metaCtxKey struct{}
  11. func Init() {
  12. bugsnag.Init()
  13. honeybadger.Init()
  14. sentry.Init()
  15. airbrake.Init()
  16. }
  17. func StartRequest(req *http.Request) context.Context {
  18. meta := make(map[string]any)
  19. return context.WithValue(req.Context(), metaCtxKey{}, meta)
  20. }
  21. func SetMetadata(req *http.Request, key string, value any) {
  22. meta, ok := req.Context().Value(metaCtxKey{}).(map[string]any)
  23. if !ok || meta == nil {
  24. return
  25. }
  26. meta[key] = value
  27. }
  28. func Report(err error, req *http.Request) {
  29. meta, ok := req.Context().Value(metaCtxKey{}).(map[string]any)
  30. if !ok {
  31. meta = nil
  32. }
  33. bugsnag.Report(err, req, meta)
  34. honeybadger.Report(err, req, meta)
  35. sentry.Report(err, req, meta)
  36. airbrake.Report(err, req, meta)
  37. }
  38. func Close() {
  39. airbrake.Close()
  40. }