sentry.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package sentry
  2. import (
  3. "context"
  4. log "kpt-tmr-group/pkg/logger/logrus"
  5. "github.com/getsentry/sentry-go"
  6. )
  7. func MustInit(dsn string, options ...*sentry.ClientOptions) {
  8. var option *sentry.ClientOptions
  9. if len(options) != 0 {
  10. option = options[0]
  11. } else {
  12. option = &sentry.ClientOptions{
  13. Dsn: dsn,
  14. AttachStacktrace: true,
  15. IgnoreErrors: []string{"400", "401", "404"},
  16. }
  17. }
  18. if err := sentry.Init(*option); err != nil {
  19. log.Errorf("ExportSentryOption error: %v", err)
  20. return
  21. }
  22. }
  23. func ReportPanic(ctx context.Context, rval interface{}) {
  24. hub := hubFromContext(ctx)
  25. hub.RecoverWithContext(ctx, rval)
  26. return
  27. }
  28. func hubFromContext(ctx context.Context) *sentry.Hub {
  29. hub := sentry.GetHubFromContext(ctx)
  30. if hub == nil {
  31. hub = sentry.CurrentHub()
  32. }
  33. return hub
  34. }
  35. func AddTag(ctx context.Context, k, v string) {
  36. hub := sentry.GetHubFromContext(ctx)
  37. if hub == nil {
  38. return
  39. }
  40. hub.Scope().SetTag(k, v)
  41. }
  42. func AddTags(ctx context.Context, tags map[string]string) {
  43. hub := sentry.GetHubFromContext(ctx)
  44. if hub == nil {
  45. return
  46. }
  47. hub.Scope().SetTags(tags)
  48. }
  49. func AddExtra(ctx context.Context, k string, v interface{}) {
  50. hub := sentry.GetHubFromContext(ctx)
  51. if hub == nil {
  52. return
  53. }
  54. hub.Scope().SetExtra(k, v)
  55. }
  56. func AddExtras(ctx context.Context, extra map[string]interface{}) {
  57. hub := sentry.GetHubFromContext(ctx)
  58. if hub == nil {
  59. return
  60. }
  61. hub.Scope().SetExtras(extra)
  62. }